Linux common commands 2

echo "hello" >> test.txt append hello to test.txt one > yes overwrite two >> yes append
echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
echo {a..z }
abcdefghijklmnopqrstu vwxyz
echo stu{0..10}
stu0 stu1 stu2 stu3 stu4 stu5 stu6 stu7 stu8 stu9 stu10
touch file{1..10} Create files 1-10
mkdir stu{0..10} Create folders stu0-stu10
rmdir stu{0..10} delete folder stu0-stu10

head -4 test.txt|tail -2 Take the first 4 in test.txt and then take the last 2
grep -v 'OLDBOY' test.txt //Print the data after removing LODBOY
grep 'OLDBOY' test.txt // Print OLDBOY
sed -n '/OLDBOY/p' test.txt //Print OLDBOY
sed "3d" test.txt //Print the data after removing the 3rd line, just enter the line number to remove multiple lines, and the interval is
sed "/ OLDBOY/d" test.txt //Print the data after excluding OLDBOY/x/ is a regular expression, after excluding all those containing x, print
sed '/^2/d' test.txt //Print excluding the beginning of 2 The data after the data /^2/ is a regular expression, excluding the
sed "/OLDBOY/p" test.txt that starts with 2 //After printing all, then print OLDBOY /x/ as a regular expression, print out all items containing x sed '
2p' test.txt //The second line is output again with single quotes
sed -n '2p' test.txt //The second line is output
sed "1a hello world" test.txt //The a parameter indicates that the specified Insert after the line, enter the content to be inserted after a, 1a means insert sed "a hello world" test.txt under the first
line
.txt //The i parameter means to insert before the specified line, enter the content to be inserted after i, 1i means to insert before the first line
sed "1c hello world" test.txt //The c parameter means to replace the content of the specified line, enter the content to be replaced after i, 1c means to replace the first line of content
sed "/^T/c hello world" test.txt // Replace lines starting with T
sed "/L/c hello world" test.txt //Replace all lines with L
sed "s/OLD/old/" test.txt //s replace some characters, followed by /original characters /replace characters/ replace only the first encountered characters on each line
sed "s/OLD/old/g" test.txt //replace all encountered characters on each line
sed "1s/OLD/old/g" test .txt //s also supports specifying the line number, the line number before s is the line number, only the first line is replaced
sed "5,$s/OLD/old/g" test.txt //replace the fifth line to the last line
sed ' /^[0-9]/s/aa/AA/g' test.txt //regular expression, replace all lines starting with numbers
sed -n "2,8p" test1.txt //print the second -8 lines of data
sed "2,8d" test1.txt //Print the data after excluding 2-8 lines

awk -F ":" '{print $1}' /etc/passwd //Use : as the separator to output the first column in the passwd file with $ followed by a few columns
awk -F ":" '{print $1" = "$2"="$3}' /etc/passwd //Use : as the separator to separate the 1st, 2nd, and 3rd columns in the passwd file with = and output
awk -F ":" '{print $(NF )}' /etc/passwd //Output the last column in the passwd file with : as the delimiter
awk '{if(NR>3 && NR<8) print $1}' test.txt output test.txt line 4-7
awk -F ":" '{if(NR>2 && NR<5) print $1}' test.txt Use: as the delimiter, output the first column (the number followed by $ is the number of columns) Line 3-4 data

grep 5 -B 2 test.txt outputs the 5th line and the first 2 lines in
test.txt grep 5 -A 2 test.txt outputs the 5th line and the last 2 lines in
test.txt grep 5 -C 2 test.txt outputs test Line 5 and 2 lines before and after .txt
ls -l |grep ^d Output all directory files
ls -l |grep ^[^d] Output all files except directories
ls -l |grep -i Mar Output all Mar months The file
grep "13[0-2]" address extracts all lines [130,131,132] starting with 13 followed by [0-2] in address

All the experiments we did before actually did not modify the content of the test.txt file, that is to say, the
modification were only output to the console, and the content of the file test.txt was not modified, we can Use the -i option to tell sed to modify the contents of the file directly, instead of outputting the modifications to the terminal, for example:
sed -i '2d' test.txt

List regular metacharacters commonly used in sed:

$ means end of line
^ means start of line
[a-z0-9] means range of characters
[^] means characters other than those in the character set

\(\) and \{m,n\} need to be escaped in sed's regular

. means any character
* means zero or more
\+ one or more  
\? zero or one
\| means or syntax

yum install tree (package name) -y
rpm -qa tree
rpm -ivh installation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324761401&siteId=291194637