Wednesday, January 26, 2011

Using `find` to delete.

So, given three options...

  1. find .... -delete
  2. find .... | xargs rm ...
  3. find .... -exec rm ...;

..or variations thereof, which option is preferable?
I'm guessing there is no hard and fast answer, and a specific situation will dictate the best option (please name them!)

Cheers.

  • I prefer to use find ... > file.txt review the file extensively, then use find ... -delete so I know the exact same results will be deleted (passing arguments is mostly bulletproof, mostly).

    From Chris S
  • Option 1 will avoid spawning external processes, which is useful under stressed conditions.

    Option 2 will spawn a single xargs process, which will spawn only as many rm processes as necessary. This option is typically used with -print0 and -0 in order to handle filenames with spaces and/or newlines.

    Option 3 will spawn a rm process for each file.

    GNU find allows a fourth option, find .... -exec rm -r {} +, which will run rm with as many filenames as possible in order to spawn only a limited number of them.

    standard : Thanks, nice explanation of each option.
    Brian Knoblauch : I only ever use the xargs method in practice.
  • Be careful, some of them have different behavior. For example if the path has a space in it, the second option using xargs will interpret the path as multiple files and throw errors (and cause other problems).

    From paulusdd

0 comments:

Post a Comment