The CD command is probably one of the top 10 commands any Linux user learns, but it’s not the only way to navigate a Linux file system, and there are other ways.

Whether you’re looking around your file system, looking for files, or trying to get into an important directory, Linux has a lot to offer. In this article, we’ll show you some tricks to make it easier to move around the file system and find and use the commands you need.

Added to the $PATH

One of the simplest and most useful ways to ensure that you don’t have to spend a lot of time looking up commands on your Linux system is to add the appropriate directory to the $PATH variable. However, the order in which directories are added to the $PATH variable is important. They determine the order in which the system looks for commands to run in directories — stopping when the first match is found.

For example, you might want to put your home directory first, so that if you create a script with the same name as another executable, just type the name of the script and it will run.

To add a home directory to the $PATH variable, do the following:

$ export PATH=~:$PATH
Copy the code

The ~ character represents the home directory.

If you save the script in the bin directory, the following will work:

$ export PATH=~/bin:$PATH
Copy the code

You can then run the script in your home directory, as follows:

$ myscript
Good morning, you just ran /home/myacct/bin/myscript
Copy the code

** Important note: ** The commands shown above will be added to your search PATH because $PATH (the current PATH) is included. They don’t cover it. Your search path should be configured in your.bashrc file, and any changes you intend to make permanent should be added there as well.

Using symbolic links

Symbolic links provide a simple and obvious way to record the location of directories that you may need to use frequently. For example, if you manage the content of your web site, you might want to “remember” the location of the web page file for your account by creating links like this:

ln -s /var/www/html www
Copy the code

The order of arguments is important. The first (/var/ WWW/HTML) is the target, and the second is the name of the link you created. If you are not currently in your home directory, the following command will do the same:

ln -s /var/www/html ~/www
Copy the code

Once set up, you can use CD WWW to access /var/www/html.

Using shopt

The shopt command also provides a way to make moving to other directories easier. When you use Shopt’s AutoCD option, just type the name to go to the directory. Such as:

$ shopt -s autocd
$ www
cd -- www
/home/myacct/www
$ pwd -P
/var/www/html

$ ~/bin
cd -- /home/myacct/bin
$ pwd
/home/myacct/bin
Copy the code

In the first set of commands above, the autoCD option of the shopt command is enabled. Typing WWW invokes the CD WWW command. Since this symlink was created in one of the ln command examples above, let’s move on to /var/www/html. The PWD -p command displays the actual location.

In the second group, typing ~/bin calls CD to the bin directory in the user’s home directory.

Note that when you type a command, the autocd behavior will not take effect, even if it is also the name of the directory.

Shopt is the bash built-in command, and it has many options. It just means you don’t have to type CD before entering the name of each directory.

To see other options for Shopt, simply type shopt.

Using the $CDPATH

Probably one of the most useful techniques for accessing a specific directory is to add the path you want to easily access to $CDPATH. This creates a list of directories to enter by entering only part of the full pathname.

On the one hand, this can be tricky. Your $CDPATH needs to contain the parent directory of the directory you are moving to, not the directory itself.

For example, suppose you want to move to the /var/www/html directory simply by typing CD HTML, and to subdirectories in /var/log using only CD and a simple directory name. In this case, the $CDPATH will work:

$ CDPATH=.:/var/log:/var/www
Copy the code

You will see:

$ cd journal
/var/log/journal
$ cd html
/var/www/html
Copy the code

$CDPATH works when you enter an incomplete path. It looks down at its directory list to see if the specified directory exists in one of them. Once you find a match, it will take you there.

Keeping. At the beginning of $CDPATH means you can access local directories without having to define them in $CDPATH.

$ export CDPATH=". :$CDPATH"
$ Videos
cd -- Videos
/home/myacct/Videos
Copy the code

It’s not hard to switch keys in a Linux file system, but you can save some brain cells if you use a few handy tricks to get to places easily.


Via: www.networkworld.com/article/353…

Author: Sandra henry-stocker lujun9972

This article is originally compiled by LCTT and released in Linux China