未经适当许可不能扩展星号

ima*_*his 19 shell permissions sudo wildcards

有人可以向我解释以下内容吗?

$ ls -ld /temp/sit/build/
dr-xr-s--- 3 asdf qwer 4096 Jan 31  2012 /temp/sit/build/

$ ls -ld /temp/sit/build/*
ls: /temp/sit/build/*: Permission denied
Run Code Online (Sandbox Code Playgroud)

所以显然,我不能在这里使用星号。我已经用 sudo 命令尝试过它,但我收到“没有这样的文件”错误而不是“权限被拒绝”......

sudo ls -l /temp/sit/build/*
ls: /temp/sit/build/batch*: No such file or directory
Run Code Online (Sandbox Code Playgroud)

但如果我不使用 * 它最终会起作用

sudo ls -l /temp/sit/build/
total 4
dr-xr-s--- 11 asdf qwer 4096 Oct  3 23:31 file
Run Code Online (Sandbox Code Playgroud)

Gil*_*il' 25

进行*通配符扩展的 shell 是您键入它的 shell。如果 shell 有权读取目录中的文件列表,则它会扩展/temp/sit/build/*/temp/sit/build/file,并sudo使用参数ls,-l和运行/temp/sit/build/file。如果 shell 无法找到任何匹配项/temp/sit/build/*(无论是因为没有匹配项,还是因为 shell 无权查看匹配项),则它会单独保留模式,并sudo使用参数ls,-l和调用/temp/sit/build/*

由于没有名为/temp/sit/build/*的文件,因此ls如果您将其传递给该名称,则该命令会报错。回想一下,ls不扩展通配符,这是 shell 的工作。

如果您希望通配符扩展发生在您没有读取权限的目录中,那么扩展必须发生在由 启动的 shell 中,sudo而不是在调用sudo. sudo不会自动启动一个 shell,你需要明确地这样做。

sudo sh -c 'ls -l /temp/sit/build/*'
Run Code Online (Sandbox Code Playgroud)

在这里,当然,您可以sudo ls -l /temp/sit/build/改为这样做,但这并不能推广到其他模式。