简体   繁体   English

为什么通配符在`sudo rm`语句中不起作用?

[英]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 我试图删除日志目录中的所有文件,并在CentOS 6.5上使用默认的bash shell

[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? 我不明白为什么rm -r /var/log/jenkins/*不起作用? Is there some default shell configuration I was missing? 我缺少一些默认的shell配置吗?

The wildcard expansion is done by the shell, not by rm . 通配符扩展由shell完成,而不是由rm

And the shell does not have sudo rights, only rm does. shell没有sudo权限,只有rm才有。

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. 因此,由于shell没有读取/var/log/jenkins ,所以没有扩展,并且rm尝试删除不存在的文件 (不是通配符) /var/log/jenkins/*

To get around this, you need a shell with sudo rights executing your rm : 要解决这个问题,你需要一个具有sudo权限的shell来执行你的rm

sudo sh -c 'rm /var/log/jenkins/*'

The directory /var/log/jenkins has no rights for "other". 目录/var/log/jenkins没有“other”的权限。 Even if you run sudo rm -r /var/log/jenkins/* , the shell expansion is done by your user. 即使你运行sudo rm -r /var/log/jenkins/* ,shell扩展也是由你的用户完成的。 You should either remove the whole direcotry ( suro rm -r /var/log/jenkinks ), or do everything as the appropriate user (I'd recommend su -ing with jenkins user). 您应该删除整个suro rm -r /var/log/jenkinkssuro rm -r /var/log/jenkinks ),或者以适当的用户身份执行所有操作(我建议使用jenkins用户进行su -ing)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM