Skip to main content
130 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 ...
Kevin Hencke's user avatar
  • 1,416
58 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 {} echo "{}" # just for show (ha) find . -size +1M ...
dee-see's user avatar
  • 697
55 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, creates ...
Scott - Слава Україні's user avatar
34 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 ...
ks1322's user avatar
  • 1,672
31 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 ...
fungusakafungus's user avatar
30 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 ...
muru's user avatar
  • 77.9k
29 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 -...
Stéphane Chazelas's user avatar
26 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 ...
Nikhil VJ's user avatar
  • 371
23 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 ...
Bruce's user avatar
  • 1,361
23 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 to ...
George Udosen's user avatar
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 child ...
muru's user avatar
  • 77.9k
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 ...
icarus's user avatar
  • 19.1k
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 ...
Kusalananda's user avatar
  • 356k
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 ...
Stéphane Chazelas's user avatar
17 votes
Accepted

How to get the total duration of all video files in current folder and sub-directories

With exiftool: $ exiftool -ext mp4 -r -n -q -p '${Duration;$_ = ConvertDuration(our $total += $_)}' . | tail -n 1 4 days 22:14:59 The main difference from @don_cristi's answer to a very similar ...
Stéphane Chazelas's user avatar
16 votes

Piping commands after a piped xargs

Just in addition to fredtantini and as a general clarification (since the docs are a bit confusing): The xargs -I {} will take the '{}' characters from the standard input and replace them with ...
Borisu's user avatar
  • 271
16 votes
Accepted

Why xargs does not process the last argument?

What went wrong b appears in the output, so it was processed, just not the way you expected. As a first step, ask bash to tell you what it sees: pass the -x option to enable its traces. $ echo a b | ...
Gilles 'SO- stop being evil''s user avatar
14 votes

passing a '-9' to xargs kill command

Example how to close php processes in batch. php you can replace using whatever your process has name: ps -aux | grep php | awk '{print $2}' | xargs kill -9
Andrzej Piotrowski's user avatar
14 votes
Accepted

find -exec {} + argument list limit

Yes, find -exec ... {} + runs the given command as many times as necessary to accommodate all the arguments without exceeding the maximum argument list size in each invocation. This is specified by ...
Stephen Kitt's user avatar
14 votes
Accepted

Move all files NOT ending with .txt

The simple shell loop variant (in bash): shopt -s extglob dotglob nullglob for pathname in ~username/data/!(*.txt); do ! test -d "$pathname" && mv "$pathname" ~username/data/other_files ...
Kusalananda's user avatar
  • 356k
14 votes
Accepted

Why do I get an error when deleting entries from Bash history?

The error is raised because xargs cannot find a history command. It is a shell builtin, as you can confirm with type history, thus not visible to xargs. Try echo 1 | xargs history -d; echo $? The ...
Quasímodo's user avatar
  • 19.4k
13 votes
Accepted

Xargs to extract filename

Just do: for f in *.html; do printf '%s\n' "[${f%.*}](./$f)"; done > index.md Use set -o nullglob (zsh, yash) or shopt -s nullglob (bash) for *.html to expand to nothing instead of *.html (or ...
Stéphane Chazelas's user avatar
13 votes

How does xargs know when a stdin input ends, so that it can start processing it?

To read its input, xargs uses the read function (see also the corresponding Linux manpage). read reads the next available data from a file descriptor into an area of memory specified by the calling ...
Stephen Kitt's user avatar
13 votes
Accepted

Reliable way to open files given by the result of find … -exec grep … {} \+

vim $(find path/ -exec grep -l 'pattern' {} +) is an unquoted command substitution, so word splitting will be performed on whitespace on its result, as well as pathname expansion. I.e., if a file a b ...
Quasímodo's user avatar
  • 19.4k
12 votes

How to repeat variables twice in xargs

Another option is to use -i flag, which is the same as -I{} (it implies that the replacement is given with {}): $ echo test | xargs -i echo {} {}
Omer Dagan's user avatar
12 votes

echo list / array to xargs

printf '%s\n' "${list[@]}" | xargs This would print each element of list on its own line and that newline-delimited list would be passed to xargs. "${list[@]}" would expand to the ...
Kusalananda's user avatar
  • 356k
12 votes
Accepted

Use xargs and kill in script with optional signal

The problem is that you’re explicitly adding a space to SIGNAL: SIGNAL=" -${SIGNAL}" and then referencing it in quotes:kill "$SIGNAL" Since kill is seeing an argument that doesn’t ...
G-Man Says 'Reinstate Monica''s user avatar
12 votes
Accepted

Run command on each line of CSV file, using fields in different places of the command

GNU parallel can read csv directly, and has item replacement built in. More or less directly taken from man parallel: parallel --csv 'my_cmd --arg1 {2} --arg2 {1} --arg3 {3}' :::: file.csv Add -j1 ...
Marcus Müller's user avatar
11 votes

Piping commands after a piped xargs

GNU Parallel makes this kind of tasks easy: find . -type d -name "*log*" | parallel --tag "ls -la {} | tail -2" If you do not want to do a full install of GNU Parallel you can do a minimal ...
Ole Tange's user avatar
  • 37.5k
11 votes
Accepted

xargs with rm + filename containing spaces

xargs -d "\n" -I {} rm \"{}\" This assumes GNU coreutils' version of xargs that supports the -d option for specifying the delimiter. This will not work together with your find command because it adds ...
Kusalananda's user avatar
  • 356k

Only top scored, non community-wiki answers of a minimum length are eligible