Introduction to the shell common commands for Linux

Source: Internet
Author: User
Tags create directory parent directory stdin

One, CD switch directory

CD/ETC switch to/etc Directory CD ~ Switch to home directory

Cd.. Return to the parent directory CD.  /.. Return to the top level two directory

CD-Returns the directory where it was before entering this directory

Second, echo output

echo "This is a test!" Display Normal string:

Echo-e "\ n" output a blank line,-e open escape

Echo ' date ' displays command execution results

Iii. chmod grant of Use rights

CHMOD ABC file Basic format

Each of which a,b,c is a number, respectively, the user, Group, and other permissions, permissions can be divided into: r=4 means readable, w=2 means writable, x=1 means executable.

To rwx the attribute then 4+2+1=7;

To rw-the attribute then 4+2=6;

The r--property is 4.

Four, cat view files

1) Print the contents of the file:

Cat cat1.txt Print Individual file contents

Cat cat1.txt cat2.txt Print multiple file contents to stitch together the contents of a file

echo "Test" | Cat-cat1.txt joining standard input and file content together

In the above code-the file name that is used as the stdin text

2) Common parameters when printing file contents:

Cat-n cat3.txt Print file contents and line numbers, line numbers are added to blank lines

Cat-b cat3.txt Print file contents and line numbers, skip blank lines

Cat-s cat3.txt to compress adjacent blank lines

3) Create a file from the keyboard:

Cat > Cat4.txt can only create new files and cannot edit existing files

4) Merge several files into one file:

Cat Cat1.txt cat2.txt cat3.txt > Cat5.txt

V. SED text editing text

1) Partial Replacement:

Sed ' s/world/sed/' sed1.txt?

Replaces only the first match of each row, does not modify the source file

Sed ' s/world/sed/2 ' sed1.txt??

Replace the 2nd match on each line?

2) Global substitution:

Sed ' s/world/sed/g ' sed1.txt

Replace all the matches in the file

3) Replace the string with the regular expression:

Sed ' s/^hello.*/sed/g ' sed1.txt

4) delimiter/can be replaced by #; * Equal characters

Sed ' s*world*sed*g ' sed1.txt

5) Delete characters:

Sed '/^$/d ' sed1.txt delete empty lines in Sed1.txt

6) If the action string contains a variable, enclose the variable again in single quotation marks:

b= "SED"

Sed ' s/world/' $b ' ' Sed1.txt?

Otherwise, the variable name is treated directly as a string

7) Direct operation of source files:?

Sed-i ' s/world/sed/' Sed1.txt?

8) Replace multiple at once:?

Sed-e ' S/hello/this is/g '-e ' s/sed/the sed/g ' Sed1.txt?

9) The replacement action is included in the file:?

Vim./test.sed

S/hello/this is/g?

S/sed/the sed/g?

Sed-f test.sed sed1.txt? > Sed2.txt--Save in a new file

Sed-i-F test.sed sed1.txt? --Direct action in the source file

VI. read accepts keyboard data and reads files

1) Read accepts input from the keyboard:

Read-p "Enter Your name:" Name

echo "Hello $name"

If you do not specify a variable, the read command places the received data in the environment variable reply:

Read-p "How old is You?"

echo "I ' m $REPLY"

2) Accept multiple data:?

? read-p "Enter Your name and age:" Name age

echo "My name is $name, and I ' m $age."

3) Timing input:?

? Read-t 3-p "Enter Your name:" Name

4) Reading silently:

Read-s-P "Enter Your password:" Password

echo "Your password is $ password"

The password you entered does not appear on the screen.

5) Read the file:?

Vim./test.txt

First

Second

Third

Vim./read4.sh

Cat-n Test.txt | While Read line

Do

Echo $line

Done

Vii. awk Advanced Text Processing

1) Basic Structure Introduction:

awk ' begin{commands} {commands} end{commands} ' file

The awk command can also be read from stdin:

echo | awk ' begin{commands} {commands} end{commands} '

awk scripts typically consist of 3 parts: BEGIN, statement block, edn,3 section are optional, and any part can be omitted from the script.

Where the begin and end keywords must be capitalized, the script contents of awk are enclosed in single or double quotes, and the awk script uses the print output.

All commands are separated by semicolons.

2) How awk works:?

(1) Execute begin{commands}? statement

(2) Read a line from a file or stdin, and then execute {commands}, repeating the process until all the files have been read

(3) When reading to the end of the input stream, execute the statement in end{commands}

3) Read the line from the file:?

awk ' begin{print "Start"} {print} end{print "END"} ' Awk1.txt

When you use print without parameters in a statement block, it prints the current line that is read from the stdin or the file.

4) Read the line from the standard input:?

Echo-e "Line1\nline2" | awk ' begin{print "Start"} {print} end{print "END"} '

5) Omit begin:

awk ' {print} end{print ' END '} ' awk1.txt

6) Omit end again:

awk ' {print} ' Awk1.txt

7) Omit statement block:

awk ' begin{print ' start '} end{print "END"} ' Awk1.txt

8) How the statement block works:

Reads a record from a file or standard input that has a ' \ n ' newline, and divides the record by the specified delimiter, and $ A records all fields, representing the first field of the record, $n representing the nth field of the record. The default delimiter is the "blank key" or "[tab] key".

9) Some other special variables:

NR: Indicates the number of records that corresponds to the current line number during execution.

NF: Indicates the number of fields that correspond to the current number of fields during execution.

Example:

awk ' {print $} ' awk2.txt--Print all text content

awk ' {print nr,nf,$3} ' awk2.txt--Prints the text content of the 3rd column of each row

awk ' end{print NR} ' awk2.txt--number of rows in the statistics file

awk ' end{print NF} ' awk2.txt--Number of columns in the statistics file

Awk-f '; ' ' {print $} ' awk3.txt--outputs the second field

Viii. telnet between SSH Linux

SSH [email protected]_ip terminal Telnet to target machine

SSH [email protected]_ip] command; Command, ... "

Remote Login The target machine executes various shell commands, separated by semicolons

Ix. copying files and directories between SCP Linux

1. Copy from local to remote

Scp-r Local_folder [Email protected]_ip:remote_folder

Scp-r Local_folder Remote_ip:remote_folder

2. Copy from remote to local

scp-r [Email Protected]_ip:remote_folder local_folder

Ten, redirect

Command > FileName ">" indicates standard output to filename, if the file does not exist, the file is created and already exists, overwriting the contents of the file

Command >> filename ">>" indicates standard output to filename, if the file does not exist, the file is created, already exists, appended to the original file

Command >> filename 2>&1 "2>&1" is to redirect the error output to standard output, where the standard output has been redirected to filename, and the error output is redirected to the filename file

Xi. RM Delete files and directories

1) Delete files:

RM rm1.txt Delete Rm1.txt

RM rm2.txt Rm3.txt Delete a specified two files in turn

2) Delete directory:

Rm-r Dir1 Deleting a directory with the-R parameter

Note: Deleting a file or directory from the previous method will ask the user if you want to delete it directly by specifying the parameter-F

Rm-f rm4.txt Delete files directly rm4.txt

RM-RF DIR2 Delete directory directly Dir2

3) Display the execution process information:

RM-VF rm5.txt directly deletes the file Rm5.txt and displays the execution process information

RM-VRF Dir3 directly deletes the DIR3 directory and displays the execution process information

4) Use regular expressions:

RM-VF *.log Delete all. log files in the current directory

RM-VRF *rm* Delete all files and directories with RM characters in their names

12. CP Copy files and directories

1) Copy the file:

CP cp1.txt cp2.txt Copy files under current directory

\CP cp1.txt cp2.txt Force replication by \CP when the destination file already exists

CP cp1.txt/root/cp2.txt copying files across directories

2) Copy the files and directories to the new directory (the new directory must already exist):

CP cp1.txt DIR1 Copy a file into the directory Dir1

CP cp1.log cp2.txt Dir1 copy multiple files to directory Dir1

Cp-r dir2 dir1 Copy directory Dir2 to directory Dir1

3) Use regular expressions

CP *.log DIR3 Copy all the. log files under the current directory to the DIR3 directory

Cp-r dir1/* dir3 copy all files under Dir1 and directories into DIR3

4) Common parameters:

Cp-v cp1.txt cp3.txt Display execution process information

Cp-i Cp1.txt cp3.txt If the destination file already exists, ask the user before copying

13, free Check the use of memory:

-B,-K,-m display memory usage in byte, KB, MB

-s< interval seconds > Continuous observation of memory usage

14. mkdir Create Directory

mkdir dir can only create normal directories that do not contain subdirectories

Mkdir-p Dir/dir1 to create a directory with subdirectories

Note: The specified directory name cannot be an existing directory in the current directory

Numerical calculation of variable by let

A=1 let "a+1" echo &a output of 2

16. LS lists all subdirectories and files in the target directory

Ls/root listing the contents of the/root directory

You can also go to the/root directory with the CD command and then execute the LS:

Cd/root

LS-LH #列出文件详细信息, file size in kilobytes, only one file information per line

17, DF Check the disk space consumption of the file system

Df-h display in an easier-to-read manner based on current disk space usage

18. mv Files and directories Rename , move

1) file, directory rename:

MV mv.txt new_mv.txt File Rename

MV dir new_dir Directory rename

2) Move the file into the directory (the destination directory must already exist):

MV New_mv.txt mv.log Dir1 move multiple files in the current directory into Dir1

MV Dir1/new_mv.txt dir2 Mobile Dir1 directory new_mv.txt to Dir2

3) Move the directory to the new directory (the new directory must already exist):

MV Dir1 Dir2 move Dir1 to Dir2

4) Use regular expressions

MV Dir2/*.txt dir3 move all. txt files in Dir1 to Dir3

5) Common parameters:

Mv-f dir3/new_mv.txt dir Direct override when the file already exists under Dir

MV Log1.txt-b Log2.txt First log2.txt backup (name log2.txt~) and overwrite

Mv-t dir2 log1.txt log2.txt move multiple files to Dir2, target directory in front

19, TR character operation (replace, delete, compress, etc.)

1) character conversion (substitution):

echo "TEST" | Tr ' A-Z ' A-Z

echo "ABC ADF" | TR ' abc ' ' XYZ '

Instead of replacing the entire string "abc" with "XYZ", replace "a" with "X", "B" with "Y", "C" as "Z"

2) Delete characters:

Cat Cat_tr.txt | Tr-d ' 0-9 '

3) Compressed characters

echo "Tesssssssst" | Tr-s ' s '

4) Exclude Extra empty lines:

Cat Cat_tr.txt | Tr-s ' \ n '

TR matches the extra "\ n" character to a single "\ n"

20. Find Files

1) Basic format:

Find Path-option [-print] [-exec-ok command] {} \;

-print output of the found file to standard output
-exec command {} \; Command operations on the files that are found
-ok and-exec are the same, except to consult the user before operation
2) Common command options:?

-name finding files by file name
-perm follow file permissions to find files
-user Find files According to the user who owns the file
-group Find files according to the group to which the files belong

-size to find files by file size

-type find a file of a certain type, such as: B device file; d directory;

c character device file; p pipe file; l symbolic link file; F normal file

-empty find blank files, folders without subdirectories

3) Examples:

Find/root/linux-shell/find-name ' *.txt '--Specify the lookup path

Find/-name ' *dir1 '--look for files or directories under the current directory named Dir1

Find./-name ' *dir1 '-type D--Look for a directory named Dir1

Find./-name ' *dir1 '-type F--Look for a plain file named Dir1

Find./-perm 777--finding files or directories according to file permissions

Find. -user Oscar--Find the file or directory to which the user belongs to Oscar

Find. -group Oscar--Find the file or directory in which the group belongs to Oscar

Find. -size +50k--Find files larger than 50k

Find. -size +50k-size-100k--Find files larger than 50k and less than 100k

Find. -empty--Find blank files or empty folders

Find.   -name ' *.log '-exec cat {} \; --Find Files and view file contents

Find.  -name ' *.log '-exec cp {} new_find.log \; --Find files and copy them

21. One of the performance analysis tools of SAR system

SAR is one of the Linux system performance analysis tools, the basic format:

SAR [options] [t] [n]

T is the sampling interval, n is the number of samples, the default value is 1

Common options:

-U: Output statistics about CPU usage

-r: Output memory and swap space statistics

-B: Output I/O and transfer rate statistics

-D: Output activity information for each block device

-X: Process ID

Sar-u 2 3 samples every 2 seconds, 3 consecutive samples, monitoring CPU usage

Sar-r 2 3 samples every 2 seconds, 3 consecutive samples, monitoring memory and swap space usage

Sar-b 2 3 samples every 2 seconds, 3 consecutive samples, monitoring the use of the buffer

Sar-d 2 3 samples every 2 seconds, 3 consecutive samples, monitoring equipment usage

Sar-u 2 3-x pid sampling once every 2 seconds, 3 consecutive samples, monitoring the use of PID CPU

22, PS static results output a certain point in time the operation of the program

PS aux

Ps-ef

Two commands can see all the currently in-memory programs, different points, the first also contains the program's CPU, memory utilization.

Ps-ef | grep Oscar view usage of the process with the Oscar string

23, top dynamic output program changes

1) Top Command example:

Top-b-N 2 >/tmp/top.txt

Top batch is updated 2 times and the result information is stored in the/tmp/top.txt file

top-d 3-n 3-p PID

Three seconds per interval monitor process usage with process number PID, updated 3 times

2) Key commands that are commonly used during top execution:

P: Sort display with CPU usage resources

M: Sort display using Memory's resource

N: Sort by PID

Introduction to the Shell Common command for Linux

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.