我有一个愚蠢的问题
usr01@srvlnx01:~$ sudo ls /var/opt/gitlab/backups/
1685014011_2023_05_25_13.2.3_gitlab_backup.tar 1685016443_2023_05_25_13.2.3_gitlab_backup.tar tmp
Run Code Online (Sandbox Code Playgroud)
但
usr01@srvlnx01:~$ sudo ls /var/opt/gitlab/backups/*.tar
ls: cannot access '/var/opt/gitlab/backups/*.tar': No such file or directory
Run Code Online (Sandbox Code Playgroud)
我可能做了一些愚蠢的事情。
use*_*686 15
通配符扩展由 shell 完成,而不是由“ls”完成。换句话说,他们不会等待“sudo”生效。
\n当您的 shell 无权查看受保护目录内的文件时,它无法扩展通配符,因此结果是“ls”被要求显示字面上名为*.tar. (如前所述,“ls”本身并不进行通配符扩展。)
作为一般解决方案,让“sudo”首先运行 shell 的另一个副本 \xe2\x80\x93 ,它将使用 root 权限处理通配符扩展:
\nsudo sh -c 'ls /var/bleh/*.tar'\nRun Code Online (Sandbox Code Playgroud)\n(sudo $SHELL -c '...'可能更笼统,但“sh”可以完成这项工作。)
某些程序可能有自己的选项,例如--include=...在程序本身中扩展通配符。不幸的是,“ls”只有--ignore='*.tar'但没有选项仅包含特定名称。