现在的位置: 首页 > 综合 > 正文

Xargs Command Examples

2018年06月10日 ⁄ 综合 ⁄ 共 5279字 ⁄ 字号 评论关闭
文章目录

xargs Man Page:

xargs [options] [command]

Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly.xargs passes these arguments in several bundles tocommand, allowingcommand to process
more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated byls orfind, for example) that get passed toxargs via a pipe.

Options

-0, --null

Expect filenames to be terminated by NULL instead of whitespace. Do not treat quotes or backslashes specially.

-e[string] , -E [string] ,
--eof
[=string]

Set EOF to _ or, if specified, to string.

--help

Print a summary of the options to xargs and then exit.

-i[string] , -I [string] ,
--replace
[=string]

Replace all occurrences of { }, or string, with the names read from standard input. Unquoted blanks are not considered argument terminators. Implies-x and-L 1.

-l[lines] , -L [lines] ,
--max-lines
[=lines]

Allow no more than lines nonblank input lines on the command line (default is 1). Implies-x.

-n args, --max-args=args

Allow no more than args arguments on the command line. Overridden by the maximum number of characters set with-s.

-p, --interactive

Prompt for confirmation before running each command line. Implies -t.

-P max, --max-procs=max

Allow no more than max processes to run at once. The default is 1. A maximum of 0 allows as many as possible to run at once.

-r, --no-run-if-empty

Do not run command if standard input contains only blanks.

-s max, --max-chars=max

Allow no more than max characters per command line.

-t, --verbose

Verbose mode. Print command line on standard error before executing.

-x, --exit

If the maximum size (as specified by -s) is exceeded, exit.

--version

Print the version number of xargs and then exit.

Examples

grep for pattern in all files on the system:

find / | xargs grep pattern > out &

Run diff on file pairs (e.g., f1.a and
f1.b
, f2.a and f2.b, etc.):

echo $* | xargs -n2 diff

The previous line would be invoked as a shell script, specifying filenames as arguments. Displayfile, one word per line (same asderoff -w):

cat file | xargs -n1

Move files in olddir to newdir, showing each command:

ls olddir | xargs -i -t mv olddir/{ } newdir/{ }

Summary:

I am trying to use xargs command using shell pipes and not able to understand how to control and use command line arguments. For example I'd like to find out all *.c file located in 100s of sub-directories and move them to another
directory called ~/old.src. How do I use command line args with xargs to achieve the same?xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited
by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

{} 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 .bak files in or below the current directory
and move them to ~/.old.files directory:

$ find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files

You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:

$ find . -name "*.bak" -print0 | xargs -0 -I file mv file ~/old.files

Where:

  1. -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.
  2. -I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.

Dealing file names with blank spaces and newline

The following will work incorrectly if there are any filenames containing newlines or spaces (it will find out all .mp3 file located in current directory and play them using mplayer):

$ find . -iname "*.mp3" -print | xargs mplayer

To get rid of this problem use -0 option:

$ find . -iname "*.mp3" -print0 | xargs -0 -I mp3file mplayer mp3file

To find out all *.c file located in 100s of subdirectories and move them to another directory called ~/old.src, use:

$ find /path/to/dir -iname "*.c" -print0 | xargs -0 -I file mv file ~/old.src

Avoiding errors and resource hungry problems with xargs and find combo

To copy all media files to another location called /bakup/iscsi, you can use cp as follows:

$ cp -r -v -p /share/media/mp3/ /backup/iscsi/mp3

However, cp command may fail if an error occurs such as if the number of files is too large for the cp command to handle. xargs in combination with find can handle such operation nicely. xargs is more resource efficient and will not halt with an error:

$ find /share/media/mp3/ -type f -name "*.mp3" -print0 | xargs -0 -r -I file cp -v -p file --target-directory=/bakup/iscsi/mp3

Please note that all of the above commands are tested with GNU/xargs version. BSD and UNIX xargs command may not have options such as -r. Please refer to your local xargs man page for further info:
man xargs

More expamples:

xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively.

Xargs Example 1:

When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem.

# find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

Xargs Example 2:

Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs.

# find /etc -name "*.conf" | xargs ls –l

Xargs Example 3:

If you have a file with list of URLs that you would like to download, you can use xargs as shown below.

# cat url-list.txt | xargs wget –c

Xargs Example 4:

Find out all the jpg images and archive it.

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

Xargs Example 5:

Copy all the images to an external hard-drive.

# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

抱歉!评论已关闭.