Information about Xargs
xargs is a command of the Unix and most Unix-like operating systems. It becomes useful when you want to pass a large amount of arguments to a command. In that case xargs will break the list of arguments in sublists large enough to be acceptable (the reader should note that you can't pass arbitrary long lists of parameters to a command [1]).
For example commands like this:
rm /path/*
or this
rm `find /path -type f`
...will fail with an error message of "Argument list too long" if there are too many files in /path
However the version below (functionally equivalent with rm `find /path -type f`) will not fail:
find /path -type f -print0 | xargs -0 rm
In this example, find feeds the input of xargs with a long list of file names. xargs then splits this list in sublists and calls rm once for every sublist. Note that this is more efficient than the functionally equivalent version bellow:
find /path -type f -exec rm '{}' \;
...which would call rm once for every single file.
xargs often covers the same functionality as the backquote feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input. It is a perfect companion for commands that output long lists of files like find, locate and grep.
find . -name "*.foo" -print0 | xargs -0 grep bar
...which uses GNU specific extensions to find and xargs to separate filenames using the null character;
find . -name "*.foo" -print0 | xargs -0 -t -r vi is similar to above, but launches the vi editor for each of the files. The -t prints the command to stderr before issuing it. The -r is a GNU extension that tells xargs not to run the command if no input was received.
find . -name "*.foo" -print0 | xargs -0 -i mv {} /tmp/trash uses -i to tell xargs to replace {} with the argument list
..... Click the link for more information.
..... Click the link for more information.
..... Click the link for more information.
For example commands like this:
rm /path/*
or this
rm `find /path -type f`
...will fail with an error message of "Argument list too long" if there are too many files in /path
However the version below (functionally equivalent with rm `find /path -type f`) will not fail:
find /path -type f -print0 | xargs -0 rm
In this example, find feeds the input of xargs with a long list of file names. xargs then splits this list in sublists and calls rm once for every sublist. Note that this is more efficient than the functionally equivalent version bellow:
find /path -type f -exec rm '{}' \;
...which would call rm once for every single file.
xargs often covers the same functionality as the backquote feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input. It is a perfect companion for commands that output long lists of files like find, locate and grep.
Examples
find . -name "*.foo" | xargs grep bar does the same as grep bar `find . -name "*.foo"` Note that the preceding command uses backticks (`), not single quotes ('). It searches in all files in the current directory and its subdirectories which end in.foo for occurrences of the string bar. These commands will not work as expected if there are whitespace characters, including newlines, in the filenames. In order to avoid this limitation one may use:
find . -name "*.foo" -print0 | xargs -0 grep bar
...which uses GNU specific extensions to find and xargs to separate filenames using the null character;
find . -name "*.foo" -print0 | xargs -0 -t -r vi is similar to above, but launches the vi editor for each of the files. The -t prints the command to stderr before issuing it. The -r is a GNU extension that tells xargs not to run the command if no input was received.
find . -name "*.foo" -print0 | xargs -0 -i mv {} /tmp/trash uses -i to tell xargs to replace {} with the argument list
References
External links
Unix command line programs and builtins (more) | |
|---|---|
| File and file system management | cat chattr cd chmod chown chgrp cksum cmp cp du df file fsck fuser ln ls lsof mkdir mount mv pwd rm rmdir split touch |
| Process management | at chroot crontab exit kill killall nice pgrep pidof pkill ps sleep time top wait watch |
| User Management/Environment | env finger id logname mesg passwd su sudo uname uptime w wall who whoami write |
| Text processing | awk comm cut ed ex fmt head iconv join less more paste sed sort tac tail tr uniq wc xargs |
| Shell programming | basename echo expr false printf test true unset |
| Printing: lp Communications: inetd netstat ping rlogin nc traceroute Searching: find grep strings Miscellaneous: banner bc cal dd man size yes | |
Unix (officially trademarked as UNIX®) is a computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie and Douglas McIlroy.
..... Click the link for more information.
..... Click the link for more information.
Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification.
..... Click the link for more information.
..... Click the link for more information.
An operating system (OS) is the software that manages the sharing of the resources of a computer. An operating system processes system data and user input, and responds by allocating and managing tasks and internal system resources as a service to users and programs of the
..... Click the link for more information.
..... Click the link for more information.
..... Click the link for more information.
Unix shell, also called "the command line", provides the traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering command input as text for a shell to execute.
..... Click the link for more information.
..... Click the link for more information.
The
..... Click the link for more information.
find program is a directory search utility, mostly found on Unix-like platforms. It searches through one or more directory trees of a filesystem, locating files based on some user-specified criteria...... Click the link for more information.
Locate, locating, or locator may refer to:
..... Click the link for more information.
- Location (dab)
- Locate Varesino, an Italian commune
- Locator software, in computing
- GNU locate, in the GNU Linux computer operating system
- Real Time Locating, in radio frequency identification
..... Click the link for more information.
grep is a command line utility that was originally written for use with the Unix operating system. Given a list of files or standard input to read, grep searches for lines of text that match one or many regular expressions, and outputs only the matching lines.
..... Click the link for more information.
..... Click the link for more information.
In computing, a directory, catalog, or folder[1] is an entity in a file system which contains a group of files and/or other directories. A typical file system may contain thousands (or even hundreds of thousands) of directories.
..... Click the link for more information.
..... Click the link for more information.
string is an ordered sequence of symbols. These symbols are chosen from a predetermined set.
In programming, when stored in memory each symbol is represented using a numeric value.
..... Click the link for more information.
In programming, when stored in memory each symbol is represented using a numeric value.
..... Click the link for more information.
newline (also known as a line break or end-of-line / EOL character) is a special character or sequence of characters signifying the end of a line of text.
..... Click the link for more information.
..... Click the link for more information.
The null character (also null terminator) is a character with the value zero, present in the ASCII and Unicode character sets, and available in nearly all mainstream programming languages.
..... Click the link for more information.
..... Click the link for more information.
VI is the Roman numeral for the number six. VI may also refer to:
Places:
..... Click the link for more information.
Places:
- Vancouver Island, British Columbia, Canada
- British Virgin Islands (FIPS country code: VI), a British territory in the Caribbean
- U.S.
..... Click the link for more information.
Unix (officially trademarked as UNIX®) is a computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie and Douglas McIlroy.
..... Click the link for more information.
..... Click the link for more information.
command line interface or CLI is a method of interacting with an operating system or software using a command line interpreter. This command line interpreter may be a text terminal, terminal emulator, or remote shell client such as PuTTY.
..... Click the link for more information.
..... Click the link for more information.
Printing: lp Communications: inetd netstat ping rlogin nc traceroute Searching: find grep strings Miscellaneous: banner bc cal dd man size yes
..... Click the link for more information.
..... Click the link for more information.
The cat command is a standard Unix program used to concatenate and display files. The name is from , a synonym of concatenate.
..... Click the link for more information.
Specification
The Single Unix Specification specifies the behavior that each of the files given in sequence as arguments will write their..... Click the link for more information.
chattr is a UNIX program that allows a user to set certain attributes to a file. Mostly chattr is used to make files immutable so that password files and certain system files cannot be erased during software upgrades.
..... Click the link for more information.
..... Click the link for more information.
cd, sometimes also available as chdir (change directory), is a command line command to change the current working directory in operating systems such as Unix, Windows and DOS.
..... Click the link for more information.
..... Click the link for more information.
The chmod command (abbreviated from change mode) is a shell command in Unix and Unix-like environments.
When executed, the command can change file system modes of files and directories. The modes include permissions and special modes.
..... Click the link for more information.
When executed, the command can change file system modes of files and directories. The modes include permissions and special modes.
..... Click the link for more information.
The chown command is used on Unix-like systems to change the owner of a file. In most implementations, it can only be executed by the Superuser. Unprivileged (regular) users who wish to change the group of a file that they own may use chgrp.
..... Click the link for more information.
..... Click the link for more information.
The chgrp command is used by unprivileged users on Unix-like systems to change the group associated with a file. Unlike the chown command, chgrp allows regular users to change groups, but only to one of which they are a member.
..... Click the link for more information.
..... Click the link for more information.
cksum is a POSIX command that reads the files specified by the File parameter and calculates a checksum, cyclic redundancy check (CRC) and the byte count for a file or files. If no files are specified, the cksum command reads standard input.
..... Click the link for more information.
..... Click the link for more information.
cmp is a command line utility for computer systems that use a Unix operating system. It compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first
..... Click the link for more information.
..... Click the link for more information.
..... Click the link for more information.
du (abbreviated from disk usage) is a standard Unix program used to estimate the file space usage; space used under a particular directory or files on a file system. History
Thedu utility first appeared in version 1 of AT&T UNIX...... Click the link for more information.
df (abbreviated from disk free) is a standard Unix computer program used to display the amount of available disk space for filesystems on which the invoking user has appropriate read access, df
..... Click the link for more information.
..... Click the link for more information.
file is a standard Unix program for determining the type of data contained in a file.
..... Click the link for more information.
History
The original version of file originated in Unix Research Version 4 in 1973...... Click the link for more information.
fuser is a UNIX command showing which processes are using a specified file.
..... Click the link for more information.
..... Click the link for more information.
ln is a standard Unix program used to create links (link) to files.
..... Click the link for more information.
Link files
Links allow more than one file to refer to the same file, elsewhere...... Click the link for more information.
This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus