我可以告诉查找到不还原初始工作目录吗?


8

findsudo -u如果初始工作目录对用户不可见,则无法在后面运行时“恢复初始工作目录” 。这导致find总是打印出令人讨厌的“ 权限被拒绝”警告消息:

$ pwd
/home/myuser
$ sudo -u apache find /home/otheruser -writable
find: failed to restore initial working directory: Permission denied

阻止查找打印此消息的最佳方法是什么?

一种方法是cd /在运行find之前,更改为find用户可以还原的目录。理想情况下,我只想找到诸如这样的选项,--do-not-restore-initial-working-directory但我想那是不可用的。;)

我主要使用基于RedHat的发行版。

Answers:


5

清理似乎是执行的非可选部分find

https://github.com/Distrotech/findutils/blob/e6ff6b550f7bfe41fb3d72d4ff67cfbb398aa8e1/find/find.c#L231

mainfind.c

  cleanup ();
  return state.exit_status;
}

cleanup 来电 cleanup_initial_cwd

https://github.com/Distrotech/findutils/blob/e6ff6b550f7bfe41fb3d72d4ff67cfbb398aa8e1/find/util.c#L534

cleanup_initial_cwd实际上更改目录

https://github.com/Distrotech/findutils/blob/e6ff6b550f7bfe41fb3d72d4ff67cfbb398aa8e1/find/util.c#L456

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

如您建议的那样,您可以尝试使用首先cd插入的shell脚本/。(此脚本存在一些问题,例如,它无法处理要搜索的多个目录)

#!/bin/sh
path="$(pwd)/$1"
shift
cd /
exec find "$path" "$@"

您还可以过滤stderr的输出以删除不需要的消息

#!/bin/sh
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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.