3

I would like to take the results from this command:

find . -type f -size +200M -exec ls -lh {} \;

store the content to a variable of some form, and then delete the found files.

How can I do this?

Excellll
  • 12,717
Dave
  • 31
  • Its extremely complex to do exactly this due to the difficulties induced by the "-lh" parameter on LS. Although there are a number of ways of getting "partially there". If you can describe your goal, maybe we can provide an elegant solution, otherwise you are looking at very complex coding to encode and decode the output. Also, do you really need to store the content as a variable - it would be easier to work with if you stored it as a file [ on temporary/memory storage even ] rather then dealing with linefeed issues. – davidgo Dec 11 '15 at 19:57

1 Answers1

1

You don't need to store the results of find in variable only to be able to remove the files (unless you've some specific requirements which you didn't mention), as find provides special -delete parameter which would do that for you:

find . -type f -size +200M -print -delete

As parsing output of ls command is not advised.

If you really want to parse the files returned by find, you need to do something like:

find . -type f -size +200M -print0 | xargs -0 rm -v

Since you can't keep shell variables with null characters, and you don't want to break the script on spaces (which can cause giant bugs), the workaround could be to store the list in the temporary file or serialize the data (e.g. base64).

kenorb
  • 25,417