20 Xargs Commands Examples in Linux / UNIX | Xargs Commands Tutorials

xargs-commands

Under Development

The xargs command is extremely useful when we combine it with other commands.This tutorials explains the usage of xargs command using few simple examples.
1. Xargs Basic Example
The xargs command (by default) expects the input from stdin, and executes /bin/echo command over the input. When you execute xargs without any argument, or when you execute it without combining with any other commands. you get Welcome message
> xargs
Hi,
Welcome to TGS.Hi, Welcome to TGS.
After you type something, press ctrl+d, which will echo the string back to you on stdout as shown below.
2. Specify Delimiter Using -d option
Delimiters can be applied so that each character in the input is taken literally using -d option in xargs.In the following example, when you use the -d\n, it will preserve newline delimiter in the output, and display the output exactly as it was typed.
> xargs -d\n
Hi,
Welcome to TGS.
3. Limit Output Per Line Using -n Option
By default as explained earlier, xargs displays whatever comes to its stdin as shown below.
> echo a b c d e f| xargs
a b c d e f
> echo a b c d e f| xargs -n 3
a b c
d e f
In the following example, we used -n 3, which will display only 3 items per line in the xargs output.
4. Delete Files that has White-space in the Filename
> find . -name “*.c” -print0 | xargs -0 rm -rf
Example
# fild /usr/preserve –mtime +30 –exec rm –f {} \;
is equivalent to….
# find /usr/preserve –mtime +30 –print | xargs rm –f
# find /usr/preserve –size +1024 –print | xargs –n20 rm –f
© Tutorials
CyberChimps