Twitter Updates

Tuesday, 10 February 2009

Using Xargs on Mac Terminal

Xargs is used to run commands on the output from other command line programs.
The OS X version is missing the default -I {} that other *nix sytems seem to have.
The following command will search for and print files matching *something*. The xargs is redundant here but gives a good frame work.

$ find . -iname *something* | xargs -I {} echo {}

Or move matching files to another location.
$ find . -iname *something* | xargs -I {} mv {} ../Other/Location

13 comments:

Anonymous said...

Thank you, good Lord, Thank You.

Days of searching produced constant frustration with
find . | xargs cp (or mv) {}
and a nearly infinite permutation of same.

Doesn't Bleedin' Work On The Stupid Mac.

Your site is the Only Place I found the key.

Which Works. Caloo, Callay . . .

Stupid Mac.

And yes, I own a few Macs.
And some WinTels, which sit ignored in their little cases.
Poor little WinTels.

Stupid Macs.

Thank you.
Yes, again.

Morgy said...

Thank you, am glad I could help.
I like macs but they do have some stupid parts, like non gnu defaults.

Poor WinTels.

wdef said...

Excellent tip, thanks. This provides undoubtedly the easiest way to batch rename files whose filenames contain spaces:

Eg The following will change the extension of files in the current directory from .fla to .flac

$ ls *fla | xargs -I {} mv {} {}"c"

Brilliant!

wdef said...

Proviso:

$ ls *fla | xargs -I {} mv {} {}"c"

xargs has a problem here with filenames beginning with a space or containing an apostrophe - watch out for French filenames containing things like "qu'a".

Anonymous said...

If you are using MacPorts, just use gxargs.

Ali Ayoub said...

thanks

Anonymous said...

Yep, your post did it for me too. I references it on stack overflow

My Brushes said...

Really great news!!! this information is well worth looking everyone. Good tips. I will be sharing this with all of my friends! Thank you for sharing valuable information.
__________________________
Graphic Design Software for Mac

John said...

Fantastic. Exactly what I needed to copy just the PDF files from a whole slew of subdirectories into another one on my Mac. Thanks much.

李洋 said...
This comment has been removed by the author.
李洋 said...

it does not work when the file name contains space.

Anonymous said...

For files with embedded spaces:

If you quote the placeholder, it should work. E.g. for a file name "a file.txt" (no quotes), the following changes it to "afile.txt" (again, no quotes): ls *file.txt | xargs -I '{}' mv '{}' afile.txt

Sumit Kumar said...

This blog is really helpful to deliver updated educational affairs over internet which is really appraisable. I found one successful example of this truth through this blog. I am going to use such information now. digital watermarking software