Pages

Saturday, April 1, 2017

All About Xargs

List of Topics

1) What is Xargs

2) Options with example's

1) What is Xargs

xargs command by default  expects the input from stdin, and executes /bin/echo command over the input.This manual page documents the GNU version of xargs.  xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.  Blank lines on the standard input are ignored.

2) Options with Examples

(i) -0     Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally).  Disables the end of file string, which  is treated like any other argument.  Useful when input items might contain white space, quote marks, or backslashes.  The GNU find -print0 option produces input suitable for this mode.

(i) -a <file> - This will take file as an input.

(ii) -p - Will prompt the user before executing ever command 

(ii) -n <Integer> - n will split the number of inputs per line

(iv) -i - Should be used while using curley braces to replcae string

find . -name 'Type*' | xargs -i mv {} test

ls *.txt | xargs -p -n 1 -i mv {} xargs_test

xargs as called above does not need the {}. It appends the STDIN to the end of the command without a placeholder. The use of {} usually means you want the STDIN somewhere in the middle of the cmd to execute, as in:

$ ls -1 | xargs -i mv {} /path/to/someplace/.

The -I requires a defined placeholder. The -i option will assume {} is the placeholder. This was where I found any assumption of {} 


eg 2: cat 10linefl | awk -F":" '{print $1}' | xargs -n 1 id

Sample file


[bhr_moham607@l0202 ~]$ cat tlinefile

root:x:0:0:root:/root:/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Eg : 


[bhr_moham607@l0202 ~]$ xargs -a tlinefile

root:x:0:0:root:/root:/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin


Eg : for this example will take a file which is having 10 lines. 

#
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

# xargs -n 2 -a 10lf
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin  ucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

No comments:

Post a Comment