The CLI (Command Line Interface) is a very powerful, flexible and extremely programmable environment that lets you do anything you can ever think of …well, perhaps except for slaying the Dragon of Isengard. To harness its power, one’s mind needs to be as clear as the cloudless hour, and thoughts as pure as mountain dew. Although the road to enlightenment is long and arduous, there are some shortcuts that will help keep what’s left of your sanity.
Below you’ll find some tips on efficient navigation in the CLI.
Go Back Home
The cd command takes you back to your /home directory:
Go Back to the Previous Directory
To go back to the previous directory, you can use cd with a dash (–):
$ cd ~/data/projects/dotfiles/i3
$ cd -
Use the Last Argument of the Previous Command
The $_ variable returns the last argument of the previous command. This can be helpful in a variety of scenarios:
$ cp i3status.tar.gz ~/data/builds/i3/i3status
$ cd $_
(This will execute script.sh)
You can make your life easier by creating aliases (= shortcuts) for commands that you use often. The syntax is very simple:
You can place your aliases in ~/.bashrc. You might need to create this file. Each time you edit this file you need to source it afterwards for the changes to take effect:
When it comes to navigation, one could, for example, create a few aliases to speed up navigating up the directory tree:
alias 2.='cd ../.. ; pwd'
alias 3.='cd ../../.. ; pwd'
alias 4.='cd ../../../.. ; pwd'
The value of an alias can be quite complex. As you can see, 4. will first change directories (cd ../../../..) and then print the current working directory – pwd. Please note a semi-colon (;) separating the commands.
$ cd -
Aliases can be used in a number of different ways. A few more examples:
alias slacktop='ssh user@slacktop'
BASH features some helpful directory stack buildins that help you navigate recently visited directories.
pushd – push a directory into the directory stack and cd to it.
popd – remove a directory from the directory stack and cd to it.
dirs – display the list of the directories in the stack.
How does it work in practice?
First of all, add a directory to the stack. Please note that it also automatically switches to the directory (the -n flag suppresses this behaviour).
Alternatively, you can cd to a given directory and issue:
After adding a few directories you can display the content of the stack:
Please note that the first entry always displays the current working directory so if it also sits at the top of the stack, you’ll see what seems like duplicate lines. The -v flag is responsible for a nicely indexed output.
To switch to one of the directories in the stack you could issue:
Admittedly, this is not the most concise way of changing directories. To make it shorter we can add an alias and a function to the ~/.bashrc file.
List the current stack by simply typing dv.
if [ -z $position ]; then
echo "You need to specify a directory in the stack"
cd $(dirs +$1 -l)
The cdd function (based on this one) makes it possible to cd to a given directory from the stack by typing:
Hope this will help you in your quest to become a CLI wizard.