The Linux cd command

The Linux cd command is used to navigate around the Linux filesystem. In this post I'll show the most common uses of the cd command.

To move to another directory on the filesystem just use the Linux cd command to move to the desired directory. For instance, this command:

cd /tmp

moves you to the /tmp directory, and this command:

cd /foo/bar

would move you to a directory named /foo/bar, assuming that directory existed.

cd - going home

Wherever you are in a Unix filesystem if you type the cd command by itself, like this:

cd

you'll move to your home directory. (If you're already in your home directory it may look like nothing happened.)

cd - going back

To move back to whatever your previous directory was, just type this:

cd -

That's a very nice shortcut, and I thank whoever came up with that one initially. :)

Moving up

If you want to move up one directory level type this:

cd ..

Unlike the DOS "cd" command, the space after the cd is important, so make sure you include it, or you'll get a "command not found" error message. To move up two directories you type this:

cd ../..

and to move up three directories you type this:

cd ../../..

and so on.

If you had a directory named html that was up three levels from your current location you could type this to move there in one command:

cd ../../../html

Going down

If instead you want to move down through a set of subdirectories named level1/level2/level3/level4 you can just type this:

cd level1/level2/level3/level4

And don't forget the command-line expansion stuff. Rarely do you need to type something out fully. Often you can just type a few characters and then use the [Tab] key to fill in the rest for you. Instead of typing that previous command out fully, odds are I could have just typed this instead:

cd le[Tab]le[Tab]le[Tab]le[Tab]

Assuming that I have no other subdirectories beginning with the letters le, my Unix system would replace le[Tab] with level1/ the first time I hit the [Tab] key, etc.