10 command actions Linux administrator saves time

Source: Internet
Author: User

Although Linux desktop has undergone major changes over the past two decades, the most significant change may be the Ubuntu Unity interface.) based on the strength that can be provided to experienced system administrators, the command line is still non-parallel. Although most of the next 10 tips are about Bash shell, all these skills can be easily applied to other current shells.

1. Use a command to create and enter the Directory

Creating and entering a new directory is a very common task. It seems that there are shortcuts to executing both commands in shell. Although there are no shortcuts, you can add the following functions to your. bashrc file:

Mkcd ()
{
Mkdir $1
Cd $1
}

Then run the source. bashrc command to read the changes in the memory and run the mkcd command to complete the two services:

Wjgilmore @ ubuntu :~ $ Mkcd articles
Wjgilmore @ ubuntu :~ /Articles $

2. Return to the previous directory.

When you need to transfer from a complex directory and want to return to the initial directory, you can pass the previous path to the cd command, but the little-known cd Command makes this thing trivial. This sequence shows the behavior:

Wjgilmore @ ubuntu-laptop :~ /Documents/techtarget_articles/ten_command_line_tricks/test2 $ cd
Wjgilmore @ ubuntu-laptop :~ $ Cd-
~ /Documents/techtarget_articles/ten_command_line_tricks/test2 $
Wjgilmore @ ubuntu-laptop :~ /Documents/techtarget_articles/ten_command_line_tricks/test2 $

3. create directory bookmarks

Continue to follow the topic of directory interaction, and you will inevitably return to some directories again and again. You can add the path to the $ CDPATH shell variant in your. bashrc file) to create bookmarks that allow you to quickly navigate to these directories:

CDPATH = '.:/home/wjgilmore/books'

Once added, you can directly navigate to the bookmarks directory from any location in the operating system path. You only need to execute the following command:

$ Cd books

4. proficient in editing command lines

How many times have you quietly edited and executed a series of slightly different commands? For example, when I am busy creating PDF versions of different book chapters from the Markdown source, I usually execute the following command:

$ Pandoc-o html/chapter06.html chapters/chapter06.md -- template = templates/html. template

In order to create the chapter04.md source file, the command line beginners will soon get tired of using the up arrow to search in history before executing the previous command), and then use the left arrow until all the chapter06.md instances are replaced with chapter04.md. There are many more efficient ways to execute this task. First, consider using the Bash command line to edit the keyboard shortcuts in two modes: Emacs and vi), which allows you to quickly navigate to the desired location:

Ctrl + a: Go to the first line
Ctrl + e: Go to the last line
Alt + f: one word forward
Alt + B: Move a word back

The second and more effective method is to use command line replacement. The following command will replace 06 found in the previous command with 04:

$ Pandoc-o html/chapter06.html chapters/chapter06.md -- template = templates/html. template
$ !! : Gs/06/04
Pandoc-o html/chapter04.html chapters/chapter04.md -- template = templates/html. template

If you are accidentally using the GNOME terminal, the meta-Alt key does not work as described, because the gno me terminal has been bound with the Alt key to the toolbar command. You can also use Shift + Alt as the meta key, but this is a bit clumsy. On the contrary, if you do not need toolbar command shortcuts, you can choose Edit> keyboard shortcut to close them... You can also disable the Enable menu access key option.

5. Save the long command for later use

When using a list of System Administrator operations, you can enter a specific long command and realize that one step in the sequence is omitted before executing it. You can save the command to history, instead of adding the #) to the beginning of the command:

$ # This is some ridiculously long command that I want to save

Press Enter and use the up arrow to display the saved command. To execute this command, you only need to remove the well number key from the beginning of the line before execution.

6. Save the input using the command alias

The long list format of the ls command is ls-l) and can be used frequently, but the hyphen makes it a little impractical for a large number of input. You can use the alias command in. bashrc to create a command alias for long commands. In this example, the command alias dir is a replacement of ls-l.

Alias dir = 'LS-l'

7. Save More input by ignoring input errors

You are in the terminal area, where copying, updating, and removing files from a directory is triggered to the next one. Or you are not in the terminal area, because your fingers are faster than the brain, or even the keyboard response time can be processed, this leads you to constantly return and correct your input errors. In your. bashrc file, shell automatically fixes all input errors when identifying the file or path name.

Shopt-s cdspell

 8. Open the application in the background

When inspecting command lines, you may need to perform another task, such as responding to an email. Of course, you can open the GUI application from the terminal by executing other commands. You only need to call their names. In this example, open Gimp:

$ Gimp

But this effectively ends your terminal session, because the application will open at the front end. If you are opening a specific program from the command line, consider modifying its default call in your. bashrc file:

Gimp ()
{
Command gimp "$ @"&
}

To reload your. bashrc file, see the source command), you will be able to call the Gimp application and pass it together with the name of any image file you want to open, as well as retain additional benefits of terminal control.

9. replace less with more

More commands are useful for quick reading of text files. Once the file is loaded to the page, you can use/to search for the file. The problem is that once you find the desired row, you can navigate up and check that the content that appears higher than the row is not possible. Fewer commands are not inferior. They allow you to scroll up and down the text. The more commands you call in the same way:

$ Less sometextfile.txt

10. Clear your command line history

History command) is easily one of the most powerful tools you can use. However, a time-saving command is particularly worth mentioning: $ HISTIGNORE shell variable.

Your history list will become very long over time. Use the $ HISTIGNORE variable to excrete records that you think are irrelevant:

$ Export $ HISTIGNORE = "&: cd: exit: ls"

This will cause all copied commands, cd, exit, and ls commands to be omitted from historical commands.

Speed is the key to managing command lines, and these 10 tips should help you start your command line management.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.