Questions tagged [xargs]

A command that constructs an argument list for a command from text given as xargs' standard input (or read from a specified file) thus allowing execution on the results of a search, for example. It is provided by the findutils package

xargs is a command for constructing and executing commands from standard input. xargs reads delimited strings and executes command-line utilities with the strings as arguments.

It is common to pass strings to xargs via standard output using a pipe (|).

However, xargs can be told to read from another file instead of standard input by passing the --arg-file or -a option, as demonstrated, for example in this answer. This makes is a surprisingly versatile utility.

xargs is provided by the findutils package which is part of the default Ubuntu installation in all current versions. This package also provides find. The two commands may be used together, for example:

find path tests | xargs commands

Since xargs will normally split on spaces, it's advisable to pass null-delimited lists to it, and use the -0 flag:

find ... -print0 | xargs -0 ...

find can execute commands on the found files (either with builtin actions or using -exec) so xargs may rarely be needed with it, but locate cannot execute commands on its results, so xargs may be useful with locate, again using null delimiting if filenames may contain spaces:

locate -0 pattern | xargs -0 command

The ability to pass alternative files to STDIN to xargs allow it to be used beyond the classic usages mentioned above. For example, instead of constructs like:

command $(cat file)

We can use something like:

xargs -a file command
52 questions
42
votes
4 answers

What does "xargs grep" do?

I know the grep command and I am learning about the functionalities of xargs, so I read through this page which gives some examples on how to use the xargs command. I am confused by the last example, example 10. It says "The xargs command executes…
AlphaOmega
  • 1,513
35
votes
3 answers

Piping find -name to xargs results in filenames with spaces not being passed to the command

Normally to remove files with spaces in their filename you would have to run: $ rm "file name" but if I want to remove multiple files, e.g.: $ find . -name "*.txt" | xargs rm This will not delete files with spaces in them.
Ash
  • 497
18
votes
1 answer

Difference between "xargs" and command substitution?

In many cases I use command substitution instead of xargs. For example rm $(ls) is the same as ls | xargs rm What really are the differences between them? I think one of the differences is that command substitution runs in subshell while xargs runs…
Sinoosh
  • 2,101
16
votes
2 answers

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

I'd like to count all the ordinary file on home directory with commands: $ find ~ -type f | xargs echo | wc -w xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option It prompts xargs: unmatched single…
12
votes
3 answers

deleting folders with spaces in their names using xargs

Why can't xargs delete directories with spaces in their names, and how to fix that? 76 find . -type d |xargs rm -rf 77 rm -rf fire\ hydrant/ 78 rm -rf wine\ glass/ 79 rm -rf tennis\ racket/ 80 rm -rf traffic\ light/ 81 rm -rf parking\ meter/ …
Mona Jalal
  • 4,695
  • 21
  • 74
  • 102
12
votes
1 answer

How to pass list of names to be searched by grep using xargs?

I have a text file containing names (nameslist.txt) and I want to read them using cat and pipe the result with xargs to a grep command, so that grep checks the existence of each name that it receives in a target file (targetfile.txt). Let's say…
11
votes
1 answer

How can I solve "unmatched double quote" error using dbus-monitor in combination with xargs?

To intercept (notify-osd) notifications on Linux (Ubuntu), I am using the dbus-monitor script below. Subsequently, the script runs another script (/opt/nonotifs/nonotifs/silent) with the intercepted notification as argument, for further…
Jacob Vlijm
  • 85,475
10
votes
3 answers

What is the difference between find with -exec and xargs?

trying to learn Bash scripting I want to execute some command on all files below my current directory that satisfy a certain condition. Using find -name *.flac Specifically I want to convert .flac to .mp3. I can find all the files. However I do not…
9
votes
1 answer

Command to change permissions only on files not directories

I have the following command find . -type f -print0 | xargs -0 chmod 644 which would successfully change to 644 the permissions on all files in ., provided that the filenames contained no embedded spaces. However it doesn't work in general. For…
Leo Simon
  • 1,549
7
votes
2 answers

Read a URL from a file and open it in a Firefox tab

PreTabs.txt is a file I have on my desktop, and it just contains the line google.com I want to pipe the content of that file into Firefox so that it will open the URL in a new tab. If you execute cat PreTabs.txt | firefox, shouldn't that be the…
user569041
7
votes
1 answer

"-bash: /usr/bin/rename: Argument list too long"

I would like to rename files within each sub-directory by adding the name of the sub-directory. Following the answer from Rename files by adding their parent folder name, I tried: rename 's/(.*)\//$1\/$1_/' */* However for lots of sub-directories…
Josselin
  • 203
6
votes
1 answer

xargs: invalid option -- 'o'

I was trying to execute this command to fix yet another error (Unable to use a TTY - input is not a terminal or the right kind of file): kubectl get pods -n foobar | grep baz | awk '{print $1}' | xargs -J % kubectl exec -it -n foobar %…
Moobie
  • 173
5
votes
1 answer

Does gzip accept stdin?

When i pipe to gzip it can not accept stdin i should using xargsto convert stdin to argument $ls 1.txt $ls |xargs gzip && ls 1.txt.gz every thing is ok . but when i want to compress a cpio archive file $ls | cpio -ov | gzip >…
Sinoosh
  • 2,101
4
votes
0 answers

fswatch | while read | xargs running command twice

Thanks in advance for any advice I'm working on an fswatch command to launch a script when files hit the Downloads folder. fswatch --event Created Downloads/ | (while read x; do echo $x | xargs -0 bash ./dlsort.sh; done) The script is working as…
3
votes
1 answer

Finding, moving and removing in Ubuntu

OS: Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-62-generic x86_64) I have a directory like the following: ~/total/ test1/ test1.txt some_other_file_i_dont_care.py test2/ test2.tex some_folder_i_dont_care/ test3/ …
tomasyany
  • 133
1
2 3 4