bash 如何在 Linux 上使用 exec 删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9259665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a file on Linux using exec
提问by glarkou
I am trying to delete a previously created file called a\.
我正在尝试删除以前创建的名为a\.
I tried to use:
我尝试使用:
1)
1)
rm -rf a\
2)
2)
rm -rf a\/
but both are not working. I tried to login to the terminal but I was unable to delete. I wrote aand then clicked taband it shows a\\/but it stills not working. Neither from terminal. I get:
但两者都不起作用。我尝试登录终端,但无法删除。我写了a然后点击tab它显示a\\/但它仍然无法正常工作。无论是从终端。我得到:
sudo rm -rf a\/
rm: cannot remove `a\': Operation not permitted
Any suggestions?
有什么建议?
采纳答案by Sergey P. aka azure
You should use exec("rm -rf a\\\\");in your php code.
您应该exec("rm -rf a\\\\");在您的 php 代码中使用。
Php uses \\to escape \and system uses the same, so you need to escape \twice
php使用\\escape\和system使用相同,所以需要转义\两次
回答by Kinetic
I'm assuming your using Ubuntu due to sudo?
由于 sudo,我假设您使用 Ubuntu?
Firstly make sure you are in the same directory as the file you are trying to delete, do this with:
首先确保您与要删除的文件位于同一目录中,请执行以下操作:
print(getcwd());
Secondly try changing the owner of file your are trying to delete to www-data (or apache depending on your server) and then see if you can delete the file from the php script.
其次,尝试将您要删除的文件的所有者更改为 www-data(或 apache,具体取决于您的服务器),然后查看是否可以从 php 脚本中删除该文件。
If this works then the problem you have is permission related.
如果这有效,那么您遇到的问题与权限有关。
There are a number of ways to solve this problem.
有多种方法可以解决这个问题。
- Make sure the files your are deleting are owned by the web server process (most secure).
- Give the files 777 permissions so any user can delete them.
- Add the web server user to sudoers (/etc/sudoers) (least secure)
- 确保您要删除的文件归 Web 服务器进程所有(最安全)。
- 为文件授予 777 权限,以便任何用户都可以删除它们。
- 将 Web 服务器用户添加到 sudoers (/etc/sudoers)(最不安全)
回答by Aif
You should try to specify the full path of the file using either /path/to/fileor ./a\\. The escape stuff have been explained already.
您应该尝试使用/path/to/file或指定文件的完整路径./a\\。逃生的东西已经解释过了。

