Source From Here
Preface
xargs is a just like "awk" ,"find" & "grep" commands processes the standard input on all unix flavoured operating sysems. Basically "xargs" is used to remove or do some operation on long list of file names which were produced by "find" & "grep" commands.
Usually many UNIX operating system doesn't accept such a long list of argument.UNIX xargs command divide that list into sub-list with acceptable length and made it work. For example I'd like to find out all *.sh file located in 100s of sub-directories and move them to another directory called ~/back.scripts. How do I use command line args with xargs to achieve the same?
as per man page "xargs" is used to execute a command, passing constructed argument list(s). The arguments are typically a long list of filenames (generated byls or find etc) that are passed to xargs via a pipe.
Some features:
Exit Status
This command returns the following exit values:
Examples
Find all the .conf files under /etc/ and pass to the ls command, -print0 is required if any filenames contain whitespace.:
Find all files in the work folder, pass to grep and search for 'profit':
{} as the argument list marker
{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .sh files in or below the current directory and move them to ~/.old.files directory:
You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
Where,
10 Popular "XARGS" Command Examples:
1) With& Without "xargs" observation:
In this example of xargs command we will see how output changes with use of xargs command in unix or Linux. Here is the output of find command withoutxargs first and than with xargs, you can clearly see that multiline output is converted into single line:
2) Xargs with grep:
When you use "xargs" in conjusction with find and grep , the grep will look for the specific word in each file in the from the standard input.
In the above example first find all .sh files from current directory or below and than on each .sh file look for word "ksh".
3) Covert muti-line output into single line
Best example of xargs is converting output of one command into one line. For example you can run any command and then combine xargs to convert output into single line. here is an example xargs in unix which does that.
4) To Delete temporary files using xargs & find:
Another common example of xargs command in unix is removing temporary files from system.
This will remove all .tmp file from /tmp or below directory. xargs in unix is very fast as compared to deleting single file at a time which can also be done by usingfind command alone.
5) xargs -0 to handle space in file name
Above example of xargs command in unix will not work as expected if any of file name contains space or new line on it. To avoid this problem we use find -print0to produce null separated file name and xargs -0 to handle null separated items. Here is an example of xargs command in unix which can handle file name with spaces and newline:
6) Counting number of lines/words/characters in each file using xargs & find:
"find" in conjuction with "xargs" and "wc" we can count number of lines/words/characters in each file under a particular directory.
Note: you can use '-c' & '-w' with wc to obtain number of characters and words respectively.
7) xargs and cut command in Unix:
Though most of xargs examples in unix will be along with find and grep command but xargs is not just limited to this two it can also be used with any command which generated long list of input for example we can use xargs with cut command in unix. In below example of unix xargs we will xargs example with cutcommand. for using cut command let's first create a test file with some data e.g.
8) To insert file names into the middle of command lines, type:
This command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where {} (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:
// -t, --verbose: Print the command line on the standard error output before executing it.
9) To run a command on files that you select individually, type:
This command sequence allows you to select files to add to the lib.a library. The -p flag tells the xargs command to display each ar command it constructs and to ask if you want to run it. Type y to run the command. Press the any other key if you do not want to run the command.
10) To construct a command that contains a specific number of arguments and to insert those arguments into the middle of a command line, type:
Supplement
* 10 Xargs Command Examples in Linux / UNIX
Preface
xargs is a just like "awk" ,"find" & "grep" commands processes the standard input on all unix flavoured operating sysems. Basically "xargs" is used to remove or do some operation on long list of file names which were produced by "find" & "grep" commands.
Usually many UNIX operating system doesn't accept such a long list of argument.UNIX xargs command divide that list into sub-list with acceptable length and made it work. For example I'd like to find out all *.sh file located in 100s of sub-directories and move them to another directory called ~/back.scripts. How do I use command line args with xargs to achieve the same?
as per man page "xargs" is used to execute a command, passing constructed argument list(s). The arguments are typically a long list of filenames (generated byls or find etc) that are passed to xargs via a pipe.
Some features:
Exit Status
This command returns the following exit values:
Examples
Find all the .conf files under /etc/ and pass to the ls command, -print0 is required if any filenames contain whitespace.:
Find all files in the work folder, pass to grep and search for 'profit':
{} as the argument list marker
{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .sh files in or below the current directory and move them to ~/.old.files directory:
You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
Where,
10 Popular "XARGS" Command Examples:
1) With& Without "xargs" observation:
In this example of xargs command we will see how output changes with use of xargs command in unix or Linux. Here is the output of find command withoutxargs first and than with xargs, you can clearly see that multiline output is converted into single line:
2) Xargs with grep:
When you use "xargs" in conjusction with find and grep , the grep will look for the specific word in each file in the from the standard input.
In the above example first find all .sh files from current directory or below and than on each .sh file look for word "ksh".
3) Covert muti-line output into single line
Best example of xargs is converting output of one command into one line. For example you can run any command and then combine xargs to convert output into single line. here is an example xargs in unix which does that.
4) To Delete temporary files using xargs & find:
Another common example of xargs command in unix is removing temporary files from system.
This will remove all .tmp file from /tmp or below directory. xargs in unix is very fast as compared to deleting single file at a time which can also be done by usingfind command alone.
5) xargs -0 to handle space in file name
Above example of xargs command in unix will not work as expected if any of file name contains space or new line on it. To avoid this problem we use find -print0to produce null separated file name and xargs -0 to handle null separated items. Here is an example of xargs command in unix which can handle file name with spaces and newline:
6) Counting number of lines/words/characters in each file using xargs & find:
"find" in conjuction with "xargs" and "wc" we can count number of lines/words/characters in each file under a particular directory.
Note: you can use '-c' & '-w' with wc to obtain number of characters and words respectively.
7) xargs and cut command in Unix:
Though most of xargs examples in unix will be along with find and grep command but xargs is not just limited to this two it can also be used with any command which generated long list of input for example we can use xargs with cut command in unix. In below example of unix xargs we will xargs example with cutcommand. for using cut command let's first create a test file with some data e.g.
8) To insert file names into the middle of command lines, type:
This command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where {} (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:
// -t, --verbose: Print the command line on the standard error output before executing it.
9) To run a command on files that you select individually, type:
This command sequence allows you to select files to add to the lib.a library. The -p flag tells the xargs command to display each ar command it constructs and to ask if you want to run it. Type y to run the command. Press the any other key if you do not want to run the command.
10) To construct a command that contains a specific number of arguments and to insert those arguments into the middle of a command line, type:
Supplement
* 10 Xargs Command Examples in Linux / UNIX
沒有留言:
張貼留言