First use a script to simulate the creation of 14 test files:
#!/bin/bash
for ((i=1;i<=10;i++));
do
if [ $i -lt 3 ]
then
touch /home/mysql/test/test$i.sh
touch /home/mysql/test/Phtest$i.sh
touch /home/mysql/test/test$i.sh.bak
elif [ $i -lt 8 ]
then
touch /home/mysql/test/test$i.log
else
touch /home/mysql/test/test$i.sql
fi
done
After executing the above script, 14 files will be created in the /home/mysql/test/ directory, the file names are as follows:
[mysql@mysql test]$ ll
总用量 8
-rwxrwxr-x 1 mysql mysql 278 12 月 28 15:04 create_file.sh
-rw-rw-r-- 1 mysql mysql 42 12 月 28 14:39 line.txt
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 Phtest1.sh
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 Phtest2.sh
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test10.sql
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test1.sh
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test1.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test2.sh
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test2.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test3.log
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test4.log
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test5.log
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test6.log
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test7.log
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test8.sql
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test9.sql
1. find 和 xargs
The function of the find command is also a very frequently used operation command in the operation and maintenance process. Its role is to help the operation and maintenance personnel to find the corresponding file. Here is an introduction to the most commonly used find usage.
1.1 Introduction to common find parameters
-exec
: The find command executes the shell command given by this parameter on the matching file, this command is very easy to use-name
: Find the file name to be matched, the name can be matched with regular-mtime -n +n
: Find files by file change time, -n means within n days, +n means n days ago-ctime -n +n
:Search files according to the time of file creation, -n means less than n days, +n means n days ago-atime -n +n
:Check by file access time, -n means less than n days, +n means n days ago-type b/d/c/p/l/f
: The searched files correspond to block devices, directories, character devices, pipes, symbolic links, and ordinary files.-size n[c]
: Search for files with a length of n bytes, if it is +n, it is a list of files whose size exceeds n bytes-mindepth -maxdepth
: These two parameters are used together to limit the depth range of the file directory level that find can find
1.2 find case
1. Find a list of all files ending with sh in the /home/mysql/test directory
[mysql@mysql test]$ find /home/mysql/test -name '*sh'
/home/mysql/test/test1.sh
/home/mysql/test/test2.sh
/home/mysql/test/create_file.sh
/home/mysql/test/Phtest1.sh
/home/mysql/test/Phtest2.sh
/home/mysql/test/dir_01/test1.sh
/home/mysql/test/dir_01/test2.sh
/home/mysql/test/dir_01/dir_02/test7.sh
/home/mysql/test/dir_sh
2. Find a list of all files ending with sh in the /home/mysql/test directory, and specify the file type as file
[mysql@mysql test]$ find /home/mysql/test -name '*sh' -type f
/home/mysql/test/test1.sh
/home/mysql/test/test2.sh
/home/mysql/test/create_file.sh
/home/mysql/test/Phtest1.sh
/home/mysql/test/Phtest2.sh
/home/mysql/test/dir_01/test1.sh
/home/mysql/test/dir_01/test2.sh
/home/mysql/test/dir_01/dir_02/test7.sh
3. Find a list of all files ending with sh in the /home/mysql/test directory, and specify the file type as a file, and do not search for subdirectories under the /home/mysql/test directory
[mysql@mysql test]$ find /home/mysql/test -mindepth 1 -maxdepth 1 -name '*sh' -type f
/home/mysql/test/test1.sh
/home/mysql/test/test2.sh
/home/mysql/test/create_file.sh
/home/mysql/test/Phtest1.sh
/home/mysql/test/Phtest2.sh
4. Find a list of all files ending with sh in the /home/mysql/test directory, and specify the file type as file, and the file size exceeds 100 bytes
[mysql@mysql test]$ find /home/mysql/test -name '*sh' -type f -size +100c
/home/mysql/test/create_file.sh
It should be noted here that if it is written -size 100c
, only files with a size of 100 bytes will be searched:
[mysql@mysql test]$ ls -l|grep -i create_file.sh
-rwxrwxr-x 1 mysql mysql 278 12 月 28 15:04 create_file.sh
[mysql@mysql test]$
[mysql@mysql test]$ find /home/mysql/test -name '*sh' -type f -size 278c
/home/mysql/test/create_file.sh
[mysql@mysql test]$ find /home/mysql/test -name '*sh' -type f -size 277c
5. Find a list of all files ending in sh / home / mysql / test directory, and specify the file type as a file, do not look for / home / mysql subdirectory under the test directory /, and add the suffix .bak files found
in There are 2 implementation methods, the first one uses -exec
, the second uses xargs:
[mysql@mysql test]$ find /home/mysql/test -mindepth 1 -maxdepth 1 -name '*sh' -type f -exec mv {
} {
}.bak \;
[mysql@mysql test]$
[mysql@mysql test]$ ls -l|grep -i bak
-rwxrwxr-x 1 mysql mysql 278 12 月 28 15:04 create_file.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 Phtest1.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 Phtest2.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test1.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test2.sh.bak
Xargs way to achieve:
[mysql@mysql test]$ find /home/mysql/test -mindepth 1 -maxdepth 1 -name '*sh' -type f
/home/mysql/test/create_file.sh
/home/mysql/test/Phtest1.sh
/home/mysql/test/Phtest2.sh
/home/mysql/test/test1.sh
/home/mysql/test/test2.sh
[mysql@mysql test]$
[mysql@mysql test]$ find /home/mysql/test -mindepth 1 -maxdepth 1 -name '*sh' -type f|xargs -I file sh -c "mv file file.bak"
[mysql@mysql test]$ ls -l|grep -i bak
-rwxrwxr-x 1 mysql mysql 278 12 月 28 15:04 create_file.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 Phtest1.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 Phtest2.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test1.sh.bak
-rw-rw-r-- 1 mysql mysql 0 12 月 28 15:04 test2.sh.bak
**6. **Search Find a list of all files ending with .bak in the /home/mysql/test directory, and specify the file type as a file, do not search for subdirectories under the /home/mysql/test directory, and will find Files are deleted directly. This operation is often used to clean up the application log. It can also be added -mtime
to find files older than N days and delete them.
[mysql@mysql test]$ find /home/mysql/test -mindepth 1 -maxdepth 1 -name '*.bak' -type f
/home/mysql/test/create_file.sh.bak
/home/mysql/test/Phtest1.sh.bak
/home/mysql/test/Phtest2.sh.bak
/home/mysql/test/test1.sh.bak
/home/mysql/test/test2.sh.bak
[mysql@mysql test]$
[mysql@mysql test]$ find /home/mysql/test -mindepth 1 -maxdepth 1 -name '*.bak' -type f |xargs rm -rf
[mysql@mysql test]$ ls -l|grep -i bak
[mysql@mysql test]$
2. awk
The function of awk can be said to be very powerful. If you want to introduce awk in detail, you can write a book. Here, I will introduce some common usages.
1. Specify the output column to print ls -l
[mysql@mysql test]$ ls -l|grep -i test|awk '{print $9}'
test10.sql
test3.log
test4.log
test5.log
test6.log
test7.log
test8.sql
test9.sql
2. String splicing
[mysql@mysql test]$ ls -l|grep -i test|awk '{print "rm -rf "$9}'
rm -rf test10.sql
rm -rf test3.log
rm -rf test4.log
rm -rf test5.log
rm -rf test6.log
rm -rf test7.log
rm -rf test8.sql
rm -rf test9.sql
3. Print single quotation marks and double quotation marks. This function is sometimes very useful when maintaining the database, such as splicing SQL
[mysql@mysql test]$ ls -l|grep -i test|awk '{print "'\''"$9"'\''"}'
'test10.sql'
'test3.log'
'test4.log'
'test5.log'
'test6.log'
'test7.log'
'test8.sql'
'test9.sql'
[mysql@mysql test]$ ls -l|grep -i test|awk '{print "\""$9"\""}'
"test10.sql"
"test3.log"
"test4.log"
"test5.log"
"test6.log"
"test7.log"
"test8.sql"
"test9.sql"
3. yet
Sed is a good hand when dealing with file replacement, a very easy to use file editing tool.
Before introducing the sed case, prepare a file. The content of the file is as follows:
[mysql@mysql test]$ cat line.txt
line1
line2
LINE3
line4
LIne5
line6
line7
1. Delete blank lines in the line.txt file
[mysql@mysql test]$ sed '/^ *$/d' line.txt
line1
line2
LINE3
line4
LIne5
line6
line7
[mysql@mysql test]$
[mysql@mysql test]$ cat line.txt
line1
line2
LINE3
line4
LIne5
line6
line7
Can be seen from the above results, the contents of the source file line.txt did not change, if the change to take effect, you can add -i
parameters to directly modify the source file content, if not, it will not modify the source file.
[mysql@mysql test]$ sed -i '/^ *$/d' line.txt
[mysql@mysql test]$ cat line.txt
line1
line2
LINE3
line4
LIne5
line6
line7
2. Replace all line strings in the line.txt file with test
[mysql@mysql test]$ sed -i 's/line/test/g' line.txt
[mysql@mysql test]$ cat line.txt
test1
test2
LINE3
test4
LIne5
test6
test7
3. Delete the line record containing the LINE string
[mysql@mysql test]$ sed '/LINE/d' line.txt
test1
test2
test4
LIne5
test6
test7
4. Find the previous line of the LINE string
[mysql@mysql test]$ cat line.txt
test1
test2
LINE3
test4
LIne5
test6
test7
[mysql@mysql test]$ sed -n '/LINE3/{g;1!p;};h' line.txt
test2
5. Find the next line of the LINE string
[mysql@mysql test]$ cat line.txt
test1
test2
LINE3
test4
LIne5
test6
test7
[mysql@mysql test]$ sed -n '/LINE3/{n;p}' line.txt
test4
4. vi
vi is a very powerful editing command for linux systems. To master vi commands proficiently is an essential skill for operation and maintenance personnel.
Test the content of the line.txt file
[mysql@mysql test]$ cat line.txt
test1
test2
LINE3
test4
LIne5
test6
test7
1. Display line number and close line number set nu display line number set nonu close line number
1 test1
2 test2
3 LINE3
4 test4
5 LIne5
6 test6
7 test7
:set nu
2. The cursor jumps to the last line to enter the editing mode, press shift+g
3. The cursor jumps to the first line to enter the edit mode, press gg, the cursor can jump to the first line
4. The cursor jumps to the beginning of the line Enter the edit mode, press shift + ^
5. The cursor jumps to the end of the line to enter the edit mode, press shift + $
6. Delete operation enters the edit mode, press dd to delete the line where the cursor is located, and press x to delete the cursor Where the character is located
7. Replace and enter the editing mode, first hit shift + :, then enter %s/test/jim/g to perform a global replacement. If you enter s/test/jim/, only the line where the cursor is located will be replaced
8. Save and enter In edit mode, first hit shift +:, then enter wq! to save and exit. If you don’t want to save, you can enter q! to exit directly.
5. ps
The ps command is a command to display operating system process information.
4. Find the top 10 processes that consume the most memory
[root@192 ~]# ps aux|sort -nr -k4|head -10
root 50929 0.3 9.1 679612 91068 ? Ssl 9 月 03 8:25 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
mysql 40333 0.4 7.9 1694620 79320 ? Sl 9 月 02 16:52 /u02/mysql/bin/mysqld --defaults-file=/u02/conf/my3308.cnf --basedir=/u02/mysql --datadir=/u02/data/3308 --plugin-dir=/u02/mysql/lib/plugin --log-error=/u02/log/3308/error.log --open-files-limit=65535 --pid-file=/u02/run/3308/mysqld.pid --socket=/u02/run/3308/mysql.sock --port=3308
polkitd 95030 1.8 7.1 1587420 71132 ? Ssl+ 9 月 04 27:57 mongod --bind_ip_all
root 957 0.0 1.1 499804 11904 ? Ssl 8 月 31 4:45 /usr/bin/containerd
mysql 40532 0.0 0.6 163420 6416 pts/4 S+ 06:26 0:00 /u02/mysql/bin/mysql -uroot -px xx --socket=/u02/run/3308/mysql.sock
root 43302 0.0 0.5 159396 5964 ? Ss 07:20 0:00 sshd: jim [priv]
root 36507 0.0 0.5 159396 5964 ? Ss 05:16 0:00 sshd: jim [priv]
root 33300 0.0 0.5 102896 5520 ? S 04:21 0:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens33.pid -lf /var/lib/NetworkManager/dhclient-97308c9f-82a6-4ab2-84a9-37bf5691a1e8-ens33.lease -cf /var/lib/NetworkManager/dhclient-ens33.conf ens33
root 706 0.0 0.4 628280 4528 ? Ssl 8 月 31 0:39 /usr/sbin/NetworkManager --no-daemon
root 43419 0.0 0.4 241196 4628 pts/6 S 07:20 0:00 sudo su - root
5. Find the top 10 processes that consume the most CPU resources
[root@192 ~]# ps aux|sort -nr -k3|head -10
polkitd 95030 1.8 7.1 1587420 71132 ? Ssl+ 9 月 04 27:57 mongod --bind_ip_all
mysql 40333 0.4 7.9 1694620 79320 ? Sl 9 月 02 16:52 /u02/mysql/bin/mysqld --defaults-file=/u02/conf/my3308.cnf --basedir=/u02/mysql --datadir=/u02/data/3308 --plugin-dir=/u02/mysql/lib/plugin --log-error=/u02/log/3308/error.log --open-files-limit=65535 --pid-file=/u02/run/3308/mysqld.pid --socket=/u02/run/3308/mysql.sock --port=3308
root 50929 0.3 9.1 679612 91068 ? Ssl 9 月 03 8:25 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 45144 0.1 0.0 0 0 ? S 07:52 0:00 [kworker/0:1]
root 44873 0.1 0.0 0 0 ? S 07:47 0:01 [kworker/0:3]
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 957 0.0 1.1 499804 11904 ? Ssl 8 月 31 4:45 /usr/bin/containerd
root 956 0.0 0.0 53284 556 ? Ss 8 月 31 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 954 0.0 0.2 251796 2780 ? Ssl 8 月 31 1:31 /usr/sbin/rsyslogd -n
root 953 0.0 0.0 112920 344 ? Ss 8 月 31 0:01 /usr/sbin/sshd -D
6. lsof
lsof is a tool for displaying open files in the current system in the Linux operating system. This tool is very useful in the operation and maintenance process.
1. View the process of occupying files
[root@192 ~]# lsof /u02/run/3308/mysql.sock
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 40333 mysql 19u unix 0xffff9cb72ba24400 0t0 443745 /u02/run/3308/mysql.sock
mysqld 40333 mysql 23u unix 0xffff9cb706041400 0t0 223567 /u02/run/3308/mysql.sock
mysqld 40333 mysql 26u unix 0xffff9cb72ba24000 0t0 443760 /u02/run/3308/mysql.sock
mysqld 40333 mysql 86u unix 0xffff9cb701f7e000 0t0 445068 /u02/run/3308/mysql.sock
mysqld 40333 mysql 87u unix 0xffff9cb73724e000 0t0 445235 /u02/run/3308/mysql.sock
2. View the process occupying the port
[root@192 ~]# lsof -i :3308
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 40333 mysql 22u IPv4 223566 0t0 TCP *:tns-server (LISTEN)
3. View the process of the directory being occupied, this is very useful when umount file system
[root@192 ~]# lsof +d /u02
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 36536 root cwd DIR 253,0 169 1002658 /u02
mysqld_sa 39322 mysql cwd DIR 253,0 46 67426672 /u02/conf
4. View the process in which the directory file is occupied
[root@192 ~]# lsof +D /u02
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 36536 root cwd DIR 253,0 169 1002658 /u02
mysql 38711 mysql txt REG 253,0 8142016 36148646 /u02/mysql/bin/mysql
mysql 38720 mysql txt REG 253,0 8142016 36148646 /u02/mysql/bin/mysql
mysqld_sa 39322 mysql cwd DIR 253,0 46 67426672 /u02/conf
mysqld_sa 39322 mysql 255r REG 253,0 28466 36258029 /u02/mysql/bin/mysqld_safe
mysqld 40333 mysql cwd DIR 253,0 267 1002659 /u02/data/3308
mysqld 40333 mysql txt REG 253,0 247739544 36258021 /u02/mysql/bin/mysqld
mysqld 40333 mysql 1w REG 253,0 278458 105389068 /u02/log/3308/error.log
mysqld 40333 mysql 2w REG 253,0 278458 105389068 /u02/log/3308/error.log
mysqld 40333 mysql 3u REG 253,0 35 116292537 /u02/log/3308/binlog/binlog.index
mysqld 40333 mysql 4uW REG 253,0 536870912 3341676 /u02/log/3308/iblog/ib_logfile0
mysqld 40333 mysql 9uW REG 253,0 536870912 3341677 /u02/log/3308/iblog/ib_logfile1
mysqld 40333 mysql 10uW REG 253,0 536870912 3341678 /u02/log/3308/iblog/ib_logfile2
mysqld 40333 mysql 11uW REG 253,0 536870912 3341679 /u02/log/3308/iblog/ib_logfile3
mysqld 40333 mysql 12uW REG 253,0 33554432 3341674 /u02/log/3308/iblog/ibdata1
mysqld 40333 mysql 13uW REG 253,0 83886080 3341675 /u02/log/3308/iblog/ibdata2
mysqld 40333 mysql 15uW REG 253,0 12582912 1002664 /u02/log/3308/iblog/ibtmp1
7. fuser
The fuser command is used to report the files and network sockets used by the process. Using this command, you can easily display the process number of the local process and what local process uses the file.
1. List all the process numbers and user names that access the /u02/run/3308/mysql.sock file
[root@192 ~]# fuser -u /u02/run/3308/mysql.sock
/u02/run/3308/mysql.sock: 40333(mysql)
2. List all access file system process numbers and user names
[root@192 ~]# fuser -u -m /home
/home: 36511c(jim) 36534c(root) 37220c(mysql) 37252c(jim) 37279c(root) 37301c(mysql) 38710c(mysql) 38711c(mysql) 38719c(mysql) 38720c(mysql) 40289c(jim) 40314c(root) 40366c(mysql) 40392c(mysql) 40393c(mysql) 40463c(jim) 40486c(root) 40508c(mysql) 40531c(mysql) 40532c(mysql) 43306c(jim) 43329c(root) 43394c(jim) 43419c(root)
3. Kill all processes that access the tps_mysql.sh file, and display the number of the process that was killed
[mysql@192 ~]$ fuser -ku /home/mysql/tps_mysql.sh
/home/mysql/tps_mysql.sh: 46741(mysql)
8. rpm
rpm command Linux operating system rpm package management tool
1. Install rpm package
[mysql@192 ~]$ rpm -ivh percona-xtrabackup-24-2.4.2-1.el7.x86_64
2. Uninstall the installation package
[mysql@192 ~]$ rpm -e percona-xtrabackup-24-2.4.2-1.el7.x86_64
3. Query the rpm installation situation
[mysql@192 ~]$ rpm -qa|grep -i percona
percona-xtrabackup-24-2.4.2-1.el7.x86_64
percona-toolkit-3.1.0-2.el7.x86_64
4. View the installation directory of the rpm package file
[mysql@192 ~]$ rpm -ql percona-toolkit-3.1.0-2.el7.x86_64
/usr/bin/pt-align
/usr/bin/pt-archiver
/usr/bin/pt-config-diff
/usr/bin/pt-deadlock-logger
/usr/bin/pt-diskstats
/usr/bin/pt-duplicate-key-checker
/usr/bin/pt-fifo-split
/usr/bin/pt-find
/usr/bin/pt-fingerprint
/usr/bin/pt-fk-error-logger
/usr/bin/pt-heartbeat
/usr/bin/pt-index-usage
9. Regular Expressions
Regular expression is a set of rules and methods defined for processing a large number of strings, and quickly search for what you want from a large number of strings. Regular expressions are generally processed in units of lines.
Precautions:
- Linux regular expressions are generally processed in units of behavior
alias grep='grep --color=auto'
Make matched content display color- Pay attention to the character set,
export LC_ALL=C
Regular expression parameters
parameter | Explanation |
---|---|
-E, --extended-regexp |
PATTERN is an extensible regular expression (abbreviated as ERE) |
-F, --fixed-strings |
PATTERN is a set of fixed-length strings separated by line breaks |
-e, --regexp=PATTERN |
Use PATTERN for matching operation pattern |
-f, --file=FILE |
Get PATTERN from FILE |
-i, --ignore-case |
Ignore case |
-w, --word-regexp |
Force PATTERN to match only words exactly |
-x, --line-regexp |
Force PATTERN to match exactly one line |
-z, --null-data |
A 0-byte data row, but not a blank row |
Miscellaneous (other options):
parameter | Explanation |
---|---|
-s 或 –no-messages` |
Do not display error messages |
-v 或 –invert-match` |
Negate |
Output control:
parameter | Explanation |
---|---|
-m, --max-count=NUM |
Stop at NUM matches |
-n, --line-number |
Show line number |
-H, --with-filename |
Display the file name of the matching result |
-h, --no-filename |
Do not display the file name of the matching result |
-o, --only-matching |
Show matching pattern |
-q, --quiet, --silent |
Quiet mode, without any general output |
-a, --text |
Equivalent to --binary-files=text |
-d, --directories=ACTION |
How to operate the directory, ACTION is'read','recurse', or'skip' |
-D, --devices=ACTION |
How to operate the equipment |
-R, -r, --recursive |
Directory recursion, equivalent to --directories=recurse |
--include=FILE_PATTERN |
Only match the file indicated by the expression |
--exclude=FILE_PATTERN |
Exclude files represented by expressions |
--exclude-from=FILE |
Read the file or directory to be excluded from the file |
--exclude-dir=PATTERN |
Get the directory to be excluded from the expression |
-L, --files-without-match |
Print out the file name that does not match |
-l, --files-with-matches |
Print out the file name that matches the content |
-c, --count |
Print out the number of times each file matches the content |
-Z, --null |
Do not print the file after the file name |
document Control:
parameter | Explanation |
---|---|
-B, --before-context=NUM | Print out the first NUM lines of matching content |
A, --after-context=NUM | Print out NUM lines after matching content lines |
-C, --context=NUM | How many lines are printed |
–color[=WHEN] | Color highlight |
–colour[=WHEN] | use markers to highlight the matching strings;WHEN is ‘always’, ‘never’, or ‘auto’ |
egrep ie grep -E
. fgrep ie grep -F
.
Tip: Regular expressions generally use single quotation marks. If variables are quoted inside, use double quotation marks.
10. Use of grep
- Find the line starting with r
root@shell1:~# grep '^r' /etc/passwd
root:x:0:0:root:/root:/bin/bash
- Find the line ending with login
root@shell1:~# grep 'login$' /etc/passwd
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
- Match one of all uppercase letters
root@shell1:~# grep '[[:upper:]]' /etc/passwd
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
- Match lines starting with rb
root@shell1:~# grep '^[rb]' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:2:2:bin:/bin:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
- Match lines where o appears twice
root@shell1:~# grep 'o\{2\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
- Match 1 to 2 lines with o
root@shell1:~# grep 'o\{1,2\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
- A line with more than two occurrences of o
root@shell1:~# grep 'o\{2,\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
- Matches 0-9 digits that appear more than 3 times
root@shell1:~# grep '[0-9]\{3,\}' /etc/passwd
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false
- Find the root and sys lines
root@shell1:~# grep -E 'root|sys' /etc/passwd
root:x:0:0:root:/root:/bin/bash
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false
- Find the lines that start with root and sys
root@shell1:~# grep -E '^(root|sys)' /etc/passwd
root:x:0:0:root:/root:/bin/bash
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false
- Find the rows with 7 or 9 on both sides
root@shell1:~# cat /etc/passwd | grep -E ':(7|9):'
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
- View the contents of the specified file /etc/logrotate.conf, no comment lines and blank lines are displayed
cat /etc/logrotate.conf | grep -Ev '^#|^$'
- Adding
-c
parameters to print out the number of times each file to match the content
root@shell1:~# grep -c 'root' /etc/passwd #因为只匹配到一行,所以只输出 1
1
- Join
-w
parameters only for the exact match, if not the same word is not output
root@shell1:~# grep -w 'bin' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin