CD and more - save time with powerful terminal navigation on Linux & macOS

CD and more - save time with powerful terminal navigation on Linux & macOS
Photo by Lukas / Unsplash

Why time matters

Have you thought how many times you use your file manager? Yeah, it's very comfortable when you need to work with photos, videos or any content suitable for visualization. Some people are scared to use console 'cause it's "too hard" and "can't type too much". Hope this post will open your eyes about cli power and will give a boost for daily tasks.

The Beginning

cd as change directory works in Unix, DOS, IBM OS/2, MetaComCo TRIPOS, AmigaOS, Microsoft Windows, ReactOS and Linux and became a live classics decades ago, since 70s thanks to Bell Labs and early Unix. If Hall of Rock'n'Roll *nix Fame exists, this command definitely must be in the first row. Unix Philosophy as it is: do navigation and do it well.

OK, let's start from /home/$USER, "Home, Sweet Home" directory.

  • $ cd - we switched to home directory for current user. Available in "HOME" environment variable too.
  • $ cd .. - move to the parent directory.
  • cd ~/Documents - easy access to dirs in /home/$USER
  • cd ${PWD}/a - move to the current directory/a subdirectory.
  • cd mydir/sub<Tab> - use Tab competition: for example, sub<Tab> will be completed to subdir or subway.
  • ↕️ - navigate using history with Up/Down keys.
  • cd current_project - use symlinks for long paths. Example: ln -s current_project work/company/X/Project124.

autojump - recording paths

Why not record the often used paths and create a database? This idea was used in autojump. The docs recommend using a wrapper called j for faster typing. To enable autojump add source /usr/share/autojump/autojump.sh to ~/.bashrc. After first run the database will be located in ~/.local/share/autojump and can be viewed with autojump --stat.

  • j foo - fast jumping to directory that contains foo, like /home/user/project1/foo.
  • jc foo - jump to subdirectory, like foo/bar
  • jo documents - open directory in file manager
  • jco images - same like above, just for subdirectory. This command will open images/2022, for example.
  • j mail - when you have several users with mail directory, better to which directory will be opened first. j p mail command will open peter/mail directory, 'cause p points to peter.

cdargs - bookmarks for command line

Bookmarks work very well in browser, why not reuse it for terminal? Typing long paths every day is boring, and people eventually try to optimize it. After installation, you should run source /usr/share/doc/cdargs/examples/cdargs-bash.sh once or better add it to your ~/.bashrc.

For example, we need to bookmark a directory with books. Type cdb and the browse mode will be opened. Then type a to add a new bookmark or d to delete one. Also interactive way can be faster: when you're located in ~/home/user/books just type cdb bk and verify something not going wrong:

$ cv
0 [bk       ]  /home/user/books

At the bottom there's a simple status with L: and B: designed to help with current status: L - list mode, B - browse mode. To display hidden directories, type ..

Finally, daily usage will look like this:

  • ca books - add bookmark called books
  • ca - added current directory
  • cdb books - move to a books directory

Super easy, I like it!

tree

Printing real trees from files and directories, no stings attached. If you have a lot of nested files & dirs, it will bring a light to full picture.

Useful options:

  • tree -f - print full paths
  • tree -d - print only directories, great if you don't need files
  • tree -d -f -L1 - in case if you don't need deep search, -L5 will show five nested directories
  • sudo tree -f -P *.iso - filter your results, this one will look for .iso images
  • sudo tree -f --prune - ignore each empty directory

popd && pushd

These commands aren't so popular into interactive terminal. Often we need to do something in one directory and move back.

$ cd /home/user/documents
_do_something_
$ cd -

Sometimes in some limited terminal sessions the troubes with $OLDPWD are possible, so we better use pushd && popd:

$ pushd /home/user/documents
_do_something_
$ popd

Readline

The GNU library to interact with command line. If you have used terminal in the past, you probably used Readline too, even blindly. Just type any word in terminal and hit Ctrl+b. What happened? Yeah, you're just using Readline API right now. This is what Readline created for - making terminal better with some keyboard tricky hits.

Most useful hotkeys:

  • Alt+d - erase a word right to cursor
  • Ctrl+k - wipe all to right
  • Ctrl+y - will insert elements from clipboard
  • Ctrl+a, Ctrl+e - Home and End keypad alternative
  • Ctrl+d - close the shell, exit alternative
  • Ctrl+h - backspace
  • Ctrl+k - clear all right to cursor and move it co clipboard
  • Ctrl+r - reverse search
  • Ctrl+t - transpose characters, must have for popular typing error
  • Alt+r - remove changes and rebuild command from history

powerline

powerline, source: official website

In short - very useful tool for terminal, the case when you understand it from first sight, super extendable and configurable. After installation, add below to your ~/.bashrc

if [ -f `which powerline-daemon` ]; then
  powerline-daemon -q
  POWERLINE_BASH_CONTINUATION=1
  POWERLINE_BASH_SELECT=1
  . /usr/share/powerline/bash/powerline.sh
fi

and look at ~/.config/powerline configuration file and setup it as you like.

Powerline can visualize:

  • current directory, also above/below
  • environment variables
  • active background jobs counter
  • remote SSH server hostname
  • CPU load
  • git: great for branches with different colors
  • date and time
  • networking
  • weather
  • email alerts

For even more customizations, also check powerline-shell project:

powerline-shell logo, source: official website

Final note

You don't need to memorize all the tips from above; just start to use some of them daily without any fear, and, hopefully, they will help to finish your tasks faster!

Read more