Xargs command usage [not translated]

Source: Internet
Author: User

Address: http://www.unixreview.com/documents/s=8274/sam0306g/

Author: Ed Schaefer

(July 2003-see Web-exclusive update
End of article)

Using UNIX javassionals think the xargs command, construct and
Execute argument lists, is only useful for processing long lists
Files generated by the find command. While xargs dutifully serves this
Purpose, xargs has other uses. In this article, I describe xargs and
Historical "Too implements arguments" problem, and present eight xargs
"One-liners ":

  • Find the unique owners of all the files in a directory.
  • Echo each file to standard output as it deletes.
  • Duplicate the current directory structure to another directory.
  • Group the output of multiple UNIX commands on one line.
  • Display to standard output the contents of a file one word per line.
  • Prompt the user whether to remove each file individually.
  • Concatenate the contents of the files whose names are contained in
    File into another file.
  • Move all files from one directory to another directory, echoing each
    Move to standard output as it happens.


Examining the "too your arguments" Problem

In the early days of UNIX/XENIX, it was easy to overflow
Command-line buffer, causing a "Too writable arguments" failure. Finding
Large number of files and piping them to another command was enough
Cause the failure. Executing the following command, from UNIX power
Tools, first edition (O 'Reilly & Associates ):

Pr-n' find.-Type F-mtime-1
-Print '| LPR

Will potentially overflow the command line given enough files. This
Command provides a list of all the files edited today to PR, and pipes
PR's output to the printer. We can solve this problem with xargs:

Find.-Type F-mtime-1-print | xargs PR
-N | lp

With no options, xargs reads standard input, but only writes enough
Arguments to standard output as to not overflow the command-line buffer.
Thus, if needed, xargs forces multiple executions of PR-N | lp.

While xargs controls overflowing the command-line buffer,
Command xargs services may overflow. I 've witnessed the following mv
Command fail -- not the command-line buffer -- with an argument list too
Long error:

Find./-type F-print | xargs-I MV-F
{}./Newdir

Limit the number of files sent to mV at a time by using the xargs-l
Option. (The xargs-I () syntax is explained later in the article).
Following command sets a limit of 56 files at time, which mv es:

Find./-type F-print | xargs-l56-I mv
-F {}./newdir

The modern Unix OS seems to have solved the problem of the find
Command overflowing the command-line buffer. However, using the find
-Exec command is still troublesome. It's better to do this:

# Remove all files with a txt extension

Find.-Type F-name "*. txt"-print | xargs
Rm

Than this:

Find.-Type F-name "*. txt"-exec RM {}
/;-Print

Controlling the call to RM with xargs is more efficient than having
The find command execute RM for each object found.

Xargs one-liners

The find-xargs command combination is a powerful tool. The following
Example finds the unique owners of all the files in the/bin directory:

# All on one line

Find/bin-type F-Follow | xargs LS-Al |
Awk 'nf = 9 {print $3} '| sort-u

If/bin is a soft link, as it is with Solaris, the-follow option
Forces find to follow the link. The xargs command feeds the LS-Al
Command, which pipes to awk. If the output of the LS-Al command is 9
Fields, print Field 3 -- The file owner. sorting the awk output and
Piping to the uniq command ensures unique owners.

You can use xargs options to build extremely powerful commands.
Expanding the xargs/RM example, let's assume the requirement exists
Echo each file to standard output as it deletes:

Find.-Type F-name "*. txt" | xargs-I
Ksh-c "Echo deleting {}; RM {}"

The xargs-I option replaces instances of {} in a command (I. e .,
Echo and RM are commands ).

Conversely, instead of using the-I option with {}, the xargs-I
Option replaces instances of a string. The above command can be written
As:

Find.-Type F-name "*. txt" | xargs-I
{} KSh-c "Echo deleting {}; RM {}"

The new, third edition of UNIX power tools by Powers et al. Provides
An xargs "one-liner" that duplicates a directory tree. The following
Command creates in the usr/project directory, a copy of the current
Working directory structure:

Find.-type D-print | SED
'S @ ^ @/usr/project/@ '| xargs mkdir

The/usr/project directory must exist. When executing, note
Error:

Mkdir: failed to make directory "/usr/project/"; file exists

Which doesn' t prevent the directory structure creation. Ignore it.
To learn how the above command works, you can read more in UNIX power
Tools, third edition, Chapter 9.17 (O 'Reilly & Associates ).

In addition to serving the find command, xargs can be a slave
Other commands. Suppose the requirement is to group the output of UNIX
Commands on one line. Executing:

LOGNAME; Date

Displays the LOGNAME and date on two separate lines. Placing
Commands in parentheses and piping to xargs places the output of both
Commands on one line:

(LOGNAME; Date) | xargs

Executing the following command places all the file names in
Current directory on one line, and redirects to file "file. ls ":

Ls | xargs echo> file. ls

Use the xargs number of arguments option,-N, to display
Contents of "file. ls" to standard output, one name per line:

Cat file. ls | xargs-N1

# From UNIX in a nutshell

In the current directory, use the xargs-P option to prompt the user
To remove each file individually:

Ls | xargs-p-N1 RM

Without the-n option, the user is prompted to delete all the files
In the current directory.

Concatenate the contents of all the files whose names are contained
In file:

Xargs cat <File> file. Contents

Into file. contents.

Move all files from directory $1 to directory $2, and use the xargs
-T option to echo each move as it happens:

Ls $1 | xargs-I {}- t MV $1/{}$ 2 /{}

The xargs-I argument replaces each {} in the string with each
Object piped to xargs.

Conclusion

When should you use xargs? When the output of a command is
Command-line options of another command, use xargs in conjunction
Pipes. When the output of a command is the input of another command, use
Pipes.

References

Powers, Shelley, Peek, Jerry, et al. 2003. UNIX power tools.
Sebastopol, CA: O 'Reilly & Associates.

Robbins, Arnold. 1999. UNIX in a nutshell. Sebastopol, CA: O' Reilly
& Associates.

Ed Schaefer is a frequent contributor to Sys Admin. He is a software
Developer and dBA for Intel's factory integrated information systems,
Fiis, in aloha, Oregon. Ed also hosts the monthly shell corner column on
Unixreview.com. He can be reached at: shellcorner@comcast.net.

July 2003 update from the author:

I 've got ed very positive feedback on my xargs article. Other
Readers have shared constructive criticism concerning:

1. When using the duplicate directory tree "one-liner", reader Peter
Ludemann suggests using

Mkdir-P option:

Find.-type D-print | SED
'S @ ^ @/usr/project/@ '| xargs mkdir-P

Instead:

Find.-type D-print | SED
'S @ ^ @/usr/project/@ '| xargs mkdir

Mkdir's "-P" option creates parent directories as needed, and
Doesn't error out if one exists. Additionally,/usr/project does not
Have to exist.

2. ludemann, in addition to reader into ster Jansson, commented that
Spaces in directory names renders the duplicate directory tree
Completely useless.

Although I'm unable to salvage the duplicate directory command,
Those find and xargs versions that support-0 (probably GNU versions
Only), you might try experimenting:

Find...-print0 | xargs-0...

Using ludemann's email example, suppose your current directory
Structure contains:

Foo

Bar

Foo bar

Find.-Type F-print | xargs-N 1
Incorrectly produces:

Foo

Bar

Foo

Bar

While find.-Type F-print0 | xargs-0
-N 1
Delivers the correct results:

Foo

Bar

Foo bar

According to the 7.1 Red Hat Linux man page for xargs and find,
-0 doesn't use the null Terminator for file names disabling the Special
Meaning of white space.

3. Reader Peter simpkin asks the question, "Does the use of xargs
Only operate after the find command has completed?

Find.-Type F-name "*. txt"-print |
Xargs RM

If not, I was under the impression that the above was a bad idea
It is modifying the current directory that find is working from, or
Least this is what people have told me, and, thus the results of find
Are then undefined ."

My response is "no". Any Unix Command that supports command-line
Arguments is an xargs candidate. The results of the find command are
Valid as the output of the LS command:

# Remove files ending with. txt in current directory

Ls *. txt | xargs RM

If a command such as this is valid:

Chmod 444 1.txt 2.txt 3.txt

Then:

Find./(-name 1.txt-o-name 2.txt-o
-Name 3.txt/)-print | xargs chmod 444

Is valid.

In closing, if I had the opportunity to rewrite "using the xargs
Command ", it wocould look somewhat different.

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.