为什么文件名扩展在这个例子中不起作用?

Tim*_*Tim 2 bash sudo filenames wildcards

我使用trash-cli以下方法删除一些文件:

$ pwd
/tmp/test
$ sudo trash mfile 
$
Run Code Online (Sandbox Code Playgroud)

然后我查找已删除文件的存储位置:

$ sudo ls /.Trash/0/ -la
total 16
drwx------ 4 root root 4096 May 19 16:52 .
drwx------ 3 root root 4096 May 19 16:52 ..
drwx------ 2 root root 4096 May 19 16:52 files
drwx------ 2 root root 4096 May 19 16:52 info

$ sudo ls /.Trash/0/* -la
ls: cannot access '/.Trash/0/*': No such file or directory

$ sudo ls /.Trash/0/files/ -la
total 12
drwx------ 2 root root 4096 May 19 16:52 .
drwx------ 4 root root 4096 May 19 16:52 ..
-rw-rw-r-- 1 t    t       6 May 19 16:48 mfile
Run Code Online (Sandbox Code Playgroud)

为什么文件名扩展在第二个命令中不起作用?

谢谢。

ilk*_*chu 6

第一个ls显示/.Trash/0/只能由 root 读取(第一行,用于.)。由于您使用的是sudo,您可能不是 root,因此您的 shell 无法列出目录的内容,也无法扩展*. ls /.Trash/0/没有sudo可能会给Permission denied

这类似于如何sudo echo foo > /some/path/filename不起作用,如果/some/path只能由 root 写入:shell 在运行之前处理扩展和重定向sudo。您需要sudo bash -c 'ls /.Trash/0/*'sudo.

  • 当然,@Tim,但是外壳程序在(整个)命令 *before* 执行它之前执行路径名扩展。在这种情况下,这意味着它使用您普通的非特权身份进行扩展,因为只有通过运行(扩展)命令才能获得特权。到那时,为时已晚。 (2认同)