Some Linux commands are so familiar that we don't even realize we're using them. the cd
The command to change directory is one of these. There are a few tricks that can help you be more efficient with cd
Or you can get rid of it completely.
A command you rarely think about
Blink all day, every day, but most of the time you don't realize. Unless something gets into your eye, you rarely think about that regular little movement. Some Linux commands are like this. Hovering over the periphery of your consciousness. Even when you use them daily, they do not attract your attention because they are very small and simple.
Within the first hour of using a Linux computer, you will learn to use the cd
command included with Bash and other shells. Maybe you had previous experience using it on another operating system and didn't need an explanation. Change current working directory, truth? What else is there to know?
Well, more than you think. Next, some tips and suggestions are included that can improve your efficiency.
Standard cd operations
For the sake of being complete, Let's quickly go over the standard uses of cd
.
If we are in the home directory, but we want to change to one located at /usr/lib/firefox/browser
, and later return to the home directory, we can use the following commands:
cd /usr/lib/firefox/browser/
cd /home/dave
You do not need to enter the full directory path; you can use autocomplete. For each part of a route, after typing enough letters to distinguish the directory name from others, press Tab to auto fill the directory name.
As an example, type the following in the command line:
cd /usr/lib/fire
Now, press tab and the shell will populate the rest of the directory “now.” for you. If you add “/ b” to the path and press Tab again, adds the directory “browser” to the command.
The shell adds a trailing slash so you can repeat the tab completion procedure. It is also because of this that there is a forward slash in the first command. There is not one in the second because that was written.
You can use the tilde (~
) as a shorthand way 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 filesystem to the destination directory, for cd
.
Relative paths are referenced from current working directory. In the home directory, there is a directory called work
. You can use the tree
command to view directory tree within work
directory: just type the following:
tree
the work
directory contains a directory called dev
. In addition there is a directory called dev
in the root directory of the file system. You can use ls
with -d
(directory) to see each of these. the -hl
The option (human readable, long list) indicates ls
to use easy-to-read units for directory sizes and long-form list.
If you write dev
, the shell assumes that you mean the “dev” in current directory. To force you to look at the “dev” in the root directory, just add a forward slash to represent the root of the filesystem, as it's shown in the following:
ls -d dev -hl
ls -d / dev -hl
the cd
the command behaves like ls
In this regard. If you refer to the directory as dev
, as it's shown in the following, assumes it refers to the directory in the work
directory:
cd dev
Without a slash, longer paths are also supposed to start from current working directory, as it's shown in the following:
cd dev/mobile/android
RELATED: 15 special characters you must know for Bash
Change directory with Double Dot
The double dot identifier represents the parent directory you are running from at the moment. If you are in a deeply nested subdirectory, you can use ..
with cd
to move to the parent directory you are in.
This moves it up two directories in the directory tree. If you add more ..
in command, enables you to move an arbitrary number of levels up in the directory tree.
Write the following:
cd ..
cd ../..
You can also create a set of aliases to perform these maneuvers for you., writing the following:
alias .2="cd ../.."
alias .3="cd ../../.."
You can use them in the same way as the commands themselves.
To keep aliases consistent across reboots of your computer, you must add them to your .bashrc
O .bash_aliases
proceedings.
RELATED: How to create aliases and shell functions in Linux
Easily jump between two directories
The script (-
) is another symbol that has a special function. Change your directory back to the one you just arrived from.
For this example, let's say it's in the directory “c”. You can use cd
to switch to the directory “room”. Then, You can use cd -
to bounce between the two directories.
To do this, write the following:
cd ../forth
CD –
CD –
The name of the directory you are moving to appears before moving to it.
RELATED: How to use pushd and popd in Linux
Another kind of relative
the shell uses the current working directory as the directory “root” or basis for relative paths. You can use the CDPATH
environment variable to determine another location as base directory for relative paths. If you spend most of your time in a certain section of the filesystem tree, this can save you a lot of keystrokes (and time) every day.
Let's write the following to do work/dev/projects
the base directory for relative paths:
export CDPATH=/home/dave/work/dev/projects
Now, every time I use the dc
command, the location in the CDPATH
Environment variable is checked first for matching directory names. If any of them match the purpose you provided in the cd
command, you are transferred to that directory.
Now, regardless of where you are in the file system, when you use the cd
command, the shell checks if the target directory is in the base directory. If so, will be moved to that destination directory.
If your destination directory starts with a forward slash (/
), which makes it an absolute route, will not be affected by the CDPATH
Environmental variable.
To prove this, we write the following:
cd c
cd prolog
cd /usr
cd forth
the CDPATH
The environment variable is truly a path, in the same way as the PATH
Environmental variable. When you type a command, the shell looks for the locations in the PATH
for a match. When you use CDPATH
, the shell looks for the locations in the CDPATH
environment variable for a match. At the same time, the same as PATH
, CDPATH
can contain multiple locations.
RELATED: How to work with variables in Bash
For the shell to find the current directory before other locations in the CDPATH
environment variable, just add a point ( .
) at the beginning of the route like this:
export CDPATH=.:/home/dave/work/dev/projects
To make your settings permanent, you must add them to a config file, What .bashrc
.
One thing to pay attention: if you set a base directory, it will also affect the directory changes made within the scripts. To avoid this, you can use absolute paths in your scripts or a test in your .bashrc
file when you specify your CDPATH
, As shown below:
if test "${PS1+set}"; then CDPATH=.:/home/dave/work/dev/projects; fi
This performs a test to see if the command line variable, $PS1
, it was established. the CDPATH
The environment variable will only be set if the test succeeds.
RELATED: How to add a directory to your $ PATH on Linux
Using shopt with cd
With the shopt
command, you can set certain options for the shell. Some of these may improve your use of cd
. To configure them, use the -s
(enable) option with shopt
to pass it an option name.
the cdspell
The option checks your directory names and fixes some common typos, including transposed or missing characters, or names with too many characters. If you find a directory that matches any of the fixes, the corrected path is printed and cd
action is carried out.
As an example, we write the following to configure the cdspell
and writes poorly “Desk” to see if the shell corrects it for us:
shopt -s cdspell
cd Desktpo
The shell encountered the error, corrected it and changed to the directory “Desk”.
Other shopt
option you can use with cd
it is autocd
. Eliminate the need to type cd
absolutely. Anything you type that is not a command, script or other executable (as an alias), used as destination directory. If you can transfer to that directory, it is printed in the terminal window and it is changed to that directory.
As an example, we write the following:
shopt -s autocd
/usr/local/games
/etc
~
Watch! You can jump through the entire file system without even using cd
!
The settings that change with shopt
they only impact interactive shells, no a los scripts.
The cd collection
You probably won't adopt all of these. Despite this, you have probably found something of interest or benefit here. After all, Anything that speeds up or simplifies command line navigation is fine!!