Xargs and find are often used in combination to perform file operations. When you delete logs, you just get used to deleting them, such as # find. -typef-name *. log | xargsrm-rf * deletes the file ending with log. If I want to move or copy the file, I need to replace it with parameters. Xargs-I parameters or-I parameters can be used together with {} to perform the xarg
Shell basics 2: Search Skills, use of find and xargs
Execute shell commands using exec or OKWhen using find, you only need to write the desired operation in a file, you can use exec to search with find, which is very convenient.(In some operating systems, only the-e x e c option is allowed to execute commands such as l s or LS-l ). Most users use this option to find and delete old files. Before executing the r m
Xargs commandXargs is a filter that passes parameters to the command, and is also a tool for combining multiple commands . It divides a data stream into small enough chunks to facilitate the processing of filters and commands. Typically, Xargs reads data from a pipe or stdin, but it can also read data from the output of a file. The default command for Xargs is ec
Xargs and exec are the filters passed by commands. They capture the results of the previous command and pass it to the next command. Simply put:
-Exec must be combined{}\;And {} indicates the Result \; Terminator of the previous query. This is a fixed method.
Xargs must be combined with | to process files in batches at a faster speed.
I did a small experiment to count the Linux system, and list the fi
1. All three commands can be used to make the preceding output a subsequent input.2. They have different processing methods for the previous output.Find. -name "*.sh"-exec cat {} \;Find. -name "*.sh" |catFind. -name "*.sh" |xargs CatExample one:[email protected] temp]# cat ex.sh a.txtb.txtc.txtd.txt5. Txt[[email protected] temp]# find.-name"ex.sh"-exec Cat {} \;a.txtb.txtc.txtd.txt5. Txt[[email protected] temp]# find.-name"ex.sh"|Cat./Ex.sh[[email pro
One, the demand for the query file named Zrsh beginning of time for July to date all files and packaging1. The first thought is the Find + Xargs + CP format.Find 2016073*-type f-name *zrsh\* |xargs cp {}./data/After the execution found that the query file is not a directory, very surprised, so directly execute find 2016073*-type f-name *zrsh\* files are queried out without any problems.2. Speculation is the
Today, when writing a shell script, I want to use a CD to enter the Xargs pipeline output directory, but prompted an error. Details are as follows:"Environment description"Directory structure:[Email protected] nagiosclient_db]# ls-ltrTotal 2488-rw-r--r--. 1 root root 405725 Mar 3 14:12 nrpe-2.12.tar.gz-rw-r--r--. 1 root root 2095419 Mar 3 14:12 nagios-plugins-1.4.15.tar.gzDrwxrwxrwx. 501 root 4096 Mar 3 14:36 nagios-plugins-1.4.15Drwxrwxr-x. 7 4096 Ma
also archived.DMESG: Display boot information for the system/VAR/LOG/DMESG: The hardware log recorded when the system was startedgrep ' 126.com '/var/log/maillogSearch for characters related to 126.com in the/var/log/maillog file3. Exec and XargsBoth exec and find useFind files with the current directory creation time greater than 10 days and deleteFind. -mtime +10-exec rm-rf {} \;Bulk Change file name: Find./*-exec mv {} {}_bak \;Xargs used behind t
######################################################################################################find ...-exec rm {} \;The find command can be found to delete the results, the difference is simply the former is to find the results of the search to the EXEC option, so when the number of files, there may be "too many parameters" error.-exec must be made up of one; End, and because usually the shell will be right; For processing, so use \; Prevent this situation. {} may need to write ' {} ', a
Method 1:
Maven is used to build an application in the past two days, there are more than 700 pom. XML file, even more depressing is in many pom. the URL of the resource library is specified separately in the XML file. I need to uniformly specify the URLs of these resource libraries to the local central nexus library. it is not practical to manually change the file configurations one by one, So Google will find a good way to replace the file content in batches. The command structure is as
Get ready:Cat Test.txt 1 3 4Example:Cat Xargs 1 3 4You can see that the resulting string is separated by a space.Separate the above string with a comma, you can use the TR command to replace the spaceCat Xargs TR ' ' ' , 'However, the TR command can only replace a single character, and multiple characters cannot be implemented.Use SED to implement the substitution of multiple strings:Cat
Tags: xargsxargs 将标准输入转换成命令行参数Syntax format: xargs "option" xargs [options]Note: There must be at least one space between the Xargs command and the following options and optionsOption Description:
parameter Options
Explanatory notes
-N
Specify the maximum number of parameters per row n, you can divide the standard
Xargs used in bulk operations behind pipe symbolsFind. -mtime +10 |xargs RM-RFls-d./|xargs-n1-i{} mv {} {}_bakBoth exec and find useFind files with the current directory creation time greater than 10 days and delete: Find. -mtime +10-exec RM-RFBatch Change file name: Find./-exec mv {} {}_bakXargs can turn multiple lines into a row of cat 1.txt|xargsScreen is the
-exec1. The parameter is a pass-through, passing a parameter once to execute the RM2. File names can also be handled with special characters such as spaces-xargs1. Once the parameters are passed to the command, you can use the-N to control the number of parameters2. Special file names need to be handled in the following ways:Find. -name "*.txt" Print0 |xargs-0 rm {}find/opt/test/-type f-name "*.txt"-exec CP {}/tmp \;find/opt/test/-type f-name "*.txt"
command: Find Parameters:-type:f File D Directory-name: File or directory name-exec: What action to perform on the found result.e.g. find. -type f-name ' abc.txt ' -exec {} \; {} represents what was previously found e.g. find. -type f-name ' *.txt ' | Xargs rm-f * is a wildcard characterUse * As a wildcard character, such as Find results for a.txt B.txtUse the pipe to find the results of the RM delivery, using
Background:Cat Test.txt 1 3 4Instance:1, XargsCat Xargs Echo {}Cat Xargs Echo {}The case I parameter is not the same effect, the lowercase i default parameter is {}, and cannot be changed, and the uppercase I parameter is custom, can be {}, a ... Wait a minute.2. awkAwk is becoming a language, and {} code blocks can be written using the shell, which is equivalent to a dynamic language.Cat awk ' {print $} '$
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.