Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why wildcard doesn't work in `sudo rm` statement?

I was trying to delete all files in log directory and using default bash shell on CentOS 6.5

[lei@ids7gueywjZ /]$ sudo ls -al /var/log/jenkins/
total 1541512
drwxr-x---   2 jenkins jenkins       4096 Jul 22 09:52 .
drwxr-xr-x. 10 root    root          4096 Jul 14 21:27 ..
-rw-r--r--   1 jenkins jenkins      31483 Jul 22 17:07 jenkins.log
-rw-r--r--   1 jenkins jenkins 1073606656 Jul 18 03:16 jenkins.log-20150718
-rw-r--r--   1 jenkins jenkins  504815011 Jul 19 03:30 jenkins.log-20150719.gz
[lei@ids7gueywjZ /]$ sudo rm -r /var/log/jenkins/*
rm: cannot remove `/var/log/jenkins/*': No such file or directory

I don't understand why rm -r /var/log/jenkins/* doesn't work? Is there some default shell configuration I was missing?

like image 256
LeoShi Avatar asked Jul 22 '15 09:07

LeoShi


People also ask

How do wildcards work in bash?

When we need to search for anything using shell commands then we need to define a pattern for searching. Wildcard characters are used to define the pattern for searching or matching text on string data in the bash shell. Another common use of wildcard characters is to create regular expressions.

What happens if you type sudo rm rf?

In short, the sudo rm -rf / command deletes everything in the root directory, which basically breaks your whole system. We'll explain the command in detail below. If you don't know what it means or what it does – you should not run it.

What is Linux wildcard?

A wildcard in Linux means it might be a symbol or set of symbols representing other characters. It is generally used in substituting any string or a character.

How do I use sudo rm?

You have to use the recursive option -r with the rm command. And thus ultimately, rm -rf command means recursively force delete the given directory. If you add sudo to the rm -rf command, you are deleting files with root power. That means you could delete system files owned by root user.


1 Answers

The wildcard expansion is done by the shell, not by rm.

And the shell does not have sudo rights, only rm does.

So since the shell does not have permission to read /var/log/jenkins, there is no expansion, and rm attempts to delete the file (not the wildcard) /var/log/jenkins/* -- which does not exist.

To get around this, you need a shell with sudo rights executing your rm:

sudo sh -c 'rm /var/log/jenkins/*'
like image 112
DevSolar Avatar answered Oct 09 '22 14:10

DevSolar