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
$ 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.

1 Answer 1


Cleanup appears to be a non-optional part of the execution of find.


in main in find.c

  cleanup ();
  return state.exit_status;

cleanup calls cleanup_initial_cwd


and cleanup_initial_cwd actually changes the directory


static void
cleanup_initial_cwd (void)
  if (0 == restore_cwd (initial_wd))
      free_cwd (initial_wd);
      free (initial_wd);
      initial_wd = NULL;
      /* since we may already be in atexit, die with _exit(). */
      error (0, errno,
         _("failed to restore initial working directory"));
      _exit (EXIT_FAILURE);

You could try using a shell script that cds into / first, as you suggested. (there are some issues with this script, it can't handle multiple directories to search, for instance)

cd /
exec find "$path" "$@"

You can also filter the output of stderr to remove the unwanted message

exec 3>&2
exec 2>&1
exec 1>&3
exec 3>&-
3>&2 2>&1 1>&3 3>&- find "$@" | grep -v "^find: failed to restore initial working directory"
# not sure how to recover find's exit status
exit 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .