Showing posts with label cd Command. Show all posts
Showing posts with label cd Command. Show all posts

Tuesday, 28 July 2020

The Linux cd command

Linux cd Command, Linux Tutorial and Material, Linux Exam Prep, Linux Study Material

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.

Thursday, 28 May 2020

Linux cd command examples

LPI Study Materials, LPI Guides, LPI Tutorial and Material, LPI Certification


Linux FAQ: Can you share some cd command examples?


The Linux cd command stands for "change directory". It is the primary command for moving between directories on a Unix/Linux filesystem.

Examples of the cd command


This first command moves you to the /usr directory. The /usr directory becomes your current working directory:

cd /usr

Similarly, this cd command moves you to the /tmp directory:

cd /tmp

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:

cd /var/www/html/unix/edu

Linux cd command: Using wildcards


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:

cd /v*/w*/h*/u*/e*

The * 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.

Linux cd command and auto-complete


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:

cd /v[Tab]/w[Tab]/h[Tab]/u[Tab]/e[Tab]

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.")

The cd command: Going home


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:

cd

As far as I know, that works with Bash, the Korn shell, the C shell, etc.

cd command: Back to the previous directory


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:

cd -

This is a great feature for when you're moving back and forth between two different directories while working on a project.