...
The -I{} specifies that the arguments by results from ls will be placed in the location of the {} called the replacement string. In fact you can use whatever variable name you want instead of {}. For example, the following will also workchanging {} to varX also works,
Code Block | ||
---|---|---|
| ||
ls | xargs -t -IvarX md5 varX md5 file1.txt MD5 (file1.txt) = d41d8cd98f00b204e9800998ecf8427e md5 file2.txt MD5 (file2.txt) = d41d8cd98f00b204e9800998ecf8427e md5 file3.txt MD5 (file3.txt) = d41d8cd98f00b204e9800998ecf8427e |
...
Code Block | ||
---|---|---|
| ||
find . -print0 | xargs -0 -I{} echo {} |
Using xarg's -0 does not work with large data sets. Keep in mind that This is because "ls" produces slightly different output from "find .". Here is an example,
Code Block | ||
---|---|---|
| ||
# Data I am working with ls 11 My Baby's Got To Pay the Rent 1.m4a 6 Habits (Stay High) [Hippie Sabotage Remix] 1.txt 11 My Baby's Got To Pay the Rent 1.txt Tin's file.txt 11 Summertime Sadness 1.m4a hello 11 The Troubles 1.m4a pwd 12 Canoeing (Katie and Alex's Theme) 1.m4a test123 # Apostrophe kills xargs here xargs: unterminated quote # -0 by itself does not solve the problem ls | xargs -0 -I{} echo {} {} # Now it works. find . -print0 | xargs -0 -I{} echo {} . ./11 My Baby's Got To Pay the Rent 1.m4a ./11 My Baby's Got To Pay the Rent 1.txt ./11 Summertime Sadness 1.m4a ./11 The Troubles 1.m4a ./12 Canoeing (Katie and Alex's Theme) 1.m4a ./6 Habits (Stay High) [Hippie Sabotage Remix] 1.txt ./hello ./pwd ./test123 ./Tin's file.txt # Remove a file to show -0 works with smaller data set, rm test123 # -0 by itself now workworks, but making any file names longer or adding back test123 breaks it ls | xargs -0 -I{} echo {} {} Kitchen-iMac:tmp tin.pham$ ls | xargs -0 -I{} echo {} 11 My Baby's Got To Pay the Rent 1.m4a 11 My Baby's Got To Pay the Rent 1.txt 11 Summertime Sadness 1.m4a 11 The Troubles 1.m4a 12 Canoeing (Katie and Alex's Theme) 1.m4a 6 Habits (Stay High) [Hippie Sabotage Remix] 1.txt Tin's file.txt hello pwd # Show's that find looks different than ls and you want to keep that in mind, find . -print0 | xargs -0 -I{} echo {} . ./11 My Baby's Got To Pay the Rent 1.m4a ./11 My Baby's Got To Pay the Rent 1.txt ./11 Summertime Sadness 1.m4a ./11 The Troubles 1.m4a ./12 Canoeing (Katie and Alex's Theme) 1.m4a ./6 Habits (Stay High) [Hippie Sabotage Remix] 1.txt ./hello ./pwd ./Tin's file.txt |
Useful Applications of xargs
Search - ...
find . -print0 | xargs -0 -I{} echo {}