There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

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!

trompete , (edited )
@trompete@hexbear.net avatar

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.

nestEggParrot ,

You should be putting them onto CDs. Lets you have cool covers to recognise them how old it is.

constantokra ,

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.

nestEggParrot ,

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.

constantokra ,

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.

DrOps ,
@DrOps@feddit.de avatar

I did 4 things, that helped me a lot:

  1. Make aliases for the most visited directories

alias cem=’cd /home/drops/.config/emacs’

  1. Make aliases for moving up the tree tree:

alias. .=’cd. . && ls’

Three points for two levels up, etc…

  1. 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
  2. Use the option to jump to subdirectories of /home/user from everywhere.
Ricaz , (edited )

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.

Father_Redbeard OP ,
@Father_Redbeard@lemmy.ml avatar

Ranger looks like how my brain wants to work. I’ll have to check that out. I like the idea of symlinks too.

dandroid ,

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 just cd $WINDOWS_HOME. Or cp $WINDOWS_HOME/Downloads/some_file .

veroxii ,

You’re pressing the tab key for auto completion right?

Father_Redbeard OP ,
@Father_Redbeard@lemmy.ml avatar

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.

einsteinx2 ,
@einsteinx2@programming.dev avatar

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…

Father_Redbeard OP ,
@Father_Redbeard@lemmy.ml avatar

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!

einsteinx2 ,
@einsteinx2@programming.dev avatar

Weird the keyboard didn’t work but glad the double tap did! It’s definitely clutch

ReversalHatchery ,

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 from ls to cd 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.

Father_Redbeard OP ,
@Father_Redbeard@lemmy.ml avatar

I have been doing that, useful on Windows as well and I do that mess for my job now.

BastingChemina ,

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.

ReversalHatchery ,

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

zikk_transport2 ,

Wait until you try “oh-my-zsh”… 😅 That’s autocompletion on steroids lol.

rambos ,

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

swartze ,

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...)

indigomirage ,

Old school, but I really like midnight commander or mc. Fast and easy.

Another good one is ranger - more concise, but great.

bayaz ,

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.

tongri ,
@tongri@mastodon.social avatar

@indigomirage @Father_Redbeard
I used mc 20 years or so ago. Now I use nnn.

Rescuer6394 ,

i have broot, an interactive tree

nightwatch_admin ,

I am old and I like ‘mc’ (Midnight Commander) a lot when doing many/complex file operations

Magister ,
@Magister@lemmy.world avatar

Same, as I used Norton Commander under DOS, good memory!

lemmyvore ,

Reminder that mc has the ability to make directory bookmarks (Ctrl + /).

orizuru ,
@orizuru@lemmy.sdf.org avatar
  • 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.

somedaysoon ,
@somedaysoon@lemmy.world avatar

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.

bahmanm ,
@bahmanm@lemmy.ml avatar

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.

HTH

kool_newt ,

Learn to use pushd and popd

SnachBarr ,

Why use a separate command when “cd -“ works just fine to take you to the previous directory

teawrecks ,

<span style="color:#323232;">cd a
</span><span style="color:#323232;">cd b
</span><span style="color:#323232;">cd c
</span><span style="color:#323232;">popd
</span><span style="color:#323232;">popd
</span><span style="color:#323232;">// you're now in "a"
</span>

<span style="color:#323232;">cd a
</span><span style="color:#323232;">cd b
</span><span style="color:#323232;">cd c
</span><span style="color:#323232;">cd -
</span><span style="color:#323232;">cd -
</span><span style="color:#323232;">// you're now in "c" and need to manually cd to "a"
</span>
wgs ,
@wgs@lemmy.sdf.org avatar

You mean


<span style="color:#323232;">cd a
</span><span style="color:#323232;">pushd b
</span><span style="color:#323232;">pushed c
</span><span style="color:#323232;">popd
</span><span style="color:#323232;">popd
</span>

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:


<span style="color:#323232;">cd a
</span><span style="color:#323232;">$SHELL
</span><span style="color:#323232;">cd b
</span><span style="color:#323232;">cd c
</span><span style="color:#323232;"># do work here
</span><span style="color:#323232;">^D
</span><span style="color:#323232;"># you're back in "a"
</span>
teawrecks ,

Ah, I guess I have auto_pushd enabled in zsh, so it just happens when I cd. TIL.

SnachBarr ,

Neat, thanks

kool_newt ,

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.

RovingFox ,
@RovingFox@infosec.pub avatar

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.

exylos ,

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.

buckykat ,

cd, ls, and the tab key are the basic tools of terminal filesystem navigation

underisk ,
@underisk@lemmy.ml avatar

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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines