xargs - Unix, Linux Command

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.   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.  If that program is
GNU
find for example, the ‘-print0’ option does this for you.


If any invocation of the command exits with a status of 255,
xargs will stop immediately without reading any further input.  An error
message is issued on stderr when this happens.





find /tmp -name core -type f -print | xargs /bin/rm -f 





find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f 

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.



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




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.


Exit codes greater than 128 are used by the shell to indicate that
a program died due to a fatal signal.


The -l and -i options appear in the 1997 version of the POSIX
standard, but do not appear in the 2004 version of the standard.
Therefore you should use -L and -I instead, respectively.




It is not possible for
xargs to be used securely, since there will always be a time gap between the
production of the list of input files and their use in the commands
that
xargs issues.  If other users have access to the system, they can manipulate
the filesystem during this time window to force the action of the
commands
xargs runs to apply to files that you didn’t intend.  For a more detailed
discussion of this and related problems, please refer to the
‘‘Security Considerations’’ chapter in the findutils Texinfo
documentation.  The -execdir option of
find can often be used as a more secure alternative.


When you use the -I option, each line read from the input is buffered
internally.   This means that there is an upper limit on the length
of input line that
xargs will accept when used with the -I option.  To work around this
limitation, you can use the -s option to increase the amount of
buffer space that
xargs uses, and you can also use an extra invocation of
xargs to ensure that very long lines do not occur. 
For example:


somecommand | xargs -s 50000 echo | xargs -I ’{}’ -s 100000 rm ’{}’ 

Here, the first invocation of
xargs has no input line length limit
because it doesn’t use the -i option.  The second invocation of
xargs does have such a limit, but we have ensured that the it never encounters
a line which is longer than it can handle.   This is not an ideal
solution.  Instead, the -i option should not impose a line length
limit, which is why this discussion appears in the BUGS section.
The problem doesn’t occur with the output of
find(1)
because it emits just one filename per line.


The best way to report a bug is to use the form at
http://savannah.gnu.org/bugs/?group=findutils. 
The reason for this is that you will then be able to track progress in
fixing the problem.   Other comments about xargs(1) and about
the findutils package in general can be sent to the
bug-findutils mailing list.  To join the list, send email to
bug-findutils-request@gnu.org. 










Advertisements


    129 Lectures 
     23 hours 

 Eduonix Learning Solutions

    5 Lectures 
     4.5 hours 

 Frahaan Hussain

    35 Lectures 
     2 hours 

 Pradeep D

    41 Lectures 
     2.5 hours 

 Musab Zayadneh

    46 Lectures 
     4 hours 

 GUHARAJANM

    6 Lectures 
     4 hours 

 Uplatz
 Print
 Add Notes
 Bookmark this page