Best practices for navigating file structure via terminal?
I've just started my Linux journey earlier this year. As a goal to learn how to self-host applications and services that will allow me to take back some control of my data. Immich instead of Google Photos, for example.
I have a local server running Unraid and 22 docker containers now. And then a VPS (Ubuntu 20.04 LTS) running two apps. I've learned a ton but one thing I can't seem to wrap my brain around is navigation through the file structure using only terminal. My crutch has been to open a SFTP session in Cyberduck to the same device I'm SSH'd to and try to figure things out that way. I know enough to change directories, make directories, using Tree to show the file structure at different levels of depth. But I feel like I'm missing some efficient way to find my way to files and folders I need to get to. Or are y'all just memorizing it and know where everything is by now?
I come from a Windows background and even then I sometimes catch myself checking via explorer where a directory is instead of using CMD or PowerShell to find it.
I'd love to hear any tips or tricks!
EDIT: I've been using Termius because they have a great Android client, but I wasn't about to pay $5/mo for sync. Especially to sync to someone else's cloud. Which led me to Tabby, which I understand has quite a large footprint resource-wise. But I guess I either don't know enough yet to be mad about it or it hasn't impacted any of my systems negatively yet. No Android client though, but you can bring your own sync solution and it has a handy little shortcut to SFTP to the current directory you're in. Between that and stuff like ranger, it's made it so much easier to learn my way around!
I think it’s just a matter of getting used to it. I had the same issue at first and the more I used the command line, the more I started to prefer it to GUI apps for certain tasks.
A couple things that I use all the time:
tab completion is incredible
cd - goes back to the last directory you were in (useful for bouncing back and forth between locations)
!$ means the last argument. So if you ls ~/Downloads and then decide you want to go there, you can cd !$.
:h removes the last piece of a path. So I can do vim /etc/network/interfaces and then cd !$:h will take me to /etc/network.
Others have mentioned using interactive tools like zoxide to easily get to frequently visited directories.
In addition, I also use nnn (https://github.com/jarun/nnn), which is a terminal file manager that you can navigate through. You can create shortcuts, snippets and bookmarks with this. I use this and zoxide + fzf regularly on CLI to navigate.
Some here also mention ranger, which is another terminal file manager. In my limited experience with ranger, I feel like the start up time is much slower than nnn; but I haven’t tried much. Tho with ranger + graphic-accelerated terminals like kitty, I believe you can preview images and files, which seems to be a great feature. So it depends on your need.
Something I haven't seen mentioned here is Ctrl + R on the command line to quick-search history. You start typing/backspacing and it shows the most recent matching history entry. Press Ctrl + R or Ctrl + Shift + R to navigate up and down through matching entries. Press Enter to pick an entry, Ctrl + C to cancel.
Name all directories lowercase, 3-5 letters long, and try to avoid directories with the same starting letter as siblings
That way you can use tab completion with just a single letter
Use the option to jump to subdirectories of /home/user from everywhere.
Not strictly file browsing advice, but you can quickly search for previously issued commands by hitting ctrl-r and starting to type. (and you can press it again to search further back)
For navigating files quickly fzf is pretty much crucial to my workflow. Being able to get my home directory to the directory of the project I want to work on in two seconds flat is such a nice feeling after manually typing the path in for months.
https://github.com/junegunn/fzf
I just use ls, cd, tree and tab completion. Sometimes I will use rg to find files which contains specified string, and use locate to find files which I known name but path.
What file structure? I just put everything in /home and then try to vaguely remember part of the filename and glob *part-of-filename*.
Ok, actually, every couple of years, I move all my files into a new directory, /home/old. I think I'm up to /home/old/old/old/old/old right now. I recommend using find to look for files in there.
There are a few directory structures I have memorized, like my programming projects for instance. For everything else, I use the GUI. That's what it's there for. Mixing and match to get the best of both worlds. Some handy tips:
xdg-open will act like clicking on a file in the GUI, and is an easy way to open folders from the terminal when you want to browse them.
Use sshfs or even just whatever is built into your desktop environment to connect to remote servers and browse them
Most terminals let you drag files or folders into them to paste their paths
Useful one I find is the z program you can install it with package manager and it's also included with zsh shell. It's basically like a smart cd command. Instead of having to type the entire path for cd, when using z you can just type the destination folder and if it's in your history it will resolve the path by itself.
I use ls and ranger, to find files i use find -name and remember that * is used as a wildcard so you can use it when searching for stuff with in incomplete filename or when copying or moving files/directories. You could also use colorls to add some flare to your ls, and oh-my-zsh for syntax highlighting and tab autocomplete
I just type ls everytime I cd into something. It's not that efficient honestly but I usually remember where I want to go after going there a couple times. Also if you hit tab twice after typing cd and a space, it shows all of the files in the directory.
Look into your shell’s tab completion abilities, the find command, and fzf. There’s also stuff like midnight commander but I find that to be a little overkill for my tastes.
Not to be "that guy", but you can use a gui file manager to access your files the same way you do so in windows. Most of them support ssh keys as well. If you'd like to check out the cli stuff, nnn or ranger can be useful. Something like midnight as abckup is good too. Definitely install fzf on both your vps and local machine.
You can also go over board and run xorg over ssh and run a small window manager, maybe awesomewm or even xfce (not that small but works fine).
Not sure if it helps with navigating but ncdu is my favorite terminal app for figuring out where something is. Then I just jump to that. I’m sure there are better ways but that’s my method.
That's a good question 💯 In my case too, it took me some time (read years 😂) to figure out what I'm comfortable w/.
I can think of 3 major ways that you can navigate the filesystem while being able to drop to a shell when you need it:
If you're familiar w/ Emacs, you can either:
Use dired and tramp on your machine to access/navigate the target machine.
Install Emacs (emacs-nox) on the target machine, SSH and then run emacs-nox and voila! No need for tramp in this scenario.
Use Midnight Commander (mc) which offers a TUI pretty much like Norton Commander (nc) from the days of yore.
Get used to the semi-standard structure of the file system and just use plain Bash (cd, pushd & popd) to move around. That is
Understand what usually goes into common directories (like /usr/share or /opt) and try to follow the same pattern when rolling your own software installations.
Learn how to use your distro's package manager to query packages and find out where things, like configurations and docs, are stored. Something as simple as rpm -q --list is what you usually need.
Often I don't find myself navigating directories when I reach for my file manager, but looking for something. Learning to use find or fzf gets rid of a fair amount of shuffling through your file system. Also, don't be afraid to type out full paths when performing copy or move commands. There isn't any reason to go to /home/documents/12/directories/deep when you can simply put the path in your ls or other command. And of course, tab complete is your friend (/ho[tab}/doc[tab]/12/[tab]/deep, etc...)
I felt the same. Simple tasks I do in terminal, but when I have to deal with too many files and folders I use filebrowser. Its amazing docker container with simple GUI
On my personal computer, zoxide, fzf, fzf tab completion allow me to jump around anywhere quite easily, I still use exa/cd for the most part. Look into this if you need more visualization. I still use a GUI file browser from time to time.
Oh my server though, I still use the default shell, so yes I just memorize where things are. But a trick is to allow for a large history file, and I use the command history search (Ctrl-R) because I tend to run the same things constantly. My setup helps too, I run things in docker, and have a data and a config directory, things go into each accordingly, and I bind mount those directories instead of using volumes.
If you edit config files a lot, in vim or nvim, :bro old will give you a list of files you recently edited and you can jump to them by inputting a number.
Vifm is ridiculously customizable but also assumes that you're competent with the modal paradigm of Vim.
If you're in a WM, you can use a terminal emulator with SIXEL support (Alacritty is no longer maintained but Wezterm is great) and you can get image previews as well.
Even on a headless server, I love being able to predefine bookmarks.
A simple workflow would be, 'e goes to '/etc/'. HJKL to whatever directory I want, ZZ, then there I am.
There's so much more that can be done, though.
Edit: It looks like alacritty is alive! No idea why I had thought that it had been abandoned. Apologies for the accidental gaslighting.
I'm surprised I didn't see fasd
fasd (pronounced 'fast') uses 'frecency' (frequency + recently) in order to jump to or open your most frecent documents or directories. A dumber version is z which works as a supliment to cd
Take some bash tutorials on the internet there are loads of them
Learn rsync, it's the best!
I've been a 20 year Linux desktop and server user. I spent my time either in an IDE for development, on a browser, or in shells. Last time I touched a graphical file manager has been years ago, if not decades. Cli shells are so so so so so much more efficient in getting shit done than GUI programs it's not even funny.
Welcome to the dark side, we have cookies!
If you use Linux desktop (you should, it rocks, use KDE!) then install yakuake! It's the friggin best awesome thing ever! F12 and 25 shells drop down on my screen. F12 and I got my browser back.