Home » 2014 » December » 24 » XARGS command in Linux with examples

6:41 PM
XARGS command in Linux with examples

XARGS - build and execute command lines from standard input or on the output of previous command

 

FORMAT
       xargs  [-0prtx]  [-E  eof-str]  [-e[eof-str]]  [--eof[=eof-str]]  [--null]  [-d delimiter] [--delimiter delimiter] [-I replace-str] [-i[replace-str]] [--replace[=replace-str]] [-n max-args] [--max-args=max-args]   [--arg-file=file] [command [initial-arguments]]


 

DESCRIPTION
       xargs reads items from the standard input line by line, delimited by blanks (which can  be protected  with  double  or  single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. 

Blank lines on the standard input are ignored.

In simple words, if we want to perform certain operations/commands on each line of output of a previous commands, we can use xargs next to that command using Pipe operator.

 

  •  Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic;  filenames  containing  blanks and/or  newlines  are incorrectly processed by xargs.
  •   In these situations it is better to use the `-0' option, which prevents such problems.  See example for this below.
  • When using this option you will need to ensure that the program which produces the input for xargs  also  uses  a  null   character as a separator. 

Options with xargs

--null, -0
              Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). 

Disables the end of file string, which is treated like  any  other  argument.   Useful  when
input items might contain white space, quote marks, or backslashes. 

 --replace[=replace-str], -i[replace-str]
              This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise.  This option is  deprecated; use -I instead.

 

 --max-args=max-args, -n max-args
              Use at most max-args arguments per command line.  Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.



xargs command examples


1. Copy all images to external hard-drive
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory


2. Search all jpg images in the system and archive it.
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz


3. Download all the URLs mentioned in the url-list.txt file
# cat url-list.txt | xargs wget –c

#> cat url-list.txt | xargs wget -c
--15:36:10-- 
http://www.google.com/
           => `index.html'
Resolving
www.google.com... failed: Name or service not known.
--15:36:10-- 
http://www.facebook.com/
           => `index.html'
Resolving
www.facebook.com... failed: Name or service not known.

FINISHED --15:36:10--
Downloaded: 0 bytes in 0 files

4. Find files named core in or below the directory /tmp and delete them. 
       find /tmp -name core -type f -print | xargs /bin/rm -f

Note:- The above command will work incorrectly if there are any filenames containing newlines or spaces. That is why we are using "-0" option in the next command:

 

5. Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.
       find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

6. Generates a compact listing of all the users on the system.            

       cut -d: -f1 < /etc/passwd | sort | xargs echo

7. Suppose you want to check the file type of each file present in current directory.

  #ls|xargs file;


8. Make a long listing of files listed in a filename

#cat filename|xargs ls -l
ls: first: No such file or directory
ls: 2nd: No such file or directory
ls: 3rd: No such file or directory
ls: shankar: No such file or directory
ls: shankar: No such file or directory
ls: four: No such file or directory
ls: line: No such file or directory
ls: four: No such file or directory
ls: line: No such file or directory
ls: four: No such file or directory
ls: sjhdfshdfsa: No such file or directory
ls: five: No such file or directory
ls: five: No such file or directory


EXIT STATUS
xargs exits with the following status:
0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.

 
 

Category: Open System-Linux | Views: 1187 | Added by: shanky | Tags: how to perform operation on the out, linux, xargs command in unix, unix, xargs, xargs command in Linux, shanky's portal, xargs comman examples | Rating: 5.0/1

Related blogs


You may also like to see:


[2015-06-19][Open System-Linux]
How to create swap file in Linux : Adding extra swap space
[2014-11-19][Open System-Linux]
ETHTOOL : A tool in Linux to display or change ethernet card setting
[2014-10-06][Open System-Linux]
Time command in Linux to give resource usage
[2014-03-12][Open System-Linux]
uptime command in Linux
[2016-03-17][Open System-Linux]
Some amazing command shortcuts in Linux

Total comments: 0
avatar