Shell – `xargs` with spaces in filenames

filenamesquotingshellxargs

I'm trying to list only non-image files, searching only in the most recent 500 files. So I run

ls -t | head -500 | file | grep -v 'image'

which isn't right: it displays a help message. Changing it to

ls -t | head -500 | xargs file | grep -v 'image'

I now sometimes get the output I want, but if the filename has spaces in it—for example Plutonian\ Nights\ -\ Sun\ Ra.mp3—then xargs will run file Plutonian, file Nights, etc.


How do I either help xargs see the spaces, or otherwise accomplish what I'm trying to accomplish?

Best Answer

Using xargs, it can be done in this way:

find . -type f -print0 | xargs -0 file | grep -v 'image' 

But xargs is so yesterday. The cool kids use parallel today. Using parallel, it would be:

find . -type f | parallel file | grep -v 'image'

See. No use of -print0 and -0. parallel is really smart by itself.

UPDATE

For listing only the most recent 500 files, your command would be:

ls -1t | head -500 | parallel file {} | grep -v image

Important

In case your parallel is old and above syntax doesn't work, then install the new version of parallel as explained here: http://www.gnu.org/software/parallel/parallel_tutorial.html

Related Question