How to Use the cd Command on Linux

A graphic of a terminal window on a Linux laptop.Fatmawati Achmad Zaenuri / Shutterstock

Some Linux commands are so familiar that we don’t even notice that we are using them. The cd command to change directory is one of them. There are a few tips that can help you become more efficient with a CD – or you can give it up, absolutely.

An order you rarely think of

You blink all day, every day, but most of the time you don’t know. Unless something gets into your eye, you rarely think of that little, regular movement. Some Linux commands are like that. They hover at the periphery of your consciousness. Even if you use them daily, they don’t grab your attention because they are so small and simple.

In the first hour of using a Linux computer, you learn how to use the cd command included with Bash and other shells. Perhaps you have already used this system on another operating system and did not need explanations? It changes the current working directory, right? What else is there to know?

Well, more than you think. Here are some tips and tricks that could improve your efficiency.

Standard cd operations

To be complete, let’s quickly review the standard uses of cd.

If we are in the home directory, but want to change one located in / usr / lib / firefox / browser, then go back to the home directory, we can use the following commands:

cd / usr / lib / firefox / browser /
cd / home / dave

You do not need to type the full directory path; you can use autocomplete. For each part of a path, after typing enough letters to distinguish the directory name from the others, press Tab to automatically complete the directory name.

For example, type the following on the command line:

cd / usr / lib / fire

Now press Tab and the shell will fill in the rest of the “firefox” directory for you. If you add “/ b” to the path and press Tab again, it adds the “browser” directory to the command.

The shell adds a trailing slash so you can repeat the tabbing process. This is also why there is a trailing slash on the first order. There is not one on the second because it has been typed.

You can use the tilde (~) as a shortcut to quickly return to the home directory from anywhere in the file system; just type the following:

cd ~

These are examples of absolute paths, where you provide the full path from the root of the file system to the target directory, to cd.

Relative paths are referenced from the current working directory. In the personal directory, there is a directory called work. You can use the tree command to see the directory tree in the working directory – just type the following:

tree

The working directory contains a directory called dev. There is also a directory called dev in the root directory of the file system. You can use ls with -d (directory) to look at each of them. The -hl option (human-readable, long list) instructs ls to use easy-to-read units for directory sizes and long format list.

If you type dev, the shell assumes that you mean “dev” in the current directory. To force it to look at the “dev” in the root directory, simply add a forward slash to represent the root of the file system, as shown below:

ls -d dev -hl
ls -d / dev -hl

The cd command behaves like ls in this regard. If you reference the directory as dev, as shown below, this assumes you mean the directory in the working directory:

cd dev

Without a forward slash, longer paths are assumed to start from the current working directory as well, as shown below:

cd dev / mobile / android

RELATED: 15 special characters you need to know for Bash

Change directory with Double Dot

The double dot identifier represents the parent directory of the current working directory. If you are in a deeply nested subdirectory, you can use .. with cd to move to the parent directory of the one you are in.

This takes you up two directories in the directory tree. If you add more .. to the command, this allows you to move an arbitrary number of levels in the directory tree.

Type the following:

cd ..
cd ../ ..

You can also create a set of aliases to perform these maneuvers for you, by typing the following:

alias .2 = “cd ../ ..”
alias .3 = “cd ../../ ..”

You can use them in the same way as the commands themselves.

To make the aliases consistent when you restart your computer, you must add them to your .bashrc or .bash_aliases file.

RELATED: How to Create Aliases and Shell Functions on Linux

Easily jump between two directories

The dash (-) is another symbol that has a special function. It changes your directory to the one you just came from.

For this example, suppose you are in the “c” directory. You can use cd to change to the “fourth” directory. Then you can use cd – to bounce between the two directories.

To do this, you type the following:

cd ../forth

cd –

cd –

The name of the directory you are moving to appears before you move there.

RELATED: How to use pushd and popd on Linux

Another type of parent

The shell uses the current working directory as the “root” or base directory for relative paths. You can use the CDPATH environment variable to set another location as the base directory for relative paths. If you spend most of your time in a certain section of the file system tree, this can save you a lot of keystrokes (and time) each day.

Let’s type the following to make work / dev / projects the base directory for relative paths:

export CDPATH = / home / dave / work / dev / projects

Now, each time you use the dc command, the location in the CDPATH environment variable is checked first for matching directory names. If one of them matches the target you specified in the cd command, you are transferred to this directory.

Now, no matter where you are in the file system, when you use the cd command, the shell checks to see if the target directory is in the home directory. If this is the case, you are moved to this target directory.

If your target directory starts with a leading slash (/), which makes it an absolute path, it will not be affected by the CDPATH environment variable.

To illustrate this, we type the following:

CDC
cd prolog
cd / usr
cd forward

The CDPATH environment variable is really a path, just like the PATH environment variable. When you type a command, the shell looks for a match in the PATH locations. When using CDPATH, the shell searches for locations in the CDPATH environment variable for a match. Likewise, like PATH, CDPATH can contain multiple locations.

RELATED: How to work with variables in Bash

To make the shell find the current directory before other places in the CDPATH environment variable, you simply add a period (.) At the beginning of the path like this:

export CDPATH = .: / home / dave / work / dev / projects

To make your settings permanent, you must add them to a configuration file, such as .bashrc.

One thing to know: if you define a base directory, it also affects directory changes made in scripts. To avoid this, you can use absolute paths in your scripts or a test in your .bashrc file when specifying your CDPATH, as shown below:

if test “$ {PS1 + set}”; then CDPATH = .: / home / dave / work / dev / projects; Fi

This tests to see if the command line prompt variable, $ PS1, has been set. The CDPATH environment variable will only be set if the test is successful.

RELATED: How to add a directory to your $ PATH on Linux

Use shopt with cd

With the shopt command, you can set some options for the shell. Some of them can improve your use of the CD. To set them, you use the -s (enable) option with shopt to give it an option name.

The cdspell option checks the names of your directory and corrects some common typing errors, including transposed or missing characters, or names containing too many characters. If it finds a directory which corresponds to one of the corrections, the corrected path is printed and the cd action takes place.

As an example, we type the following to set the cdspell option and spell “Office” to see if the shell fixes it for us:

shopt -s cdspell
cd Desktpo

The shell detected the error, corrected it and went to the “Desktop” directory.

Another shopt option that you can use with cd is autocd. This saves you from having to type cd at all. Anything you type that is not a command, script, or other executable (such as an alias) is used as the target directory. If you can transfer to this directory, it is printed in the terminal window and you are replaced by this directory.

As an example, we type the following:

shopt -s autocd
/ usr / local / games
/ etc
~

See! You can browse the entire file system without even using a cd!

The parameters you modify with shopt only affect interactive shells, not scripts.

The cd collection

You probably won’t adopt all of this. However, it is likely that you have found something interesting or advantageous here. After all, anything that speeds up or simplifies your command line browsing is all good!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.