2

I remove some files using trash-cli:

$ pwd
/tmp/test
$ sudo trash mfile 
$

Then I look for where the deleted file is stored:

$ 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

Why does filename expansion not work in the second command?

Thanks.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Tim
  • 101,790

1 Answers1

6

The first ls shows that /.Trash/0/ is only readable by root (the first line, the one for .). Since you're using sudo, you probably aren't root, so your shell can't list the contents of the directory and can't expand the *. ls /.Trash/0/ without the sudo would probably give Permission denied.

This is similar to how sudo echo foo > /some/path/filename doesn't work, if /some/path is writable only by root: The shell processes expansions and redirections before running sudo. You'd need sudo bash -c 'ls /.Trash/0/*' or such to have the command line processed under sudo.

ilkkachu
  • 138,973
  • Thanks. By sudo, didn't I get superuser privillege to read /.Trash/0/? – Tim May 19 '18 at 22:51
  • 2
    Sure you did, @Tim, but the shell performs pathname expansion on the (whole) command before executing it. In this case, that means it does the expansion with your ordinary, unprivileged identity, because it is only by running the (expanded) command that you obtain privilege. By then, it is too late. – John Bollinger May 20 '18 at 03:46