Home > System Tutorial > LINUX > Tips on using command line tools in Linux systems (1)

Tips on using command line tools in Linux systems (1)

PHPz
Release: 2024-02-09 19:30:16
forward
686 people have browsed it

If you are just starting to use command line tools in Linux systems, then you should know that it is one of the most powerful and useful tools in the Linux operating system. The difficulty of learning depends on how in-depth you want to delve. However, no matter your skill level, some tips and tricks in this article will help you.

In this series of articles, we will discuss some very practical tips for using command line tools, and hope that your command line experience will be more pleasant.

However, before starting the next step, I must emphasize that the test examples in this article were all tested under the Ubuntu 14.04LTS system. The command line shell version we are using is bash 4.3.11.

Tips on using command line tools in Linux systems (1)

Some tips on using Linux command line tools

We assume that you have mastered some basic knowledge of the Linux command line, such as what is the root account and home directory, what are environment variables, how to view directory contents, etc. These tips are explained while also introducing the concepts involved, if any.

Easily switch directories - shortcut

Suppose you are doing some operations on the command line, and you need to frequently switch back and forth between the two directories. And these two directories are in two completely different paths, for example, under /home/ and /usr/ respectively. What would you do?

Among them, the simplest and most direct way is to enter the full path of these directories. Although there is nothing wrong with this method in itself, it is a waste of time. Another way is to open two terminal windows and operate them separately. However, these two methods are neither convenient to use nor technical.

Fortunately for you, there is another, simpler way to solve this problem. All you need to do is to manually switch to these two directories first (add their respective paths through the cd command), and then you can use the cd - command to quickly switch back and forth between the two directories.

For example:

I am currently in the following directory:

]$ pwd
/home/himanshu/Downloads
Copy after login

Then, I switch to other directories under the /usr/ path:

cd /usr/lib/
Copy after login

Now, I can easily use the following commands to quickly switch to two directories forward and backward:

cd -
Copy after login

The following is a screenshot of the operation of the cd – command:

Tips on using command line tools in Linux systems (1)

One thing I have to emphasize to you is that if you use cd plus path to switch to the third directory during the operation, then the cd – command will be applied to the current directory and the third directory. switch between.

Easily switch directories - related details

For those users who are very curious and want to understand how cd - works, the explanation is as follows: As everyone knows, the cd command requires a path as its parameter. Now, when the – symbol is passed as an argument to the cd command, it will be replaced by the value of the OLDPWD environment variable.

Tips on using command line tools in Linux systems (1)

You should understand now that the OLDPWD environment variable stores the path to the previous operating directory. This explanation is explained in the man help document of the cd command. However, unfortunately, the man command help tool may not be pre-installed in your system (at least not under the Ubuntu system).

However, installing this man help tool is also very simple. You only need to execute the following installation command:

sudo apt-get install manpages-posix
Copy after login

Then do the following:

man cd
Copy after login

After opening the man help document home page, you will see the following clear explanation:
——When the - symbol is used as a parameter value of the cd command, it will be equivalent to the following operation command:

 cd "$OLDPWD" && pwd
Copy after login

There is no doubt that the cd command sets the OLDPWD environment variable value. So every time you switch the operating directory, the path to the previous directory will be saved to this variable. This also makes us see a very important point: any time you start a new shell instance (including manual execution or using a shell script), there is no 'previous working directory'.

Tips on using command line tools in Linux systems (1)

This is also logical, because the cd command sets the OLDPWD environment variable value. Therefore, the OLDPWD environment variable will not contain any value unless you execute the cd command at least once.

Moving on, although this is a little hard to understand, the results of the cd - and cd $OLDWPD commands are not the same in all environments. For example, when you reopen a new shell window.

Tips on using command line tools in Linux systems (1)

It can be clearly seen from the above screenshot that when the cd - command prompt is not set with the OLDPWD value, the cd $OLDPWD command does not report any error; in fact, it changes the current working directory to the user's in the home directory.

那是因为 OLDPWD 变量目前还没有被设置, $OLDPWD 仅仅是一个空字符串。因此, cd $OLDPWD 命令跟 cd 命令的执行结果是一致的,默认情况下,会把用户当前的工作目录切换到用户的 home 目录里。

最后,我还遇到过这样的要求,需要让 cd – 命令执行的结果不显示出来。我的意思是,有这样的情况(比如说,在写 shell 脚本的时候),你想让 cd – 命令的执行结果不要把目录信息显示出来。那种情况下,你就可以使用下面的命令方式了:

cd - &>/dev/null
Copy after login

上面的命令把文件描述符 2(标准输入)和 1(标准输出)的结果重定向到 /dev/null 目录。这意味着,这个命令产生的所有的错误不会显示出来。但是,你也可以使用通用的 $? 方式来检查这个命令的执行是否异常。如果这个命令执行报错, echo $? 将会返回 1,否则返回 0。

或者说,如果你觉得 cd – 命令出错时输出信息没有关系,你也可以使用下面的命令来代替:

cd - > /dev/null
Copy after login

这个命令仅用于将文件描述符 1 (标准输出)重定向到 /dev/null 。

总结

遗憾的是,这篇文章仅包含了一个跟命令行相关的小技巧,但是,我们已经地对 cd – 命令的使用进行了深入地探讨。建议你在自己的 Linux 系统的命令行终端中测试本文中的实例。此外,也强烈建议你查看 man 帮助文档,然后对 cd 命令进行全面测试。
如果你对这篇文章有什么疑问,请在下面的评论区跟大家交流。同时,敬请关注下一篇文章,我们将以同样的方式探讨更多有用的命令行使用技巧。

The above is the detailed content of Tips on using command line tools in Linux systems (1). For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template