Changing the working directory
You might think that you already know how to change your working directory, but do you do it efficiently?
Current/Working directory
The working directory is the directory from which your terminal "is". (It can be printed with the pwd command.)
In fact, this definition does not make much sense, since your terminal does not have a location (except the path of its corresponding binary executable!). A more proper definition would be: the working directory is the directory from which relative paths are expanded.
With this definition, it become clear that it is relative to a given terminal, at a given moment. Besides, it is compatible with the idea that one can intuitively have!
Basic usage of cd
cd is a shell builtin command.
To "go" to directory DIR (i.e. to make DIR the new working directory), simply execute the command cd DIR.
Just a piece of advice: It is preferable to run a long cd command (i.e. a "long jump"), than multiple small cd commands (i.e. multiple "small jumps"). Why? Simply because a long cd command is much more reusable, assuming you often do the same jumps, and would like to reuse your old cd commands from your bash history (see my first article for more details)!
Basic cd tricks
- Of course,
cd ..will make you "go" to the parent directory (of the current working directory), since it is what..basically means. - Without argument (or with a null-sized argument),
cdwill make you "go" to your home directory. - Finally,
cd -will make you "go back" to the previous working directory.
This last feature is pretty nice, but you can't "go back" more than 1 directory with this method (you will loop, since you will "go back" to the directory from which you "went back"...). You can see it as a working directory history, but you can only "go back" once...
But I would not talk about this if there was not a better way... Because yes, there is! And I will even show you how to customize your .bashrc to use this feature without changing your habits!
Working directories stack
Actually, there exist two commands to define an history of your working directories. This history is stored as a stack (last in first out data structure): it behaves like you would imagine an history works, but you can only "go back" to previous "locations" (and not "go next"). Note also that a working directory stack is defined for a given terminal (it is not shared with other terminals being executed, as you would expect).
pushdcan be used ascd, expect it will also save the current working directory into the top of your working directory stack.popd(without arguments) will make you go to the directory on top of your working directory stack, and remove it from your stack. So basically,popdwill make you "go back" in your working directory history! (as long as you usedpushdto navigate instead ofcd)
These commands are really powerful, but they require to always use pushd instead of cd, which can be tedious. Because let us be honest, we are used to type cd everyday, and cd is much faster and convenient to type than pushd, even with autocompletion (yes, Linux users are lazy, and that's a good thing!).
Therefore, you can simply create an alias for cd, to include a pushd command with the following command to add to your ~/.bashrc file:
alias cd=">/dev/null pushd"
Enjoy!