I have a few lines in a shell script whose function is to iterate through directories, match some names, and delete them. Some areas of the file system are write protected and I would like to leave those alone but redirect the prompt or some other indication of the error into a log when they are encountered.
rm -r /path/to/directory at that point in the script causes the script to output
rm: descend into write-protected directory /path/to/directory/subdirectory?
So it hangs and [eventually] times out when left to its own devices.
I attempted to correct this behavior by piping "n" to that prompt with
yes n | rm -rv /path/to/directory
This produces two undesired results:
yes: standard output: Broken pipe; yes: write errorfor every directory that is removed, and
- Not returning the
rm: descend into write-protected...message to stdout.
What is the right way to do this?
According to @roaima I am not actually piping
yes n to the prompt at all because the existence of that pipe makes
rm non-interactive. This is ironic because I was really exploiting the interactivity just to get some output but if I turn off interactivity the program will just exit in error. I don't want to ignore the error (e.g. by appending
|| true as I have seen so many other places on SE) because I want the names of the protected files.