CD and more - save time with powerful terminal navigation on Linux & macOS
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 tosubdir
orsubway
.↕️
- 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, likefoo/bar
jo documents
- open directory in file managerjco images
- same like above, just for subdirectory. This command will openimages/2022
, for example.j mail
- when you have several users withmail
directory, better to which directory will be opened first.j p mail
command will openpeter/mail
directory, 'causep
points topeter
.
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 calledbooks
ca
- added current directorycdb 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 pathstree -d
- print only directories, great if you don't need filestree -d -f -L1
- in case if you don't need deep search,-L5
will show five nested directoriessudo tree -f -P *.iso
- filter your results, this one will look for.iso
imagessudo 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 cursorCtrl+k
- wipe all to rightCtrl+y
- will insert elements from clipboardCtrl+a, Ctrl+e
- Home and End keypad alternativeCtrl+d
- close the shell,exit
alternativeCtrl+h
- backspaceCtrl+k
- clear all right to cursor and move it co clipboardCtrl+r
- reverse searchCtrl+t
- transpose characters, must have for popular typing errorAlt+r
- remove changes and rebuild command from history
powerline
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:
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!