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 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 me
deleted by creator
Fascinating—I didn’t know that one! I’m definitely going to have to read the manual for that.
deleted by creator
Not sure if you just misread the commands or not, but that’s a pretty different usecase than what they’re describing.
!$ means the last argument. So if you ls ~/Downloads and then decide you want to go there, you can cd !$.
Nice, I usually do !!:n. This is a lot better.
Nothing like extremely cryptic shell scripts
I don’t use it in scripts, only when typing.
Fair enough… i’ll have to test it out
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.Just learned about
rg
, thanks.
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:
$include /etc/inputrc ## arrow up "\e[A":history-search-backward ## arrow down "\e[B":history-search-forward```
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).
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.
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
mc
is killer. All the features of a desktop file manager but in your terminal!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.
You’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.
deleted by creator
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 withls -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.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
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.
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 + /).
I did 4 things, that helped me a lot:
- Make aliases for the most visited directories
alias cem=’cd /home/drops/.config/emacs’
- Make aliases for moving up the tree tree:
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 just have lots of symlinks in my homedir.
I do have
..
andaliases though.
Mostly if I’m gonna work with files I just use
ranger
, or FZF from my shell to find stuff.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 .
Check out ranger fm, helps navigate through files very quickly, also has a ton of features
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
cd
,find
,pushd
,popd
and last but not leastnnn
.cd -
is an easier alternative topushd/popd
.Yes. It’s (
cd -
) in most cases much more convenient.
I find that zsh with plugins makes my life very easy. And if I need to quickly find something, fzf works wonders
Learn to use
pushd
andpopd
Why use a separate command when “cd -“ works just fine to take you to the previous directory
cd a cd b cd c popd popd // you're now in "a"
cd a cd b cd c cd - cd - // you're now in "c" and need to manually cd to "a"
Neat, thanks
You mean
cd a pushd b pushed c popd popd
Right ?
Depending on your shell,
pushd
/popd
might not be an option. For a similar functionality, I like to use a subshell which is portable across all shells:cd a $SHELL cd b cd c # do work here ^D # you're back in "a"
Right, but what if the dir you want to return to was not the last one?
For example, you are working in two dirs and go into your home dir for something, now you can pop yourself back into your long ass hard to type working path.