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:
cd -
goes back to the last directory you were in (useful for bouncing back and forth between locations)!$
means the last argument. So if youls ~/Downloads
and then decide you want to go there, you cancd !$
.:h
removes the last piece of a path. So I can dovim /etc/network/interfaces
and thencd !$:h
will take me to/etc/network
.Besides using
!$
,alt+.
on bash copies the last argument into the line you are typing, which might be better if you want to edit it.I've used the
cd -
several times, it is very handy. the others are new to me so I'll check it out.I might be wrong here, but "cd" without any arguments works as "cd -" for me.
cd
without arguments takes you to$HOME
, so it's the same ascd ~
interesting, that’s equivalent to
cd ~
for meYou're pressing the tab key for auto completion right?
Um...no. I'll admit I didn't know that was an option. Weirdly I do it all the time in PowerShell. Though I am using Termius right now and at least on Android it doesn't support tab auto complete. That said, it does auto suggest as you type to get you in the ballpark. I'll have to try it again from my PC once I get my office put back together.
Also make use of the up arrow on your keyboard, with which you can quickly reuse commands you have ran recently.
For example you search for a directory with
ls -alh
in multiple steps, and when you have found the one you actually want to work in, then you once again press up, and either edit the command fromls
tocd
to switch to it, or just edit it to your favorite text editors command and put the file name at the end of the path. Tab helps with that too.Tab completion is done by the shell, not by the command you want to use, though worth mentioning that some tools install tab completion helpers your shell makes use of automatically.
It does not work on all terminal but you can also your the beginning of a command then the up key. It will show you only the previous command that start the same way.
I have been doing that, useful on Windows as well and I do that mess for my job now.
I use Termius on iOS and double tapping the screen sends a tab (I may have enabled it in settings but I don’t think so). I think you can also put a button for it above the keyboard. In any case it does work for tab completion. I know I’m on iOS and not Android but I’d be really surprised if the Android version had no way to send a tab…
You are correct, I just tried it. I have a keyboard on my Galaxy Tab S7+ and it will recognize the Tab key in normal text entry fields but doesn't seem to work in Termius. The double tap is pretty clutch!
Weird the keyboard didn’t work but glad the double tap did! It’s definitely clutch
Oh and then there's
ncdu
too with which your can navigate your fs, but that won't allow you to open files, it is for finding what takes a lot of space.The vim text editor can also let you browse directories and open files in them, when you pass a folder's path to it. But that may be an extension now that I think about it.. maybe tpope's plugin loader does it? But maybe it isn't so it's easiest to just try it out
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 andzoxide
+fzf
regularly on CLI to navigate.Some here also mention
ranger
, which is another terminal file manager. In my limited experience withranger
, I feel like the start up time is much slower thannnn
; but I haven’t tried much. Tho withranger
+ graphic-accelerated terminals likekitty
, I believe you can preview images and files, which seems to be a great feature. So it depends on your need.I am old and I like ‘mc’ (Midnight Commander) a lot when doing many/complex file operations
Reminder that mc has the ability to make directory bookmarks (Ctrl + /).
Same, as I used Norton Commander under DOS, good memory!
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.
Also, if OP is new, they may not yet be aware of aliases and functions. Generally you'd out those in a ~/.bashrc file that gets automatically executed when a terminal starts. They'll allow you to save a more complex command as a really simple one. And particularly can be useful when things you want to run are in unusual directories. Eg, maybe you have a git repo somewhere that contains some project you spend most of your time on, so you could have an alias that just cd's you to it's directory. Git also has its own way of doing aliases and that's really nifty for the more complicated git commands (or the more commonly used, like st for status).
I saw that mentioned in another comment and I've been testing it out while I try to get Cryptpad installed on my VPS and its very nice!
You can also make this the default behaviour as you start typing a command.
Create ~/.inputrc and add these lines:
ls / cd for basic stuff
fzf if I want to find my way through the history
broot if I want to search for a file
ripgrep if I want to find a file with specific contents.
I know that the last 3 are not available by default, but they are good pieces of software, so I'm just going to install them.
You might already know, but fzf does a lot more than just history. You can also use it to find files, open files, and cd into different directories and more.
I've never heard of broot but it looks cool and I'm about to check it out right now.
I simply use ls and spam cd xyz cd .. etc
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)
Can't believe I've never heard of this before, thanks for the tip!
Another person saved from hitting up 50 times
Damn, that is incredibly useful. Thanks!
Also check https://github.com/junegunn/fzf and https://github.com/ajeetdsouza/zoxide
Two great utilities that will reduce the number of file jumping and searching you need to do. :)
More useful tools: https://zaiste.net/posts/shell-commands-rust/
I did 4 things, that helped me a lot:
alias cem=’cd /home/drops/.config/emacs’
alias. .=’cd. . && ls’
Three points for two levels up, etc...
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.
Instead of aliases, I use variables that I set in my .bashrc.
For example, on WSL I have
export WINDOWS_HOME=/mnt/c/Users/username
. Then I can justcd $WINDOWS_HOME
. Orcp $WINDOWS_HOME/Downloads/some_file .
Instead of aliases, I just have lots of symlinks in my homedir.
I do have
..
and...
aliases though.Mostly if I'm gonna work with files I just use
ranger
, or FZF from my shell to find stuff.Ranger looks like how my brain wants to work. I'll have to check that out. I like the idea of symlinks too.
The Linux Command Line is a book I still go back and look stuff up in.
A meaty tome! That looks super handy to have around though.
I just use
ls
,cd
,tree
and tab completion. Sometimes I will userg
to find files which contains specified string, and uselocate
to find files which I known name but path.In Linux, the
locate
command is crazy fast. I am amazed at how slow search is in Windows, compared to this.Locate cheats by using a pre-built index.
But after the 2.5 years it takes to build the Windows index wouldn't it be the same - just searching through a built index?
lol i remember hunting and systematically ripping out the file indexer in like vista? I think? It was terrible lol
It was honestly hilarious how bad it was and how much it could slow the system down in some situations. I'd be curious to see just how much of it was a byproduct of HDD's - that is to say, just how slow is it even in solid state drives!
It's been a while since I've had a Windows machine, but doesn't Windows index the content of files as well as their names? If so, that would have fairly profound differences from
slocate
.Windows indexes files too though.
Windows has a pre-built index as well (or at least it has a search indexer service that enjoys as warm a CPU as possible). That doesn't appear to improve the speed of search, though.
Unless you use everything (that's what the software is called) to search.
It's so fast I'm amazed why microsoft can't have something similar out of the box.
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've got to agree. It's replaced z for me and its just a great multipurpose tool
I will definitely look into this, thank you
I'm using both of them:) zoxide comes with a
zi
command which lets you search through your recent directoriesAgreed, fzf (and similar fuzzy finders) have been a game-changer with regards to the way in which I navigate the shell. Add in a couple of one-liners and I'm never more than a second away from any nested directory
Here are some of the most used aliases in my configs if anyone would like to try it out
Note that they use
fd
andexa
but they can easily be swapped out forfind
andls
if those aren't available on your system (which would allow for shorter aliases since they're the fzf defaults IIRC)To navigate quickly between directories in ssh, I recommend you using zoxide as cd replacement.
Zoxide remembers the directories you visited, then you can jump to them very quickly.
Check out ranger fm, helps navigate through files very quickly, also has a ton of features
lf sounds interesting, too. Haven't tried it yet. The link is a guide on migrating from ranger.
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.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 usingfind
to look for files in there.You should be putting them onto CDs. Lets you have cool covers to recognise them how old it is.
You kid, but I just ripped a bunch of old data CDs and decided to also scan their covers and stick them in the tar.gz file along with the images. Some of them were pretty creative.
Back when I started my dream was go have folders of DVDs of all things I would need. Luckily for me cheat fast HDDs became a thing. Still should burn atleast my favourite contents.
Yeah. I recently bought a tape drive to do just that. Turns out fiber channel hbas are harder to get working than I thought. First one didn't fit, like physically the card was too long and hit the CPU fan power connector. Second one fit, but the computer wouldn't boot. Third I get a driver error, and since it's enterprise stuff the threads I find on it basically say 'contact your vendor'. At least they're really cheap. Should have spent the extra money for a SAS compatible one.
Not necessarily navigation, but
ncdu
will give the total size of your directories. It's a simple but very useful toolOh that is handy. I've been trying to figure out wtf is taking up so much room on my VPS that's only running Miniflux and Wallabag.
Use the fish shell... No, seriously it's autocomplete and tab functionality makes browsing directories through the terminal so much easier
lol, their website is delightfully retro. Only thing missing is a random GIF that has no context, just there to grab attention. https://fishshell.com/
i have broot, an interactive tree
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 autocompleteI know there is probably a historical reason but I hate how find parses its arguments.
Any other app would be fine --name or find -n.
Every time I use it I have to spend a few minutes checking the results to make sure that it's actually doing what I want it to do.
That's one of the reasons why the more modern
fd
is a nice alternative: it accepts command line args as you'd expect.Also every other search program has the needle as a positional argument and either reserves a named parameter to specify haystack, or has the haystack come after.
Apparently the
find
devs thought users would spend more time using it as an alternative tols -a
than finding specific files@BaumGeist @nathris try zsh plugins/ widgets in term, that helped me
Depending on system, something like
locate
/mlocate
might be installed, and is almost certainly available if the following seems like a good idea.Tools/daemons like them are quicker for finding files - basically because they index all files except those in specified places. (Or potentially only those in specified places depending on tool/configuration.)
That way, rather than
find -name 'some_wildcard_string'
, it's insteadlocate 'partial_filename_match
orlocate --regex 'some_regex_string'
.As for speed:
locate / | wc -c
returned 565035, the count of files currently indexed bymlocate
on my computer, in 0.3 seconds. Quite a bit quicker thanfind
! (locate /
literally returns any file with a/
in the full pathname, which basically means every single file in its DB).cd, ls, and the tab key are the basic tools of terminal filesystem navigation
Try a TUI like mc (midnight commander)
pushd and popd may be useful
I tend to use walk or midnight commander. zoxide is nice as well. It also helps to have bash aliases. https://github.com/antonmedv/walk
https://github.com/ajeetdsouza/zoxide https://midnight-commander.org/ https://www.hostinger.com/tutorials/linux-commands
@craigevil @Father_Redbeard
Hi I use Ranger in BASH terminal a lot, let me weigh in on how very useful it is and how easy to customize to my needs it has been. There is a similar app written in C++ But I couldn't script it as easy. Until I found Ranger I also used MC as my workhorse.
mc
is killer. All the features of a desktop file manager but in your terminal!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.
Not sure if limited to certain emulators, alt+l should do that for you
I think pressing TAB sends Alt+I to the terminal so yeah.
Not following you.
Forgot a word sorry. I said pressing TAB or Alt+I are the same thing. But I was mistaken, it's tab and Ctrl+I.
Not used to ctrl+i so thanks.
That's where I'm at now. And it does work. But I knew there had to be something out in the wild that folks use to traverse or at least understand where they are better. I do like Tree for a more in depth
ls
though. I don't recall the options you can throw at the end of tree off the top of my head but you can specify how many layers you want to go down to see a visual of the file structure.Problem with most tips and tricks is that they require customizations to the OS. Many people who use Linux in a more advanced fashion, tend to also use lots of different systems, e.g. because they're a system administrator. And you don't want to have to make customizations to every system just to be able to get around. So, you learn to work with the lowest common denominator (mostly POSIX commands).
Many of us do still choose to make some customizations to our most used systems, but yeah, we try to keep it lightweight and mostly just utilize tools that aid in using the POSIX commands, not replace them.
exa is a nice alternative to ls and tree commands. Just add an alias to them based on the views you want.
But like other comment points out avoid lots of customization if you work on various shared systems, esp SSHing in.
With my keyboard layout and other keybind customizations my system is pretty unusable to others except basic mouse on browser. Like wise i have trouble using others' systems and need to setup any new installs to a precise way before able to work. Slightly regret going too much into customization in certain aspects.
You can quickly display the filesystem hierarchy using the
tree
command.Yeah I've been messing with that. I like that you can limit how deep you want to go, like if you just want to see folders but not the files within, for example.
Just using "cd -" to go back where you were is useful.
history command and ! and the number in history, like
!123
For instance, it's useful
pushd
andpopd
are good for keeping a history and going back to different directories.If you like those, using cdr within zsh is amazing. It automatically keeps track of where you've been, and you can set up tab completion to show the history with a number next to each directory for easy switching.
Iirc, that was my main reason for switching to zsh a few years ago
That's really cool. I guess the only advantage is pushd and popd is that they're usually already installed.
you can also put !! for the most recent history entry, useful if you forget to put sudo
Old school, but I really like midnight commander or mc. Fast and easy.
Another good one is ranger - more concise, but great.
I love ranger. Mostly I just use the basic shell, but when I'm doing a lot of random file transfer, seeing what I'm doing with ranger is nice.
It's super fast and intuitive if you're already into vim and vim-style keybindings.
@indigomirage @Father_Redbeard
I used mc 20 years or so ago. Now I use nnn.
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).
I was speaking more towards my VPS , which of course has no OS GUI. In Unraid I do use the very nice GUI regularly though.
Midnight commander is a pretty nice alternative!
There are tools like sshfs that let you mount remote directories as if they're local. Most of the time I tend to use nnn with some of the little extra bits you can find in the docs like cd to last directory on quit and multi-colored tabs etc.
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...)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:
dired
andtramp
on your machine to access/navigate the target machine.emacs-nox
) on the target machine, SSH and then runemacs-nox
and voila! No need fortramp
in this scenario.mc
) which offers a TUI pretty much like Norton Commander (nc
) from the days of yore.cd
,pushd
&popd
) to move around. That is/usr/share
or/opt
) and try to follow the same pattern when rolling your own software installations.rpm -q --list
is what you usually need.HTH
cd
,find
,pushd
,popd
and last but not leastnnn
.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
https://github.com/agkozak/zsh-z
Any directory in the filesystem is less than 5 key presses away
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 aconfig
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.
When did alacritty die? I thought it was still actively maintained. Maybe I missed an important commit somewhere but it looks active as of now?
Maybe I was wrong? Gonna check again. Alacritty was my fave. Hopefully I misread something.
Perhaps when it was under jwilms original repo and now its under the alacritty org. Looks like it's going strong and moving from yaml to toml config, which makes me happy.
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
I find that zsh with plugins makes my life very easy. And if I need to quickly find something, fzf works wonders
@Father_Redbeard
alias !="cd .."
Works wonders on azerty keyboards, where ! is close to return key. I guess you could pair another key with ctrl for the same effect ?
if i’m not really sure what the contents of a given folder are, and i want to look into files as i go, i’ll often just open vim in file explorer mode
Pardon the ignorant question, but can you do that with a headless server as your target? I've not used Vim at all.
Yes, vim is a command line program.
If you look up "Cli file manager", there's a bunch that you can check out and try.
Tree, grep, and find are usually my three go-tos. Tree to get a general view of a ton of nested files/folders, then if I know a name I'll use
find . -name "filename"
, if I know a bit of contents, i'll use `grep -re "content string" to find files containing that.I recommend reading the man pages because you can often chain together these in fairly powerful ways.
Awesome! You've given me some reading material. Thanks.
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.
I just finished buttoning up my new PC build and installed Linux on it, actually. Pop!_OS though because I like the layout and it's very un-Windows-like which is what I was after. I'm excited to learn more!
Still, get yakuake. It's probably my favorite apo
I will! Looks slick. Thanks for the input.
Depends on your workflow and file structure, but nvim with nvchad works great for coding. I also sometimes use ranger to better see file structures.