find
sudo -u
如果初始工作目录对用户不可见,则在运行时无法“恢复初始工作目录” 。这会导致 find 总是打印一条烦人的Permission denied警告消息:
$ pwd
/home/myuser
$ sudo -u apache find /home/otheruser -writable
find: failed to restore initial working directory: Permission denied
Run Code Online (Sandbox Code Playgroud)
防止 find 打印此消息的最佳方法是什么?
一种方法是cd /
在运行 find 之前更改为 find 用户可以恢复的目录,例如。理想情况下,我只想找到诸如此类的选项,--do-not-restore-initial-working-directory
但我想这不可用。;)
我主要使用基于 RedHat 的发行版。
清理似乎是find
.
在main
中find.c
cleanup ();
return state.exit_status;
}
Run Code Online (Sandbox Code Playgroud)
cleanup
电话 cleanup_initial_cwd
并cleanup_initial_cwd
实际更改目录
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);
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所建议的,您可以尝试使用首先cd
进入的 shell 脚本/
。(此脚本存在一些问题,例如无法处理多个目录进行搜索)
#!/bin/sh
path="$(pwd)/$1"
shift
cd /
exec find "$path" "$@"
Run Code Online (Sandbox Code Playgroud)
您还可以过滤 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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4450 次 |
最近记录: |