Linux FAQ: Can you share some
cd command examples?
cd command stands for "change directory". It is the primary command for moving between directories on a Unix/Linux filesystem.
This first command moves you to the /usr directory. The /usr directory becomes your current working directory:
cd command moves you to the /tmp directory:
You don't have to move just one directory at a time though. You can easily jump from one directory to another directory that's far, far away, like this:
You can also use wildcard characters when moving between directories. This next command works just like the previous command, taking me to the /var/www/html/unix/edu directory on my Linux system:
* wildcard characters can be interpreted as "any number of any character", so the Linux system expands the
/v*/w*/h*/u*/e* that I typed into /var/www/html/unix/edu.
An even better way to cd to that same directory is to use the auto-complete functionality in the Bash shell of all modern Linux systems. Using auto-complete, you can hit the [Tab] key any time you might normally use the asterisk, so I could move to that previous directory using the following keystrokes:
That doesn't look great in text, but trust me, you'll come to love the Bash auto-complete functionality. (As they would say on the tv show Monk, "You'll thank me later.")
On all Unix and Linux systems that I'm aware of, you can always get back to your home directory by typing the
cd command without any arguments, like this:
As far as I know, that works with Bash, the Korn shell, the C shell, etc.
Again, with every Unix and Linux shell I've worked with, you can also always move back to your previous directory by adding a hyphen after the
cd command, like this:
This is a great feature for when you're moving back and forth between two different directories while working on a project.
I hope these Unix and Linux cd command examples have been helpful. If you have any questions or comments, just leave a note in the Comments section below.