57

Suppose that I want to delete all files in a folder that are greater than 1 MB.

$ find . -size +1M | xargs -0 rm

This will not delete files that have space in their names. So I want it to quote all arguments it sends to rm. If find gives it Some report.docx it should pass "Some report.docx" to rm.

How can I do that?

5

5 Answers 5

50

I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.

find . -size +1M | xargs -I {} rm "{}"
4
  • With xargs -I, you'll still have problems with filenames that contain single quotes, double quotes, backslashes or newlines (leading blanks are also a problem, but with the output of find ., that can only occur for filenames that contain <newline><blanks>). Feb 20, 2021 at 7:28
  • 8
    Quoting that second {} makes absolutely no difference here. That's just quoting for the shell, and that {} doesn't need quoting in most shells. As far as xargs is concerned, it receives an argument containing {} (the two characters { and }) for both the {} after -I and "{}" after rm. It is exactly the same as xargs -I {} rm {} or xargs -I "{}" rm '{}' Feb 20, 2021 at 7:43
  • Just escape the quotes like this: find . -size +1M | xargs -I {} rm \"{}\"
    – lots0logs
    Aug 8, 2022 at 22:37
  • It simply don't work. It gets only the first element (tested with tar). See unix.stackexchange.com/a/635479/41011 instead Mar 2 at 8:50
27

As you're already using that non-standard 1M, chances are your find implementation also supports -delete. So, simply use:

find . -type f -size +1M -delete

Where supported, that's by far the safest and most efficient.

If you insist on using xargs and rm with find, just add -print0 in your command:

find . -type f -size +1M -print0 | xargs -r0 rm -f --

(-print0 and -0 are non-standard, but pretty common. -r (to avoid running rm at all if find doesn't find anything) is less common, but if your xargs doesn't support it, you can just omit it, as rm with -f won't complain if called without argument).

The standard syntax would be:

find . -type f -size +1048576c -exec rm -f -- {} +

Other way:

find . -type f -size +1M -execdir rm -f -- {} +

(that's safer than -exec/xargs -0 and would work with very deep directory trees (where full file paths would end up larger than PATH_MAX), but that's also non-standard, and runs at least one rm for each directory that contains at least one big file, so would be less efficient).

From man find on a GNU system:

-print0

True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.

4
  • 7
    This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though. Dec 10, 2014 at 8:33
  • 3
    @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.] Dec 10, 2014 at 21:22
  • 1
    @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc. Dec 11, 2014 at 6:36
  • On OSX I do xargs -0 instead of xargs -r0.
    – Rolf
    Sep 13, 2018 at 15:56
15

Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.

6

Here is a simple way to do it that does not rely on special flags in find. It works for any input into xargs. Just use sed to wrap each line in quotes.

find . -size +1M | sed 's/.*/"&"/' | xargs ls -l

You can replace ls -l with rm after you are satisfied it works.

3
  • With sed 's/.*/"&"/' quoting, you'll still have problems with filenames that contain " or newline characters or sequences of bytes that don't form valid characters in the locale. Feb 20, 2021 at 7:08
  • Stéphane: Good catch regarding the -delete and double quotes in file name. I have removed the -delete. Regarding escaping quotes it needs a more robust sed expression. Do you have any suggestions on how to fix this? I’ll think about it.
    – Asim Jalis
    Feb 21, 2021 at 14:50
2

Actually just reiterating an existing comment to the question so that it appears under answers, in my case I was using locate to find filenames containing a certain string:

Ivan Zakharyaschev wrote: Use xargs -d$'\n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

So for example using Ivan's suggestion,

$ locate -i example_string | xargs -d$'\n' md5sum

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .