108
votes
Accepted
How do I get xargs to show me the command lines it's generating without running them?
You may benefit from the -p or -t flags.
xargs -p or xargs --interactive will print out the command to be executed and then prompt for input (y/n) to confirm before executing the command.
% cat list
...
60
votes
cp after xargs not working
While you already know how you should solve your current problem, I'll still answer about xargs.
xargs puts the string it got in the end of command, while in your case you need that string before the ...
54
votes
How to repeat variables twice in xargs
Just write {} two times in your command. The following would work:
$ echo test | xargs -I {} echo {} {}
test test
Your problem is how the commands are nested. Lets look at this:
echo test | xargs -...
53
votes
Accepted
Passing multiple parameters via xargs
I believe that you can’t use -I that way.
But you can get the effect / behavior you want by saying:
echo {1..8} | xargs -n2 sh -c 'echo "the number $1 comes before $2"' sh
This, essentially, ...
48
votes
How to quote arguments with xargs
I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.
find . -size +1M | xargs -I {} rm "{}"
38
votes
Why does xargs strip quotes from input?
if you want xargs to ignore quotes one of the good soultion can be the use of xargs flag xargs -0
Directly from Man page OPTIONS
OPTIONS -0, --null
Input items are terminated by a ...
34
votes
Accepted
xargs -I option
"with names read from standard input" means that xargs takes the data coming in on its standard input, splits it up, and uses it to run the command given in its arguments. By default, it splits on ...
33
votes
Accepted
Why does 'top' fail to run via 'xargs'?
Turns out that there is a special option --open-tty in xargs for interactive applications like top. From man xargs:
-o, --open-tty
Reopen stdin as /dev/tty in the child process before
...
28
votes
Why does xargs strip quotes from input?
For GNU xargs, I found another solution in the manpage: explicitly specify delimiter to be '\n'. This turns off special handling for quotes:
--delimiter=delim, -d delim
Input items are terminated by ...
26
votes
Have xargs use alias instead of binary
Use alias xargs='xargs '
alias: alias [-p] [name[=value] ... ]
(snip)
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
26
votes
Accepted
xargs - append each argument with a parameter
One way to do it:
echo "a b c" | xargs printf -- '-f %s\n' | xargs mycommand
This assumes a, b, and c don't contain blanks, newlines, quotes or backslashes. :)
With GNU findutil you can handle the ...
26
votes
Accepted
Copy multiple files using command `xargs`
As the cp error indicates, the target directory must come last. Since it looks like your cp doesn't have an equivalent of GNU cp's -t option, you have to get xargs to insert the filename between cp ...
24
votes
How do I get xargs to show me the command lines it's generating without running them?
Put an echo in front of the command to run?
$ echo a b c d e | xargs -n2 echo rm
rm a b
rm c d
rm e
24
votes
Accepted
How can I prevent arguments to `xargs` from being prefixed with spaces?
Use -I {}, and {} at the place where you want the argument to appear:
xargs -I {} -n 1 echo rm /usr/src/packages/{}
(You can use something other than {}, {} is just very common.)
Without this, ...
24
votes
find . -print0 | xargs -0 cmd vs find . -exec cmd {} +
TL;DR
There's no clear winner. My recommendation would be to use:
find . -exec cmd {} +
Wherever it's enough as it's more portable, uses less resource and has fewer issues, and one of:
xargs -r0 -...
23
votes
Accepted
How can I get the index of the xargs "parallel processor"?
If you're using GNU xargs, there's --process-slot-var:
--process-slot-var=environment-variable-name
Set the environment variable environment-variable-name to a unique
value in each running ...
22
votes
How can I find files and then use xargs to move them?
Perhaps this command is possible now and wasn't back in 2013, but this works perfectly for me:
ls pattern* | xargs mv -t DESTINATION/
The -t key puts the destination folder first, freeing up mv ...
22
votes
Accepted
How to give a comma-separated list as arguments to the next command
This should equally work as well:
s1 | xargs -d "," -n1 s2
Test case:
printf 1,2,3,4 | xargs -d ',' -n1 echo
Result:
1
2
3
4
If s1 outputs that list followed by a newline character, you'd want ...
21
votes
Why does 'top' fail to run via 'xargs'?
top is an interactive program, for instance you can type i to toggle showing idle processes. Whilst it could arrange to read from /dev/tty in practice it expects that stdin is connected to the ...
20
votes
Accepted
How to make xargs handle spaces and special characters?
Use -d '\n' with your xargs command:
cat file | xargs -d '\n' -l1 mkdir
From manpage:
-d delim
Input items are terminated by the specified character. Quotes and backslash are not ...
20
votes
Accepted
find and sed string in docker got error ` Device or resource busy`
Yes, as you found, the file is mounted by docker, which means you are not allowed to change its inode from within docker container. But, what if you only change the content of file without touching ...
20
votes
Accepted
If no input is given to xargs, don't let xargs run the utility
You are looking for the -r option. This is a non-standard option which may be implemented on your system (check your xargs manual).
From the GNU xargs manual:
-r, --no-run-if-empty
If the standard ...
19
votes
Accepted
One liner to see if grep returned something?
Just tack the exit status check after grep, it will always get the exit status from the last command of the pipeline by default:
sudo dmidecode | grep -q ThinkPad; echo $?
Use -q to suppress any ...
18
votes
Accepted
Remove all duplicate word from string using shell script
One more awk, just for fun:
$ a="aaa bbb aaa bbb ccc aaa ddd bbb ccc"
$ echo "$a" | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s%s",$i,FS)}{printf("\n")}'
aaa bbb ccc ddd
By the way, even ...
17
votes
GNU find and masking the {} for some shells - which?
The {} needed to be quoted in versions of the fish shell prior to 3.0.0.
$ fish -c 'echo find -exec {} \;'
find -exec ;
And in the rc shell (also akanga based on rc, but not es):
$ rc -c "echo ...
17
votes
Accepted
What is difference between results and effectiveness after adding -f in rm and -print in find?
-f tells rm to never prompt (e.g. when it encounters read-only files) and to ignore missing files it’s asked to delete (instead of indicating an error). It will also not complain if not passed any ...
17
votes
Accepted
How do I create directories named after each month?
With -I, xargs gets one argument per line as opposed to the default of one argument per (blank or newline delimited, possibly quoted) word without -I (and implies -n). So in your example date is ...
16
votes
Accepted
ssh-add add all private keys in .ssh directory
Slightly convoluted, but:
for possiblekey in ${HOME}/.ssh/id_*; do
if grep -q PRIVATE "$possiblekey"; then
ssh-add "$possiblekey"
fi
done
You can also add all of your keys to your ~/....
16
votes
Accepted
How do I use `find` to go to directory of that file
At least if you have GNU find, you can use -printf '%h' to get the directory
%h Leading directories of file's name (all but the last ele‐
ment). If the file name contains no ...
16
votes
Accepted
Xargs into second side of pipe?
It's because you need a shell to create a pipe or perform redirection. Note that cat is the command to concatenate, it makes little sense to use it just for one file.
cat file1.txt | xargs -I{} sh -c ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
xargs × 692find × 171
bash × 165
shell × 101
pipe × 69
grep × 66
shell-script × 61
linux × 53
command-line × 41
awk × 37
sed × 31
curl × 26
ls × 23
cp × 21
arguments × 18
files × 17
cat × 17
macos × 17
parallelism × 17
text-processing × 15
tar × 13
quoting × 13
rm × 13
echo × 12
zsh × 11