Navigating with the Linux Command Line using the cd command
Goal: Navigating the Linux file system
Prerequisites: Access to a Linux console
One of the most common things a server administrator has to do is navigate through the operating system. While in many ways this is a very trivial task there are a few shortcuts that some may not know about.The cd command is by var the easiest way to navigate to your files.
To change directory you can use the following syntax:
[~]# cd [directory]
Take the scenario where you are sitting in /home/ and want to navigate into /home/joe you could run the following command:
[/home]# cd joe
Now lets say you are currently residing in /home/joe, but you want to switch to view the mysql logs on your server. Instead of going back to root then going up through the directories one at a time, you can specify that you want to start at the root by starting your [directory] with a / like so:
[/home/joe]# cd /var/lib/mysql/
Notice that since i proceeded var with the / the cd command knows that we are not going to /home/joe/var/lib/mysql but instead to /var/lib/mysql.
If you want to change directory into the directory above your current location you can use .. for the directory. In this example it will take you from /home/joe back to /home.
[/home/joe]# cd ..
Similarly if you wanted to go back up 2 directories you could use the following command:
[/home/joe]# cd ../../
If you want to change directory back to your home directory at any time you can use the command below. In this example I will be going from /var/lib/mysql to /home/joe since I am logged in as the user joe.
[/var/lib/mysql]# cd ~
Finally, one of the commands that I find myself using extremely frequently is the command to return / go back to your last directory you were working from. So for example, if I change directory from /var/lib/mysql to /etc, then want to go back to /var/lib/mysql I can use this command to go back one directory:
[/etc]# cd -