2

I'm reading how Linux works by Brian Ward and he has issued the following warning "Don't try options such as -exec before you know this form by heart" find -name file -print

Thus far, I have not understood why that form in particular is so important. Why might I not want to search for directories in a particular place regardless of their name. Also, it seems to me that -print is a default option and the manpage strongly suggests using -print0 as some file names may include new line characters.

3

The author cautions the reader against trying -exec without understanding the structure and approach of the find command.

There is no harm in you trying find -type d -print, for example. CAUTION But do you understand what would happen if you ran this find -depth -type d -exec mv {} {}.surprise \;?

If you want to see what would happen, replace (or supplement) the mv with a command that performs no permanent action:

find -depth -type d -exec echo mv {} {}.surprise \;

With regard to -print0, that is only of use when working within a pipeline. (But it's very useful there when your other utilities support its NUL-separated records.) If you want simply to list the find output for visual inspection, use -print instead.


PS. When you want to recover from having run the dangerous command on an important part of your filesystem you can probably run find -depth -type d -print0 | while IFS= read -r -d '' d; do mv "$d" "${d%.surprise}"; done. But it would have been better to try it in a safe part of /tmp.

  • 1
    That command in the last paragraph won't undo the original code. The directories will have been renamed with .surprise suffixes. Your code will then try to move dirname.surprise.surprise, which doesn't exist. – Barmar Jul 11 at 21:20
  • 1
    @Barmar thanks. I wasn't thinking straight. Now fixed. – roaima Jul 11 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.