Change Directory using cd Linux Command

In this Linux tutorial for beginners, you will learn how to change directories in Linux using the cd command. The Linux cd command is very easy to use. There are a couple options you can use with the cd command, but I won’t cover them as I hardly ever use them. I will show you how to use some common variables with the Linux cd command though.
When using the Linux cd command to change directory, the syntax of the Unix cd command is cd [directory]. Pretty easy to remember right? So if you are wanting to change your current working directory to your home directory, you can run cd /home/max as max is my username. With the cd command if you do not specify any directory it will automatically change directory to your home, so just running cd in the Linux shell will take you to your home directory as well. Another way to get to your home directory in Linux is using the home variable $HOME. So if you type cd $HOME you will be taken to your user’s home directory as well.
You can also use the $HOME variable as part of a path. Say if you wanted to change directory to /home/max/images/family you could run cd $HOME/images/family which will take you to /home/max/images/family but of course your $HOME not mine!
Some other common uses of the Linux cd command is to change directory to the parent directory. To do this you could use cd ../ which will change directory to the parent directory of the active directory you are already in. You can also use ../ as part of your path, so say my current working directory is /home/max/images/family and I want to change directory to /home/max/images/friends you have 2 options. You can run the cd command with the full path: cd /home/max/images/friends or your other option is to use the ../ parent directory as part of your path like this: cd ../friends/ since I was already in /home/max/images/family the parent directory is /home/max/images/
One more common thing I see some people asking is how to change to a directory with a space in the shell. When you are in the command line interface a space is a special character so you will have to escape it using a backslash \ I usually try not to make any filenames or directory names with any special characters in them, but if you do have a directory named say Linux Stuff you would use cd Linux\ Stuff to change directories.
You can also use the cd shell command to change directory as part of a script. For example if I was wanting to make a script that would change directory to /usr/local/games/quake3 and then run ./quake3 since the quake3 game needs your shell to be in /usr/local/games/quake3 as the working directory for the game to run properly, you could run cd /usr/local/games/quake3 && ./quake3 which you can save to a file in /usr/bin/quake3 so when you run quake3 the shell script will cd /usr/local/games/quake3 and if that runs with no errors the shell script will then also run ./quake3
At the moment I am pretty tired, so the quake3 game was the only thing I could think of off the top of my head that you would want to script the Linux cd command.
I hope this cd Linux tutorial for beginners has helped you learn more about the Linux shell command cd so you can change directories easily using the Linux shell. Remember to come back and visit Beginner Linux Tutorial for more Linux tutorials for beginners as this site will always be adding more Linux tutorials.

No comments:

Post a Comment