Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common commands of Shell in linux

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what Shell commands are commonly used in linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Ls: similar to the dir command under dos

There are three most commonly used parameters for ls:-a-l-F.

Ls-a

The files on the Linux are. The first files are treated as hidden files by the system, and they cannot be seen only with the ls command, while with ls-a, in addition to showing the general file name, even the hidden file will be displayed.

Ls-l

This parameter displays more detailed file information.

Ls-F

Using this parameter means adding more symbols indicating the file type at the end of the file, such as * for executable, / for directory, and @ for linking files, all because of the use of the-F parameter. But now almost all Linux distributions have a built-in-F parameter for ls, that is, we can see all kinds of distinguishing symbols without entering this parameter.

2. Cd: used to switch the user's current working directory

Cd aaa enters the aaa directory

If you do not specify a directory after the cd command, you will switch to the current user's home directory

Cd ~ switch to the current user's home directory without specifying a directory after the same function as cd

Cd-the command followed by a minus sign returns to the directory before the switch

Cd.. Return to the directory one level up under the current directory

3. Pwd: used to display the user's current working directory

4. Mkdir and rmdir

Midir: create a directory rmdir: delete a directory

Both commands support the-p parameter. For the mkdir command, it is created if the parent directory of the specified path does not exist, and for the rmdir command, all hierarchical directories of the specified path are deleted. If there is content in the folder, the rmdir command cannot be used.

As follows:

Mkdir-p 1-2-3

Rmdir-p 1-2-3

5. Cp: copy command

Copy one file to another directory: cp 1.txt.. / test2

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

Copy a folder an and rename it b:cp-r a b

6. Mv: move command

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

Rename a file in this directory: mv 1.txt 2.txt

Change a file to another directory and rename it: mv 1.txt.. / test1/2.txt

7. Rm command

The rm command is used to delete files. Similar to the del/erase command under dos, there are three parameters commonly used in the rm command:-irecom, RQ, and f.

-I: the system will ask for confirmation before deleting the file, and the file will not be deleted until the user enters the carriage. It should be noted that the files deleted under linux cannot be restored, so be sure to confirm them carefully before deleting them.

-r: this parameter supports directory deletion, which is similar to the rmdir command.

-f: contrary to the-I parameter,-f means forced deletion

8. Du, df command

The du command displays the disk space occupied by the current directory, and the df command shows the current disk space remaining.

If the du command takes no arguments, it returns the usage of the entire disk, which, if followed by a directory, is the usage of the directory on the disk.

Du-hs specifies a directory to view the total size of a specified directory

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

Both commands support the-kmam and-h parameters, similar to-k and-m, which represent display units, one is k bytes and the other is megabytes, and-h represents human-readable, which is a friendly and readable display.

9. Cat command

The function of the cat command is to display or link general ascii text files. Cat is an abbreviation for concatenate, similar to the type command under dos. The usage is as follows:

Cat file1 displays the contents of file1 files

Cat file1 file2 displays the contents of file1,file2 in turn

Cat file1 file2 > file3 combines the contents of file1 and file2, and then "redirects (>)" to the file3 file.

">" is the right redirector, which means that the result of the command on the left is regarded as the input of the command on the right. Note: if the file on the right is an existing file, its original contents will be cleared and become the output of the command on the left. If you want to write as an append, use the "> >" redirector instead.

If there is no specified file on the left side of ">", such as cat > file1, it will wait for the user to enter, and then press [Ctrl] + [c] or [Ctrl] + [d] after the input is finished, the user's input will be written to file1.

10. Echo command

The echo command is used no less frequently than ls and cat, especially in shell scripting.

Syntax: echo [- ne] [string]

Function: echo will send input strings to standard output, separated by blank characters, and add newline characters to * *.

Parameters:

Automatic line wrapping when-n displays a string

-e supports escape characters in the following formats, while-E does not support escape characters in the following formats

/ a sound of warning

/ b Delete the previous character

/ c * without newline symbol

/ f wraps the line but the cursor remains in its original position

/ n wrap and move the cursor to the beginning of the line

/ r move the cursor to the beginning of the line, but do not wrap

/ t insert tab

/ v is the same as / f

/ / insert / character

/ nnn inserts the ASCII character represented by nnn (octal)

Example:

Oracle@hjtest:~/hgd > echo "123" 456"

123 456

Oracle@hjtest:~/hgd > echo "123/n456"

123/n456

Oracle@hjtest:~/hgd > echo-e "123/n456"

one hundred and twenty three

four hundred and fifty six

Oracle@hjtest:~/hgd > echo-E "123/n456"

123/n456

Oracle@hjtest:~/hgd > echo-E "123Compact Universe 456"

123//456

Oracle@hjtest:~/hgd > echo-e "123Compact Universe 456"

123/456

Oracle@hjtest:~/hgd > echo-e "123Compact 100456"

123 to 456

Note:

Under the bash used by Linux, there is a difference between single quotation marks''and double quotation marks. Single quotation marks ignore all escapes, and double quotes do not ignore the following special characters:

Dollar signs ($), Back quotes (`), Backslashes (/), Excalmatory mark (!)

Examples are as follows:

Oracle@hjtest:~ > echo "`TEST`"

-bash: TEST: command not found

Oracle@hjtest:~ > echo '`TEST`'

`TEST`

Oracle@hjtest:~ > echo "$TEST"

Oracle@hjtest:~ > echo'$TEST'

$TEST

Oracle@hjtest:~ > echo "/ / TEST"

/ TEST

Oracle@hjtest:~ > echo'/ / TEST'

/ / TEST

Oracle@hjtest:~ > echo "Hello!"

Echo "Hello"

Hello

Oracle@hjtest:~ > echo'Hellobirds'

Hello!

11 、 more,less,clear

N more,less command

These two commands are used to view files. If a file is too long and the display content exceeds a screen, you can only see the content of * * with the cat command, and you can view it by page with the commands more and less. The more directive allows paging of the contents of more than one page to be suspended, and the user presses the button before continuing to display the next page. And less in addition to the function of more, you can also use the arrow keys to scroll up or down the file, more convenient to browse and read.

Common action commands for less:

Press enter to move down one line

Y move up one line

The space bar scrolls down one screen

B scroll up one screen

D scroll down half the screen

Help from h less

U move half the screen upward

W can specify which line to display, starting from the next line of the specified number; for example, if the specified number is 6, it will be displayed from line 7

G jump to the * line

G jumps to * line

P n% jumps to n%, such as 10%, that is, the display starts at 10% of the contents of the entire file.

/ pattern searches for pattern. For example, / MAIL means to search for MAIL words in a file.

V call the vi editor

Q exit less

! command calls SHELL to run commands; for example! ls displays all files under the current directory of the current column

N clear command

The clear command is used to clear the current screen display without any parameters, and has the same function as the cls command under dos.

12 、 head,tail

N head and tail commands

All are used to view text files, the difference is that head displays the first n lines of the file, tail displays the last n lines of the file, and the default n is 10 lines. You can specify the number of rows by-n, such as:

Head-100 file and tail-100 file show the contents of the first and last 100 lines of the file, respectively.

N tail-f command

You can view what's new in the file in real time.

13. Wc command

This command is used to count the number of bytes, words, and lines in the specified file. The options for this command mean the following:

-l count the number of rows

-w count words

-c Statistics Bytes

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

Number of lines, words, bytes, file name

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

For example:

Oracle@hjtest:~ > wc 1.txt 2.txt

460 1679 16353 1.txt

300 1095 10665 2.txt

Total dosage of 760 2774 27018

Oracle@hjtest:~ > wc-l 1.txt

460 1.txt

The default parameter is-lcw, that is, the result of the wc file1 file2 command is the same as above.

14. Grep command

Grep is an acronym for (global search regular _ expression (RE) and print out the line), which is used to search for lines containing specified patterns from the file side and print them out. It is a powerful text search tool that supports searching for text using regular expressions. Grep works like this by searching for string templates in one or more files. If the template includes spaces, it must be referenced by "", and all strings after the template are treated as file names. The search results are sent to the screen without affecting the contents of the original file.

Grep can be used for shell scripts because grep returns a status value to indicate the status of the search, 0 if the template search is successful, 1 if the search is unsuccessful, and 2 if the searched file does not exist. We can use these return values to do some automated text processing.

Example:

$ls-l | grep'^ a'

Filter the ls-l output through the pipeline, showing only lines that start with a.

$grep 'test' d*

Displays lines that contain test in all files that begin with d.

$grep 'test' aa bb cc

Displays the lines that match the test in the aa,bb,cc file.

$grep'[a Merz] / {5 /}'aa

Displays all lines that contain at least five consecutive lowercase characters for each string.

$grep'w / (es/) t.incremental aa 1'

If the west is matched, the es is stored in memory and marked as 1, and then any character (. *) is searched, followed by another es (/ 1), and the line is displayed when found. If you use egrep or grep-E, instead of using the "/" sign for escape, you can just write'w (es) t.

15. Man,logout command

Man command

Man is the abbreviation of manual, which is equivalent to the online Help of Unix/Linux. Every system command and call has a very detailed description, most of which are in English. Such as: man ls is to view the instructions for the use of the ls command, there is generally another way to view help, such as: ls-help, which is supported by most commands.

Logout command

This command is used to exit the system and corresponds to the login command.

16. Piping and xargs

Pipe:

Use the pipe character "|" provided by Linux to separate the two commands, and the output of the command on the left side of the pipe character will be used as the input of the command on the right side of the pipe character. Continuous use of pipes means that the output of * * commands will be used as the input of the second command, the output of the second command will be used as the input of the third command, and so on.

Note: the input of the command on the left side of the pipe is used as the input of the command on the right side of the pipe (the input of the command is certain), not a parameter, not all commands support pipes.

Example: ls | grep a to view the file or folder whose name contains an in the current directory

Xargs:

Most Linux commands produce output: list of files, list of strings, and so on. But what if you want to use some other command and take the output of the previous command as a parameter? For example, the file command displays the file type (executable, ascii text, etc.); you can process the output to show only the file names, which you currently want to pass to the ls-l command to view the timestamp. The xargs command is used to do this.

Note: the find command passes the matching files to the xargs command, while the xargs command fetches only some of the files at a time, not all of them, unlike the-exec option. In this way, it can first process some of the files obtained by *, then the next batch, and go on like this.

Example:

1. Look for the memory information dump file (core dump) throughout the system, and then save the results to the / tmp/core.log file:

$find /-name "core"-print | xargs echo "" > / tmp/core.log

2. When there are too many files in a directory, directly using the rm * command will cause the package parameter to be too long. You can delete all of them by using the following method

$ls | xargs rm

17, basename and dirname

Basename is used to check the name of a file without a path, while dirname is used to view the path of a file. We will know the effect by testing the results:

> basename / home/hj/1.txt

1.txt

> dirname / home/hj/1.txt

/ home/hj

> basename 1.txt

1.txt

> dirname 1.txt

This is the end of this article on "what are the common commands of Shell in linux?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report