Linux advanced command line skills

Source: Internet
Author: User
Tags touch command

Linux Command Line has powerful functions. For skilled Linux system administrators, they only need to combine various techniques to form a complex command to complete user tasks.

How can a complicated Linux Command be made up? The following describes several tips.

  Redirect using input/output

In Linux Command Line mode, if the output required by the command is not from the keyboard, but from the specified file, this is the input redirection. Similarly, the command output can be written to a specified file instead of displayed on the screen. This is the output redirection.

Next let's take a look at the practical application of standard input/output redirection in constructing a Linux Command.

  1. input redirection and output redirection

# Wc home.txt

The first command is to input the aa.txt file as the WC command, and calculate the number of lines, words, and characters of aa.txt. The second command saves the lscommand output to a file named home.txt. If the file behind the symbol already exists, the file will be overwritten.

  2. Use both input and output redirection

# Iconv-f gb18030-t UTF-8 new-aa.txt

This command uses the same input direction (new-aa.txt ). Aa.txt is a gb18030 file, while new-aa.txtis a new file that converts the content of aa.txt into UTF-8 format.

  Pipeline

The pipeline operator "|" provided by Linux is used to separate the two commands. The output of the command on the left of the pipeline operator is used as the input of the command on the right of the pipeline operator. The continuous use of pipelines means that the output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the Third Command, and so on. Next let's take a look at how the pipeline gets an application in constructing a Linux Command.

  1. Use a pipe

# Rpm-qa | grep licq

This command creates an MPS queue with the pipe character "|. The MPs queue uses rpm-qa command output (including all installed RPM packages in the system) as the grep command input to list RPM packages with licq characters.

  2. Use multiple pipelines

# Cat/etc/passwd | grep/bin/bash | wc-l

This command uses two pipelines and sends the cat command (display the content of the passwd file) to the grep command using the first pipeline, the grep Command finds all rows containing "/bin/bash". The second pipe sends the grep input to the wc command, and the wc command counts the number of rows in the input. The function of this command is to find out how many users in the system use bash.

  Use commands to replace

In Linux Command Line Mode, when a pair of "'" (top delimiter) is encountered, the commands contained in "'" will be executed first, then, the output result is substituted into the command line as a parameter, which is replaced by the command. It is similar to the redirection function of input and output, but the difference is that the command is replaced with the output of one command as the parameter of another command. Next we will look at its practical application.

  1. Use commands to replace

# Touch 'date when policyymm1_d1_k1_m1_s'.txt

This command is replaced with a command. The date + % Y % m % d % k % M % S command will be executed first, and it will output the current time in the specified format. Then, the time will be used as a parameter of the touch command. The result is that a file with the current time as the file name is created.

  2. Use commands to replace

# Kill '/sbin/pidof smbd'

This command will kill all sshd processes. Here, the pidof command is used to give the process number, because kill operates on the process number. Both of them are replaced by commands, implementing the function of killing all sshd processes with only one command.

  Comprehensive Application

After understanding and familiarizing yourself with the previous skills, it is a high skill to apply them comprehensively. At the same time, some commonly used and complex Linux commands must be mastered. Some basic and important commands frequently used in constructing Linux Commands include grep, tr, sed, awk, find, cat, and echo. Here are some examples of comprehensive applications.

1.

# Man ls | col-B> ls.man.txt

This command uses both output redirection and pipelines to convert ls help information into a text file that can be directly read.

2.

# Kill-9 'ps-ef | grep smbd | tr-S' | awk-f'' {print $2 }''

The functions in this example are the same as those above, but it does not rely on pidof commands. It uses commands in most Unix systems and is suitable for more system environments. But it looks more complicated. It uses three pipelines and one command replacement, and grep, tr, and awk commands related to character operations.

From the previous introduction, we can see that through the combination of several techniques, Linux commands can complete complex functions. In addition, you can organize these commands into a script, add functions such as functions, variables, judgments, and loops, and add some programming ideas, which is a Shell script with more powerful functions.

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.