$ sudo -iu abc ls -ltr /sites/servers/server_instance/logs/access*
ls: cannot access /sites/servers/server_instance/logs/access*: No such file or directory
$ sudo -iu abc ls -ltr /sites/servers/server_instance/logs/
total 594812
-rwxrwxrwx 1 abc abc 45 Mar 21 12:42 old.log
-rwxrwxrwx 1 abc abc 304537970 Mar 24 12:45 console.log
-rwxrwxrwx 1 abc abc 304537970 Mar 24 13:20 access_nginx.log
Can anyone explain why this happens? I am stuck on a script due to this.
Best Answer
One possibility is that you don't have permissions to access one or more of the directories in that path (
/sites/servers/server_instance/logs
). The wildcard expansion is carried out by your shell, and then the expanded paths are passed to thesudo
command.If your user doesn't have permissions, expansion wouldn't work in the first command. It would be run as-is (
ls -ltr /sites/servers/server_instance/logs/access*
), and there isn't a file literally namedaccess*
). Ifabc
does have the required permissions for all the directories in the path, the second command, which didn't have any wildcards, would be untouched by your shell, and it would work fine.