Common Linux Commands (V)
Today, we continue to summarize the find command of linux. The find command is often used with the other two commands exec and xargs, so let's see how they actually use it today.
1 exec command
- The exec parameter is followed by the command command, with';'as the end sign, and before';', add''. Considering the different meanings of semicolons in each system, the backslash is added in front.
1.1 grammar
find [file directory]... - exec [commond] {}
1.2 command parameters
1.3 Command Example
-
Execute ls command with the - exec option
[root@ERICSSON Test]# [root@ERICSSON Test]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log test11 [root@ERICSSON Test]# find . -type f -name "*log" -exec ls -ld {} \; -rw-r--r--. 1 root root 0 Apr 18 11:40 ./1.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./2.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./3.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./4.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./5.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./6.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./7.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./8.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./9.log -rw-r--r--. 1 root root 0 Apr 18 11:40 ./10.log
-
Find the target file in the directory and execute the rm deletion command with the - exec option
[root@ERICSSON Test]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log test11 [root@ERICSSON Test]# find . -type f -name "*log" -exec rm {} \; [root@ERICSSON Test]# ls test11 [root@ERICSSON Test]#
note: Be careful when using rm commands. It's best to use interactive mode to add parameters - i
[root@ERICSSON Test]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log test11 [root@ERICSSON Test]# find . -type f -name "*log" -exec rm -i {} \; rm: remove regular empty file './1.log'? y rm: remove regular empty file './2.log'? y rm: remove regular empty file './3.log'? y rm: remove regular empty file './4.log'? y rm: remove regular empty file './5.log'? y rm: remove regular empty file './6.log'? y rm: remove regular empty file './7.log'? y rm: remove regular empty file './8.log'? y rm: remove regular empty file './9.log'? y rm: remove regular empty file './10.log'? y [root@ERICSSON Test]# ls test11
-
Find files to move to the specified directory
[root@ERICSSON /]# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys Test tmp usr var [root@ERICSSON /]# cd Test/ [root@ERICSSON Test]# ls Test01 [root@ERICSSON Test]# cd Test01/ [root@ERICSSON Test01]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log [root@ERICSSON Test01]# cd .. [root@ERICSSON Test]# cd Test01/ [root@ERICSSON Test01]# find . -name "*.log" -exec mv {} .. \; [root@ERICSSON Test01]# ls [root@ERICSSON Test01]# cd .. [root@ERICSSON Test]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log Test01 [root@ERICSSON Test]#
2 xargs command
The xargs command has two advantages over the exec command.
Firstly, compared with exec command, xargs processes part of the file and continues to run, instead of executing all the files like exec. In some systems, the length of commands that can be passed to exec is limited, so that after several minutes of execution of the find command, an overflow error will occur. Error messages are usually "parameter column is too long" or "parameter column overflow".
Secondly, in some systems, using the - exec option will initiate a corresponding process to process every matched file, not all matched files will be executed as parameters at one time; in some cases, there will be too many processes, system performance degradation, and therefore inefficiency; while using xargs command, there will be only one process.
2.1 grammar
somecommand |xargs -item command
2.2 Command Parameters
- a file is read from a file as sdtin - p Ask the user once every time an argument ation is executed. - Num is followed by a number of times to indicate the number of argument s used at one time when the command is executed, and all are used by default. - t means to print the command first and then execute it. - I or - I, depending on linux support, assign each name of xargs to {} one line at a time, which can be replaced by {}. - The maximum number of characters on the s num command line refers to the maximum number of characters on the command line after xargs. - L num reads num from standard input once and sends it to command command. -l and -L. - The d delim separator, the default xargs separator is carriage return, and the argument separator is a space. Here, the xargs separator is modified.
2.3 Command Example
-
Define a test file with multiple lines of text data in it and turn it into a single line output
[root@ERICSSON Test]# cat test.txt a b c d e f g h i j k l m n o p q r s t u v w x y z [root@ERICSSON Test]# cat test.txt | xargs a b c d e f g h i j k l m n o p q r s t u v w x y z [root@ERICSSON Test]#
-
Limit the number of parameters read by xargs with-n parameter
[root@ERICSSON Test]# cat test.txt a b c d e f g h i j k l m n o p q r s t u v w x y z [root@ERICSSON Test]# cat test.txt | xargs -n3 a b c d e f g h i j k l m n o p q r s t u v w x y z [root@ERICSSON Test]#
-
- The d option allows you to customize a delimiter
# echo "nameXnameXnameXname" | xargs -dX -n2 name name name name //Combined with the - n option, use the ___________ # echo "nameXnameXnameXname" | xargs -dX -n2 name name name name
-
Find a common file in the system and test its file type with xargs command
[root@ERICSSON Test]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log [root@ERICSSON Test]# find . -type f -print | xargs file ./1.log: empty ./2.log: empty ./3.log: empty ./4.log: empty ./5.log: empty ./6.log: empty ./7.log: empty ./8.log: empty ./9.log: empty ./10.log: ASCII text [root@ERICSSON Test]#
-
Find files with read/write/executable in the current directory and modify the corresponding permission Chmod (the command will be explained later)
[root@ERICSSON Test]# ll total 4 -rw-r--r--. 1 root root 8 Apr 18 14:25 10.log -rw-r--r--. 1 root root 0 Apr 18 12:04 1.log -rw-r--r--. 1 root root 0 Apr 18 12:04 2.log -rw-r--r--. 1 root root 0 Apr 18 12:04 3.log -rw-r--r--. 1 root root 0 Apr 18 12:04 4.log -rw-r--r--. 1 root root 0 Apr 18 12:04 5.log -rw-r--r--. 1 root root 0 Apr 18 12:04 6.log -rw-r--r--. 1 root root 0 Apr 18 12:04 7.log -rw-r--r--. 1 root root 0 Apr 18 12:04 8.log -rw-r--r--. 1 root root 0 Apr 18 12:04 9.log [root@ERICSSON Test]# find . -type f -print | xargs chmod 777 [root@ERICSSON Test]# ll total 4 -rwxrwxrwx. 1 root root 8 Apr 18 14:25 10.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 1.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 2.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 3.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 4.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 5.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 6.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 7.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 8.log -rwxrwxrwx. 1 root root 0 Apr 18 12:04 9.log
-
Combining grep command (explained after the pipe character) to filter out the specified proper noun
[root@ERICSSON Test]# ls 10.log 1.log 2.log 3.log 4.log 5.log 6.log 7.log 8.log 9.log [root@ERICSSON Test]# echo 'shenwei is a good person'>10.log [root@ERICSSON Test]# cat 10.log shenwei is a good person [root@ERICSSON Test]# find . -type f -print | xargs grep "shen" ./10.log:shenwei is a good person [root@ERICSSON Test]#