10 xargs command example in Linux

Source: Internet
Author: User
Reprinted: http://javarevisited.blogspot.com/2012/06/10-xargs-command-example-in-linux-unix.html

In order not to look over the wall, reposted here.

10 xargs command example in Linux-Unix tutorial Xargs command in UNIXOr Linux is a powerful Command Used in conjunction

Find and
Grep command in UNIX to divide a big list of arguments into small list stored ed from standard input. find and grep command produce long list of file names and we often want to either remove them or do some operation on them but have UNIX Operating System
Doesn't accept such a long list of argument. UNIX xargs command divide that list into sub-listWith acceptable length and made it work. This

UNIX tutorial is in continuation of my earlier post on UNIX like
10 examples of chmod command in UNIX and
How to update soft link in Linux. if you haven't read those unit tutorial than check them out. by the way in this tutorial we will see different example of UNIX xargs command to learn how to use xargs command with find and grep and other Unix Command and
Make most of it. though what you can do with xargs in UNIX can also be done by using options provided in find but believe xargs is much easy and powerful.


UNIX xargs command exampleFollowing is list of examples of xargs command which shows how useful knowledge of xargs can be. feel free to copy and use this command and let me know if it didn't work in any specific UNIX operating system like Aix or Solaris.Xargs Example 1-with and without xargsIn this example of xargs command we will see how output changes with use of xargs command in Unix or Linux. Here is the output

Find command without xargs first and than with xargs, you can clearly see that multiline output is converted into single line: devuser@System:/Etc
Find.
-Name "* bash *"
./Bash. bashrc
./Bash. bash_logout
./Ults/Etc/Bash. bashrc
./Ults/Etc/Bash. bash_logout
./Ults/Etc/SKEL/. Bashrc
./Ults/Etc/SKEL/. Bash_profile
./Postinstall/Bash. Sh. Done
./Setup/Bash.lst.gz
./SKEL/. Bashrc
./SKEL/. Bash_profile

Devuser@System:/Etc
Find.
-Name "* bash *"|
Xargs

./Bash. bashrc./Bash. bash_logout./Ults/Etc/Bash. bashrc
./Ults/Etc/Bash. bash_logout./Ults/Etc/SKEL/. Bashrc
./Ults/Etc/SKEL/. Bash_profile./Postinstall/Bash. Sh. Done
./Setup/Bash.lst.gz./SKEL/. Bashrc./SKEL/. Bash_profileXargs Example 2-xargs and grepAnother common use fo UNIX xargs command is to first find the files and then look for specific keyword on that file using grep command. Here is
Example of xargs commandThat does itFind.
-Name "*. Java"| Xargsgrep"Stock" This will first find all java files from
Current directory or below and than on each Java file look for word "stocks" if you have your development environment setup in Linux or Unix this is a great tool to find function references or class references on java files.Xargs Example 3-delete temporary file using find and xargsAnother common example of xargs command in UNIX is removing temporary files from system.Find/TMP
-Name "*. tmp"| XargsrmThis will remove all. tmp file from/tmp or below directory.Xargs in UNIX is very fastAs compared to deleting single file at a time which can also be done by using find command alone. By the way this is also a very

Popular UNIX interview question.Xargs Example 4-xargs-0 to handle Space in file nameAbove
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-print0 to produce null separated file name and xargs-0 to handle null separated items. here is an example
Xargs command in UNIX which can handle file name with spaces and newline:Find/TMP
-Name "*. tmp"-print0| Xargs-0RmXargs Example 5-xargs and cut command in UNIXThough 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 cut command. For using cut command let's first create a. CSV file with some data e. g. devuser@System:/Etc
CatSmartphones.csv
IPhone, iphone4s
Samsung, Galaxy
LG, Optimus
HTC, 3 dnow we will display name of mobile companies from first column using
Xargs command
In one line: devuser@System:/Etc
Cut-D,
-F1 smartphones.csv|
Sort | xargs

HTC iPhone LG SamsungXargs Example 6-command convert Muti line output into single lineOne more common example of xargs commands in Linux is by 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. devuser@System :~/Perl
Ls
-1
*. Txt
Derivatives.txt
Futures.txt
Fx.txt
Options.txt
Stock.txt
Swaps.txt

Devuser@System :~/Perl
Ls
-1
*. Txt|
Xargs

Derivatives.txt futures.txt fx.txt options.txt stock.txt swaps.txtXargs Example 7-Counting Number of lines in each file using xargs and find.In this example of xargs command in UNIX we will combine "WC"Xargs and find to count number of lines in each file, Just like we did in our previous example with grep where we tried to find specific word in each
Java file. devuser@System :~/Perl
Ls
-1
*. Txt|
Xargs WC

-L
0 derivatives.txt
2 futures.txt
0 fx.txt
1 options.txt
3 stock.txt
0 swaps.txtXargs command Example 7-passing subset of arguments to xargs in Linux. Some commands in UNIX can only work at certain number of argument e.g. Diff Command needs two argument. When used with xargs you can use flag "-n" to instruct xargs on
How many argumentIt shoshould pass to given command. This
Xargs command line option
Is extremely useful on certain situation like repeatedly doing diff etc. xargs in Unix or Linux will continue to pass argument in specified number until it exhaust all input. here is an example of UNIX xargs command with Limited
Argument: devuser@System :~/Perl
Ls
-1
*. Txt|
Xargs
-N
2Echo
Derivatives.txt futures.txt
Fx.txt options.txt
Stock.txt swaps.txt in this example, xargs is passing just two files at a time to echo as specified
"-N 2"Xargs command line option.Xargs Example 9-Avoid "argument list too long"Xargs in Unix or Linux was initially use to avoid "argument list too long" errors and by using xargs you send sub-list to any command which is shorter than "arg_max" and that's how xargs avoid "argument list too long" error. you can see current value
"Arg_max" by using getconf arg_max. normally xargs own limit is much smaller than what modern system can allow, default is 4096. you can override xargs sub list limit by using "-s" command line option.Xargs Example 10-find-exec vs find + xargsXargs with find command isMuch faster than using-ExecOn find. Since-exec runs for each file while xargs operates on sub-list level. To give an example if you need to change permission of 10000 files xargs with find will be almost 10 K
Time faster than find with-exec because xargs change permission of all file at once. For more examples of find and xargs see my post

10 frequently used find command examples in LinuxImportant points on xargs command in UNIX and LinuxNow let's revise some important points about xargs command in UNIX which is worth remembering: 1. an important point to note about xargs is that it doesn't handle files which has newlines or white space in its name and to avoid this problem one shoshould always use "xargs-0 ". xargs-o change separator to null character so its important that input
Feed to xargs is also use null as separator. for example GNU find command use-print0 to produce null separated file names.2. xargs command replace es command from standard input which is by default separated with space or newlines andexecute those commands, you can still use double quotes or single quote to group commands.3. if you don't give any command to xargs in UNIX, default Command executed by xargs is/bin/ECHO and it will simply display file names.4. one rare problem with xargs isEnd of file string, By default end of file string is "_" and if this string occurs in input the rest of input is ignored by xargs. though you can change end of file string by using option "-EOF ". 5. use man xargs orXargs -- HelpTo get help on xargs online while working in Unix or Linux. In shortXargs command in Unix or LinuxIs an essential tool which enhances functionality of Front Line commands like find, grep or cut and gives more power to your shell script. These xargs command examples are good start for anyone wants
To learn more about xargs command. though these xargs examples are tested in Linux environment they will applicable for other UNIX systems likes Solaris or AIX also. let us know if you face any issue while using these examples. otherLinux Command tutorialsFrom javarevisited blog: how to work faster and efficient in UNIX environmenthow to terminate process using kill command in unixsort command in Unix-5 examplestar command examples in UNIX and linuxhow to calculate size of directory in UNIX and linuxbasics of File and directory permissions in UNIX Operating System

Read more:

Http://javarevisited.blogspot.com/2012/06/10-xargs-command-example-in-linux-unix.html#ixzz2OKE7sC53

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.