0

I have the following directory listing:

  0 -rw-r--r-- 1 root root      0 Sep  2 15:19 aws.greengrass.LambdaLauncher.log
  0 -rw-r--r-- 1 root root      0 Sep  2 15:19 aws.greengrass.LambdaRuntimes.log
  0 -rw-r--r-- 1 root root      0 Sep  2 14:53 aws.greengrass.Nucleus.log
 80 -rw-r--r-- 1 root root  75017 Sep  2 15:55 greengrass_2022_09_02_15_0.log
 40 -rw-r--r-- 1 root root  36930 Sep  2 16:50 greengrass_2022_09_02_16_0.log
216 -rw-r--r-- 1 root root 217065 Sep  2 20:40 greengrass_2022_09_02_20_0.log
 96 -rw-r--r-- 1 root root  92764 Sep  2 21:54 greengrass_2022_09_02_21_0.log
 64 -rw-r--r-- 1 root root  58307 Sep  2 22:57 greengrass_2022_09_02_22_0.log
 48 -rw-r--r-- 1 root root  46475 Sep  6 14:37 greengrass_2022_09_06_14_0.log
 16 -rw-r--r-- 1 root root  14845 Sep  6 17:57 greengrass_2022_09_06_17_0.log
 40 -rw-r--r-- 1 root root  39037 Sep  6 18:11 greengrass_2022_09_06_18_0.log
184 -rw-r--r-- 1 root root 186318 Sep  6 19:48 greengrass_2022_09_06_19_0.log
 12 -rw-r--r-- 1 root root  10793 Sep  6 20:25 greengrass_2022_09_06_20_0.log
124 -rw-r--r-- 1 root root 122363 Sep  6 21:43 greengrass.log

I want to delete any file who's name starts with greengrass. I have tried these wildcard commands but none of them work:

sudo rm /greengrass/v2/logs/greengrass*.*
sudo rm /greengrass/v2/logs/greengrass*
sudo rm /greengrass/v2/logs/greengrass*.log

I get:

rm: cannot remove '/greengrass/v2/logs/greengrass*.*': No such file or directory

2 Answers2

3

What is happening is all about glob expansion.

when you run:

 sudo rm dir/*

the shell running the sudo tries to expand the '*' wildcard. If it cannot read the directory, then it passes the wildcard to sudo as is.

sudo executes rm with dir/* and rm does not do glob expansion, only shells do. rm is looking for a file called * which is a legal (but unusual) filename.

If the directory was readable by the user running sudo, then the acutall sudo command would be:

sudo rm dir/filea dir/fileb dir/filec

which would have worked. since you want root to do the glob expansion you will need to use a shell as follows

sudo sh -c "rm dir/*"

Then sudo will run a shell as root, that will run the command "rm dir/*" since shell understand how to expand globs, then it will be turned into "rm dir/filea dir/fileb ..."

toppk
  • 657
0

Is the directory /greengrass/v2/logs readable by your regular user? If not, it'll do this because the wildcard needs to be expanded to a list of matching files by the shell (before it's passed to sudo, which passes it on to rm), and if the shell can't list the contents of the directory... that doesn't happen.

The easiest solution is probably to use sudo -s to open a root shell, then run rm /greengrass/v2/logs/greengrass*.* from that root shell. And then use either exit or Control-D to exit the root shell and go back to normal.