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.

Comment Linux tutorial or Leave a Trackback

6 Comments

  1. Dejan Tomic
    Posted August 26, 2011 at 7:01 am | Linux tutorial comment

    And how can I set a directory with “!” in its name as the current directory?

    I mean, I have a directory on my USB stick called “!Project”… for example /media/MyUSB/!Project/123/… now how can I switch to it with terminal so I can run my program under it?
    Or it is just unsupported and I HAVE to remove the “!” from the directory name?

  2. Posted August 27, 2011 at 10:39 am | Linux tutorial comment

    Dejan Tomic,
    I didn’t try it, but I think all you have to do is escape the !. so if you wanted to cd to that directory, you can run cd /media/MyUSB/\!Project/123/. By placing a backslash \ in front of a character will escape it, meaning it will take it’s special character properties away and use it as a plain character. Normally in Linux an * is a wild card, meaning it can match anything. So if you have a bunch of images named IMG_0001.CR2, IMG_0002.CR2, IMG_0003.CR2, and keep going. You also have an IMG_000*.CR2.. don’t know why, but you do. If you do rm -rf IMG_000*.CR2 You will remove all of those images, not just the one with the * in the filename. Because * matches 1 2 3 and *. Now you want to delete just the one with the * in the filename, rm -rf IMG_000\*.CR2 now * is an actual * and not matching 1,2,3,etc.

  3. ronak
    Posted November 3, 2011 at 12:57 am | Linux tutorial comment

    naw i am “~” i want to /directory withouct cd /
    haw can i do the /directory?

  4. Posted November 5, 2011 at 1:49 pm | Linux tutorial comment

    Ronak,
    So you are in /home/$USER and you want to be in /SOME-DIR but don’t want to cd /? You can cd /SOME-DIR or you can cd ../../SOME-DIR not sure if that answers your question or not. Let m know, thanks for stopping by!

  5. sairam
    Posted November 21, 2011 at 1:25 am | Linux tutorial comment

    please
    Write and execute the command to change the /bin directory

  6. mike
    Posted March 3, 2013 at 10:32 am | Linux tutorial comment

    Nah, just more confusing, why not just show a simple list of commands, eg move up the directory tree, move back down again, move to specific directory irrespective of present location. The instructions given above are too verbose, why not give a simple command, followed by the verbose description

One Trackback

  1. […] cd – is used for changing into a different directory in the Linux shell […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*