未加星标

Linux command line navigation tips and tricks - part 1

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

If you've just started using the command line in linux, then it's worth knowing that it is one of the most powerful and useful features of the OS. The learning curve may or may not be steep depending on how deep you want to dive into the topic. However, there are some Linux command line tips/tricks that'll always be helpful regardless of your level of expertise.

In this article series, we'll be discussing severalsuch tips/tricks, hoping that they'll make your command line experience even more pleasant.

But before we move ahead, it's worth mentioning that all the instructions as well examples presented in this article have been tested on Ubuntu 14.04LTS. The command line shell we've used is bash (version4.3.11)

Linux command line tips/tricks

Please note that we've assumed that you know the basics of the command line in Linux, like what is root and home directory, what are environment variables, how to navigate directories, and more. Also, keep in mind that tips/tricks will be accompanied by the how and why of the concept involved (wherever applicable).

Easily switch between two directories - the quick tip

Suppose you are doing some work on the command line that requires you to switch between two directories multiple times. And these two directories are located incompletely different branches, say, under /home/ and under /usr/, respectively. What would you do?

One, and the most straightforward, option is to switch by typing the complete paths to these directories. While there's no problem with the approach per se, it's very time consuming. Other option could be to open two separate terminals and carry on with your work. But again, neither this approach is convenient, nor it looks elegant.

You'll be glad to know that there exists an easy solution to this problem. All you have to do is to first switch between the two directories manually (by passing their respective paths to the cd command), and then subsequent switches can be accomplishedusing the cd - command.

For example:

I am in the following directory:

$ pwd

/home/himanshu/Downloads

And then I switched to some other directory in the /usr/ branch:

cd /usr/lib/

Now, I can easily switch back and forth using the following command:

cd -

Here's a screenshot showing the cd - command in action.


Linux command line navigation tips and tricks - part 1

An important point worth mentioning here is that if you make a switch to a third directory in between all this, then the cd - command will work for the new directory and the directory from which the switch was made.

Easily switch between two directories - related details

For the curiousbunch, who want to know how the cd - command works, here's the explanation: As we all know, the cd command requires a path as its argument. Now, when a hyphen (-) is passed as an argument to the command, it's replaced by the value that OLDPWD environment variable contains at that moment.


Linux command line navigation tips and tricks - part 1

As it would be clear by now, the OLDPWD environment variable stores the path of previous working directory. This explanation is there is the man page of the cd command, but sadly, it's likely that you'll not find the man page pre-installed on your system (it's not there on Ubuntu at least).

However, installing it is not a big deal, all you have to do is to run the following command:

sudo apt-get install manpages-posix

And then do:

man cd

Once the man page opens, you'll see that it clearly says:

- When a hyphen is used as the operand, this shall be equivalent to the command: cd "$OLDPWD" && pwd

Needless to say, it's the cd command that sets the OLDPWD variable. So every time you change the directory, the previous working directory gets stored in this variable. This brings us to another important point here: whenever a new shell instance is launched (both manually or through a script), it does not have a 'previous working directory.'


Linux command line navigation tips and tricks - part 1

That's logical because it's the cd command which sets this variable. So until you run the cd command at-least once, the OLDPWD environment variable will not contain any value.

Moving on, while it may seem counterintuitive, the cd - and cd $OLDWPD commands do not produce same results in all situations. Case in point, when a new shell has just been launched.


Linux command line navigation tips and tricks - part 1

As clear from the screenshot above, while the cd - command complained about the OLDPWD variable not being set, the cd $OLDPWD command did not produce any error; in fact it changed the present working directory to the user's home directory.

That's because given that the OLDPWD variable is currently not set, $OLDPWD is nothing but an empty string. So, the cd $OLDPWD command is as good as just running cd , which - by default - takes you to your home directory.

Finally, I've also been through situations where-in it's desirableto suppressthe output the cd - command produces. What I mean is, there can be cases (for example, while writing a shell script), where-in you'll want the cd - command to not produce the usual directory path in output. For those situations, you can use the command in the following way:

cd - &>/dev/null

The above command will redirect both file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null . This means that any error the command produces will also be suppressed. However, you'll still be able to check the success of failure of the command using the generic $? technique - the command echo $? will produce '1' if there was some error, and '0' otherwise.

Alternatively, if you're ok with the cd - command producing an output in error cases, then you can use the following command instead:

cd - > /dev/null

This command will only redirect the file descriptor 1 (STDOUT) to /dev/null .

Conclusion

Sadly, wecould only cover one command line-related tip here, but the good thing is that we managed to discuss a lot of in-depth stuff about the cd - command. You are advised to go through the tutorial thoroughlyand test everything - that we've discussed here - on your Linux box's command line terminal. Also, do go through the command's man page, and try out all the featuresdocumented there.

In case you face any issue or have some doubt, do share with us in comments below. Meanwhile, wait for the second part, where-in we'lldiscuss some more useful command line-related tips/tricks in the same manner as has beendone here.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: LinuxUbuntuUT
分页:12
转载请注明
本文标题:Linux command line navigation tips and tricks - part 1
本站链接:http://www.codesec.net/view/523024.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(116)