Shell Common Commands Summary

Source: Internet
Author: User
Tags parent directory readable disk usage

Now, based on the development of Linux, Internet products are basically based on Linux,android,mac, Linux everywhere, so, as a time-tested, not familiar with Linux is not good, record some of the common shell commands in the test.

1, LS: Print out the list of the current directory

Files on Ls–a:linux are treated as hidden files by the system, and only the LS command is not visible to them, and the hidden files are displayed with Ls-a in addition to the generic filenames.

Ls–l: This parameter displays more detailed file information.

2. CD: Switch the current directory

CD/: Back to the root directory

Cd.. : Returns to the top level directory

CD-: Return to the most recently used directory

3. PWD: Used to display the user's current working directory

4, mkdir and RmDir

Midir: Creating a Directory

RMDIR: Deleting a directory

Two commands support the-p parameter, and for the mkdir command, if the parent directory of the specified path does not exist, it is created, for the rmdir command to delete all hierarchical directories of the specified path, if there is content in the folder, you cannot use the rmdir command

Mkdir-p/home/test/test1

Rmdir-p/home/test/test1

5. CP: Copy command

CP 1.txt.. /test1: Copy a file to another directory

CP 1.txt 2.txt: Copy a file to this directory and rename it

Cp-r test1 test2: Copy a folder A and rename it to B

6. MV: Move command

MV 1.txt.. /test1: Move one file to another directory

MV 1.txt 2.txt: Renaming a file in this directory

MV 1.txt.. /test1/2.txt: Move a file to another directory and rename it

7. RM: Delete Files

–i: The system will ask for confirmation before deleting the file, and the file will be deleted after the user returns. Files deleted under Linux are hard to recover (they are said to be recovered with tools, but I have not recovered successfully), so be sure to check them carefully before deleting them.

–R: This parameter supports directory deletion, features and RmDir commands are similar

–f: Instead of the-I parameter,-f means force delete

RM-RF./*: Delete all files in the current directory

8 , du, df:du command can display the disk space occupied by the current directory, the DF command can show the current disk space remaining

If the du command does not add any parameters, then the entire disk usage is returned, if the directory is added later, it is the directory on disk usage

Du-sh dir: View the total size of the specified directory

Du-sh./*: View the size of all folders and files in the current directory

Both commands support the-K,-M and-h parameters, which are similar to-K and-M, both representing the display units, one for the K-byte and one for megabytes, and-H for Human-readable, which is a friendly-readable display

9 , Cat : Show or link files

Cat file1: displaying file1 file contents

Cat file1 file2: Display the contents of File1,file2 in turn

Cat file1 file2 > File3: Combine the contents of File1, File2, and then redirect (>) to File3 file > is the right redirect, indicating that the left command result is the input to the right command

Note: If the file on the right is an existing file, the original content will be emptied and the left command output. If you want to write in append mode, use the >> redirect instead

If the ">" left no file specified, such as: Cat >file1, will wait for the user input, after the input is completed and then press [Ctrl]+[c] or [ctrl]+[d], the user's input will be written to File1

Ten , Head , tail: Used to view files

Head displays the first n rows of the file, tail displays the end n rows of the file, and the default n is 10 rows. You can specify the number of rows by using the-N method

head-100 file and tail-100 files indicate the contents of the header 100 lines and the end 100 lines respectively

Tail-f file: See what's new in real-time, [Ctrl]+[c] Exit

One , WC : Counts the number of bytes, words, and lines in the specified file

-L: Count rows

-W: Count words

-C: Statistics of bytes

These options can be used in combination. The order and number of output columns are not affected by the order and number of options. Always appear in the following order and at most one column per item

Number of rows, words, bytes, filenames

If there is no file name in the command line, the file name does not appear in the output

WC file1 file2:

460 1679 16353 File1

1095 10665 File2

760 2774 27018 Total dosage

Wc-l file1:

460 File1

The default parameter is-LCW, that is, the WC file1 file2 command executes the same result as above

A , grep : Used to search for a line containing a specified pattern from a file and print it, a powerful text-search tool that supports searching text using regular expressions

grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can do some automated text processing with these return values

Ls-l | grep ' ^a ': Filters the contents of the Ls-l output through a pipeline, showing only the lines beginning with a

grep ' Test ' d*: Displays all rows with test in the file beginning with D

grep ' test ' file1 file2 file3: Displays the line that matches test in the File1,file2,file3 file

grep ' [a-z]/{7/} ' file: Displays all rows containing a string of at least 7 consecutive lowercase characters per string

grep ' t/(es/) t.*/1 ' file: if test is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (/1), which is found to display the line. If you use Egrep or GREP-E, do not use the "/" number to escape, directly written as ' t (es) T.*/1 ' on it.

The regular expression is used here, it is not here to repeat

- , pipelines, and Xargs

Pipeline:  

Using the pipe symbol "|" provided by Linux Separates the two commands, and the output from the left command of the pipe character is entered as the command to the right of the pipe. Continuous use of the pipe means that the output of the first command will be entered as the second command, and the output of the second command will be entered as the third command, and so on

Note: Not all commands support pipelines

ls | grep test: View the file or folder under the current directory with the name test

Xargs :

The pipeline is the input to the left command output as the right command, but what if you want to use the output of the previous command as a parameter? Need to use Xargs

Find/-name "core"-print | Xargs echo "" >/tmp/core.log: Find the memory information dump file in the entire system (core dump, which generates a core dump file when the general program exits abnormally), and then saves the results to the/tmp/core.log file

ls | Xargs RM: When there is too much file in a directory, the package parameters are too long with the RM * command, which can be removed in the following way

14, awk: Read the file line by row, with a blank space as the default delimiter will be sliced each row, cut the part of the various analytical processing

The most basic function of the awk language is to browse and extract information in a file or string based on the specified rules, before awk extracts the information for additional text operations. A complete awk script is typically used to format the information in a text file.

Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.

Last-n 5 | awk ' {print $} ': Show the 5 most recently logged-in accounts

PS | grep exe | awk ' {print $} ' | Xargs kill-9: Kill all processes named EXE end

cat/etc/passwd | Awk-f ': ' {print $} ': Show/etc/passwd's account

These are some of the shell commands that are commonly used in the testing process and are welcome to add to the discussion.

Shell Common Commands Summary

Related Article

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.