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

14 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.

A Person 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 said...

thanks

Anonymous said...

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

Unknown 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

147 said...

I've got a question about the syntax of mac xargs. I was reading through the tutorials here ...
https://ryanstutorials.net/linuxtutorial/bonus.php#xargs
and came across this command for xargs ...

basename -s .JPG -a *.JPG | xargs -n1 -i mv {}.JPG {}.jpg

to change the case of extensions. It works just fine on linux but the syntax is slightly different for mac. Playing around with it and thanks to this post I was able to modify the command to work on mac, here it is ...

basename -s .JPG -a *.JPG | xargs -n1 -I {} mv {}".JPG" {}".jpg"

I understand the difference b/w -i and -I (though don't really understand the reasoning behind the change) but why the extra {}, what is the purpose? As far as I see it, the first part of the command, gets the basename of all files (ex: image.JPG, image2.JPG, image3.JPG) with a .JPG extension, passes that into the xargs command and replaces the {} with "image". So in the first, linux, command it would go something like this mv image.JPG image.jpg; image2.JPG image2.jpg; image3.JPG image3.jpg. But the second one seems to do as such image.JPG mv image.JPG image.jpg ... and so on. Which doesn't make any sense. Help me understand it please.

Unknown said...

thanks