How to use the cd command on Linux

Some Linux commands are so familiar to us that we don’t even realize we’re using them. the cd Command to change directories is one of them. There are a few tricks that can be used to become more efficient cd– or you can give it up altogether.

An order you seldom think of

You blink all day, every day, but most of the time you are unaware of it. Unless something gets in your eye, you seldom think of this small, regular exercise. Some Linux commands are like that. They float on the periphery of your consciousness. Even though you use them on a daily basis, you won’t notice them because they are so small and simple.

You will learn how to do this within the first hour of using a Linux computer cd Command included in bash and other shells. Perhaps you already had experience using it on a different operating system and did not need an explanation. It changes the current working directory, doesn’t it? What else is there to know?

Well, more than you think. Here are a few pointers and tips that can improve your efficiency.

The standard CD operations

For the sake of completeness, let’s briefly review the standard uses of. by cd.


When we are in the home directory but want to switch to one under /usr/lib/firefox/browser, and then returning to the home directory, we can use the following commands:

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

You don’t need to enter the entire directory path; You can use autocomplete. For each part of a path, after entering enough letters to distinguish the directory name from the others, press Tab to auto-complete the directory name.

To the example, enter the following on the command line:

cd /usr/lib/fire

Now press the tab key and the shell fills the rest of the “firefox”Directory for you. Adding “/ b” to the path and pressing Tab again adds the “browser” directory to the command.

The shell adds a trailing slash so you can repeat the tab completion process. This is why there is also a slash after the first command. There is none on the second because it was typed.

You can use the tilde (~) as a short form to quickly return to the home directory from anywhere in the file system; Just enter the following:

cd ~

These are examples of absolute paths in which you specify the entire path from the root of the file system to the destination directory in order to cd.


Relative paths are referenced from the current working directory. In the home directory there is a directory called work . You can use the … tree Command to see directory tree within the work Directory – just enter:

tree

the work 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 view them. the -hl (human readable, long listing) option says ls to use easily readable units for directory sizes and long format listing.

When you type dev, the shell assumes that you mean the “dev” in the current directory. To force the “dev” to appear in the root directory, just add a leading slash to represent the root of the filesystem, as shown below:

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

the cd Command behaves like ls so far. If you refer to the directory as devas shown below, it assumes you have the directory in the work Directory:

cd dev

Without a leading slash, it is assumed that longer paths also start from the current working directory, as shown below:

cd dev/mobile/android

15 special characters you need to know in Bash

Change the directory with double dot

The colon identifier represents the parent directory of the current working directory. If you are in a deeply nested subdirectory, you can .. with cd to change to the higher-level directory of the directory in which you are located.


This will move you two directories up in the directory tree. If you add more .. the command allows you to move any number of levels up in the directory tree.

Enter the following:

cd ..
cd ../..

You can also create a number of aliases to perform these maneuvers for you by typing:

alias .2="cd ../.."
alias .3="cd ../../.."

You can use these like the commands themselves.

To make the aliases consistent when your computer restarts, you need to add them to your .bashrc or .bash_aliases File.

How to create aliases and shell functions on Linux

Simply switch between two directories

The hyphen (-) is another symbol with a special function. It will change your directory back to the one you just came from.


Therefore example, let’s assume you are in directory “c”. You can use cd to change to the “fourth” directory. Then you can use cd - Jump back and forth between the two directories.

To do this, enter the following:

cd ../her

CD –

CD –

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

How to use pushd and popd on Linux

Another kind of relative

The shell uses the current working directory as the “root” or base directory for relative paths. You can use the … CDPATH Environment variable to specify a different storage location as the base directory for relative paths. Spending most of your time in a specific section of the filesystem tree can save you a lot of keystrokes (and time) each day.

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

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

Now every time you do that dc Command, the place in the CDPATH The environment variable is first checked for matching directory names. If any of them match the goal you specified in the cd Command, you will be transferred to this directory.

Now, regardless of where you are in the filesystem, if you have the cd Command, the shell checks whether the target directory is in the home directory. If so, they will be moved to this target directory.


If your target directory has a leading slash (/), which makes it an absolute path, it is not made by the CDPATH Environment variable.

To demonstrate this, let’s enter:

cd c
cd prolog
cd /usr
cd forth

the CDPATH Environment variable is really a path, just like that PATH Environment variable. When you enter a command, the shell searches the locations in the PATH for a game. If you use CDPATH, the shell searches the locations in the CDPATH Environment variable for a match. Also the same as PATH, CDPATH can contain multiple locations.

How to work with variables in Bash

So that the shell uses the current directory before other locations in the CDPATH Environment variable, just add a period ( . ) at the beginning of the path as follows:

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

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

Note the following: If you specify a base directory, this also affects directory changes within scripts. To avoid this, you can use absolute paths in your scripts or a test in your script .bashrc File if you provide yours CDPATH, As shown below:

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

This will do a test to see if the command line prompt variable, $PS1 , was set. the CDPATH Environment variable is only set if the test is successful.

Here’s how to add a directory to your $ PATH on Linux

Use Shopt with CD

With the shopt command, you can set certain options for the shell. Some of these can improve your use of cd. To set them, use the -s (activate) option with shopt to pass it an option name.


the cdspell Option checks your directory names and corrects some common typos, including mixed up or missing characters, or names with too many characters. If a directory is found that matches one of the corrections, the corrected path is printed and the cd Action takes place.

as example, let’s enter the following to get the cdspell Option and misspell “Desktop” to see if the shell corrects it for us:

shopt -s cdspell
cd Desktpo

The shell recognized the error, corrected it and switched to the “Desktop” directory.

Other shopt Option you can use with cd is autocd. It eliminates the need for typing cd at all. Anything you enter that is not a command, script, or other executable file (such as an alias) will be used as the destination directory. If you can transfer to this directory, it will be printed in the terminal window and you will be changed to this directory.

as example, let’s enter:

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

See! You can hop across the entire filesystem without using it cd!


The settings you change with shopt only affect interactive shells, not scripts.

The CD collection

All of this is unlikely to be done by you. However, it is likely that you have found something interesting or beneficial here. After all, whatever makes your command line navigation faster or easier is good!

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts