xargs is a command that executes another command and generates its arguments from stdin

learn more… | top users | synonyms

48
votes
6answers
5k views

When is xargs needed?

The xargs command always confuses me. Is there a general rule that can help me figure out when I need it? Consider the two examples below: $ \ls | grep Cases | less prints the files that match ...
14
votes
7answers
10k views

Using xargs with input from a file

Say I have a file with the following bob john sue Now these directly corrospond to (in this case) URL pattern such as http://example.com/persons/bob.tar, john.tar, sue.tar. I would like to take ...
14
votes
2answers
829 views

gnu find and masking the {} for some shells - which?

The man page for gnu find states: -exec command ; [...] The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, ...
11
votes
6answers
6k views

xargs with stdin/stdout redirection

I would like to run: ./a.out < x.dat > x.ans for each *.dat file in the directory A. Sure, it could be done by bash/python/whatsoever script, but I like to write sexy one-liner. All I could ...
11
votes
3answers
2k views

bash find xargs grep only single occurence

Maybe it's a bit strange - and maybe there are other tools to do this but, well.. I am using the following classic bash command to find all files which contain some string: find . -type f | xargs ...
11
votes
4answers
9k views

Sorting the output of “find”?

I need to be able to alphabetically sort the output of find before piping it to a command. Entering | sort | between didn't work, so what could I do? find folder1 folder2 -name "*.txt" -print0 | ...
11
votes
6answers
1k views

Why does xargs cause apt-get to abort?

I'm trying to remove a list of packages from a file. I'm using the following command: cat packages | xargs sudo apt-get remove packages is my file containing a list of packages I want to remove. ...
11
votes
8answers
820 views

Spreading stdin to parallel processes

I have a task that processes a list of files on stdin. The start-up time of the program is substantial, and the amount of time each file takes varies widely. I want to spawn a substantial number of ...
10
votes
3answers
4k views

Find -exec + vs find | xargs. Which one to choose?

I understand that the -exec can take a + option to mimic the behaviour of xargs. Is there any situation where you'd prefer one form over the other? I personally tend to prefer the first form, if ...
9
votes
4answers
3k views

How to make `xargs` ignore child's exit and keep processing further

I sometimes run long xargs jobs overnight and it is really annoying to discover in the morning that xargs died somewhere in the middle, for example because of a segmentation fault in one single ...
9
votes
1answer
288 views

How to do `head` and `tail` on null-delimited input in bash?

find command can output names of files as a null-delimited strings (if -print0 is provided), and xargs can consume them with -0 option turned on. But in between, it's hard to manipulate that ...
9
votes
2answers
263 views

Piping nothing to xargs

I have a script that may in some circumstances have nothing on stdout. At the end of the script, I pass the lines I have to another command using xargs. My problem is, when there is nothing on stdout, ...
8
votes
3answers
282 views

How to execute a command repeatedly with different arguments?

I'm on Ubuntu. I copied some arguments (separated by newline) and I can use xsel to print them out like this $ xsel arg1 arg2 arg3 arg4 ... Now, I want to use each of these arguments for another ...
7
votes
2answers
2k views

How to delete commands in history matching a given string?

I need to delete all commands in my history matching a string. I've tried: $ history | grep searchstring | cut -d" " -f2 | history -d -bash: history: -d: option requires an argument $ history | grep ...
7
votes
3answers
310 views

What's wrong with this xargs command?

Consider the output: % { echo one; echo two; echo three; } | xargs -I{} -L1 echo test-{} test-{} one test-{} two test-{} three Why doesn't {} get substituted as per the manual page (and my memory, ...
7
votes
2answers
1k views

How to stop xargs from badly merging output from multiple processes?

I'm using xargs with the option --max-args=0 (alternatively -P 0). However, the output of the processes is merged into the stdout stream without regard for proper line separation. So I'll often end ...
7
votes
3answers
3k views

How to open multiple files from `find` output?

I know I can open multiple files found in a dir like so: find -name *.foo -xargs <command> {} \; This works, but when trying to open a bunch of textfiles in gedit at the same time, it opens ...
6
votes
6answers
11k views

How to search for a word in entire content of a directory in linux

need to search for something in entire content I am trying: find . | xargs grep word I get error: xargs: unterminated quote How to achieve this?
6
votes
5answers
994 views

Find images by size: find / file / awk

I've been trying to find png image files a certain height (over 500px). I know that file will return image dimensions. Example: $ file TestImg1a.png TestImg1a.png: PNG image data, 764 x 200, ...
5
votes
4answers
978 views

recursively chmod

I was trying to chmod folders and files with: find . -type d -exec chmod 755 {} \; find . -type f -exec chmod 644 {} \; But I was wondering: How to do it in one line using find and excluding the ...
5
votes
4answers
377 views

How do I get a single total of lines with `wc -l`?

I've added a git alias to give me the line counts of specific files in my history: [alias] lines = !lc() { git ls-files -z ${1} | xargs -0 wc -l; }; lc However, wc -l is reporting multiple totals, ...
5
votes
2answers
561 views

Is using xargs faster than not using it?

Is this faster: find /tmp -name core -type f | xargs /bin/rm -f than doing this: find /tmp -name core -type f -exec /bin/rm -f {} \; So would using xargs increase overall speed? (I got the ...
5
votes
2answers
114 views

Is there a way to make this one-liner faster?

Context I have a directory of thousands of zip files that are dated in the form YYYYMMDD_hhmmss.zip and each about 300K. Within each zip file is about 400 xml files each about 3K. The problem I ...
5
votes
2answers
1k views

Reformatting a large number of XML files

I'm manipulating a large number of XML files scattered throughout a nested directory structure. I tried the following (which almost works): $ find . -name "*.xml" -type f | xargs -- xmllint --format ...
5
votes
3answers
249 views

Can you pipe to a .bash_profile function?

I received a great function for highlighting files in Apple's finder using the command-line. It's basically a wrapper for osascript. I got it from Mac OS X: How to change the color label of files ...
5
votes
2answers
2k views

Provide parameters to scp with xargs

When I run svn st | awk '{print $2}', I get: a.py b.py c.py Then I want to scp these files to a remote server. I have tried: svn st | awk '{print $2}' | xargs scp my_name@my_server:~/ But it ...
5
votes
4answers
89 views

Have xargs use alias instead of binary

Bash 4.2 on CentOS 6.5: In my ~/.bash_profile I have a bunch of aliases, including: alias grep='grep -n --color=always' so that I can get color highlighting and print line numbers automatically ...
5
votes
2answers
3k views

find + xargs: argument line too long

I have a line like the following: find /foo/bar -name '*.mp4' -print0 | xargs -i {} -0 mv -t /some/path {} but I got the following error: xargs: argument line too long I am confused. Isn't the ...
5
votes
3answers
633 views

Batch copy to multiple directories

I have about 9000 files in a directory and I want to mv them into 90 files in 100 directories by filename order, ignoring any remainder. In Copy multiple files from filename X to filename Y?, Caleb ...
4
votes
3answers
134 views

cycle through one command's output and use in another command [duplicate]

How can I use the output of one command - line-by-line - into another command? I know how to do this with the | pipe symbol, but this uses the entire output in one command. I would like to go line by ...
4
votes
2answers
498 views

Append line to many files

I want to add some text to over 200,000 files I am trying this find . -name *.txt -print | xargs -I % echo "hello world" >> % But nothing is happening. When i run find . -name *.txt it work ...
4
votes
4answers
2k views

Xargs and rm with a *

I am trying to execute the following command ls -d a* | xargs -i sudo rm -rf {}/* The problem is that when I add the /* part the command does not work. I want to remove the directory contents (not ...
4
votes
4answers
2k views

Remove numbers from filenames

I've a problem modifying the files' names in my Music/ directory. I have a list of names like these: $ ls 01 American Idiot.mp3 01 Articolo 31 - Domani Smetto.mp3 01 Bohemian rapsody.mp3 01 Eye of ...
4
votes
3answers
334 views

Use command grep and locate

How I can make the grep command locate certain words in the files specified by the routes found by the locate command? locate my.cnf | grep user (I want that grep command search the word "user" ...
4
votes
2answers
477 views

Get the complement of the result of an ls command

Let's say I have a directory with multiple files, all of which are either binary files (files with no declared extensions) and source files (.c extension). I do this: $ ls and get: README.md ...
4
votes
3answers
4k views

Pass xargs argument as a string to another command with '>'?

Suppose I have a bunch of files in /tmp/. I do find . -type f | xargs -iFILES And I want to do a md5sum in each file, outputting to a file with the same name, but with .md5 extension. find . ...
4
votes
4answers
980 views

xargs and vi - “Input is not from a terminal”

I have about 10 php.ini files on my system, located all over the place, and I wanted to quickly browse through them. I tried this command: locate php.ini | xargs vi But vi warns me Input is not ...
4
votes
2answers
123 views

What is the syntax for passing the output of a command to xargs?

As an example I am using xdotool to obtain the list of windows on my desktop, which outputs a list of numbers, one on each line. An example command for that is: xdotool search --name "a_program", ...
4
votes
3answers
588 views

`grep -l .. | xargs vim` generates a warning, why?

If I run a command like grep -rl test . | xargs vim I get a warning "Vim: Warning: Input is not from a terminal." But I am still able to edit the files. Why the warning?
4
votes
4answers
4k views

copying multiple files to multiple directories

I've a problem copying many files in different directories. Let me explain better: Let's say I have the following in a dir: $ ls file1 file2 file3 file4 file5 dir1 dir2 dir3 and I want to copy ...
4
votes
1answer
219 views

Why did using xargs fail in this case?

I tried to understand the usage of xargs and did the following experiment. ls | xargs | touch I want to refresh the files dates and directoris in the curent directory. Though it is a bit silly,for ...
4
votes
6answers
1k views

Grep a directory and return list with line numbers

I'm currently trying to learn more about bash scripting and all of that fun stuff, and I pieced together this little command: find $path | xargs grep -n $pattern | awk '{print $1}' While this DOES ...
4
votes
3answers
113 views

zsh's “echo SomeText >> **/filename” in other shells

I need to fill all files with a specific filename (recursively) with a text. In zsh this can be done with echo SomeText > **/TheFileName I search generic solutions for sh-compatible and/or tcsh ...
4
votes
2answers
375 views

Decrypt files encrypted with gpg using xargs

I have a lot of files encrypted with gpg. All files have the same password. Is it possible to use xargs to decrypt files? ls | xargs -n 1 gpg asks for the password for every file.
4
votes
2answers
86 views

Why is the default choice for -I argument '{}'?

It seems you can pass in a variable name like 'file' for -I argument of xargs, but there are a plethora of examples that use {}. Is this because of the backward compatibility? Or does it help with the ...
4
votes
1answer
2k views

Moving files with find + xargs: target is not a directory?

When I run the following command: > mkdir some_dir > find /foo/bar/ -name '*.csv' -print0 | xargs -0 mv {} some_dir I get hundreds of lines that say: mv: target `/foo/bar/XX.csv` is not a ...
4
votes
2answers
1k views

Why doesn't this xargs command work?

I wanted to delete all .sh extensions so did this: ls *.sh | xargs -I {} mv {} `basename {} .sh` However it doesn't work, it behaves like basename returns unchanged file name. Why is it behaving ...
4
votes
1answer
82 views

Why do bash variables disappear when I use xargs?

I have a simple bash script and want to count how often a command gets called. Here is a minimal example, the counter is called c and should be 4 at the end: #!/bin/bash c=0; for a in X Y; do for ...
4
votes
1answer
3k views

“Argument list too long”: How do I deal with it, without changing my command?

When I run a command like ls */*/*/*/*.jpg, I get the error -bash: /bin/ls: Argument list too long I know why this happens: it is because there is a kernel limit on the amount of space for ...
4
votes
2answers
576 views

Is there any way to use xargs across a pipe?

I'm trying to automatically convert flac files moved to a folder into mp3 in another folder. My current line of code is this: inotifywait -m -r -q -e moved_to --format "'%w%f'" ~/test | xargs -I x ...