find
isn't able to ‘restore initial working directory’ when run behind sudo -u
if the initial working dir is not visible to the user find runs as. This causes find to always print an annoying Permission denied warning message:
$ pwd
/home/myuser
$ sudo -u apache find /home/otheruser -writable
find: failed to restore initial working directory: Permission denied
What is the best way to prevent find from printing this message?
One method is to change to a directory that the find user can restore, such as cd /
, before running find. Ideally, I'd just like an option for find such as --do-not-restore-initial-working-directory
but I guess that's not available. 😉
I'm mostly using RedHat-based distributions.
Best Answer
Cleanup appears to be a non-optional part of the execution of
find
.https://github.com/Distrotech/findutils/blob/e6ff6b550f7bfe41fb3d72d4ff67cfbb398aa8e1/find/find.c#L231
in
main
infind.c
cleanup
callscleanup_initial_cwd
https://github.com/Distrotech/findutils/blob/e6ff6b550f7bfe41fb3d72d4ff67cfbb398aa8e1/find/util.c#L534
and
cleanup_initial_cwd
actually changes the directoryhttps://github.com/Distrotech/findutils/blob/e6ff6b550f7bfe41fb3d72d4ff67cfbb398aa8e1/find/util.c#L456
You could try using a shell script that
cd
s into/
first, as you suggested. (there are some issues with this script, it can't handle multiple directories to search, for instance)You can also filter the output of stderr to remove the unwanted message