大网数据广告活动
服务器运维问答

rm -rf cannot delete session files

· 52CE 我爱测

1. list the files in session dir $ /var/lib/php/sessions$ sudo ls -la total 20 drwx-wx-wt 2 root root 12288 May 20 14:26 . drwxr-xr-x 4 root root 4096 Feb 26 21:15 .. -rw------- 1 www-data www-data 0 May 20 14:26 sess_716a24lf4ts…

问题原文

原帖链接 · Samuel Lui · CC BY-SA 3.0

1. list the files in session dir

$ /var/lib/php/sessions$ sudo ls -la
total 20
drwx-wx-wt 2 root     root     12288 May 20 14:26 .
drwxr-xr-x 4 root     root      4096 Feb 26 21:15 ..
-rw------- 1 www-data www-data     0 May 20 14:26 sess_716a24lf4tsnmfucpq50uf1pk7
-rw------- 1 www-data www-data   124 May 20 14:26 sess_fkif8ed8k85olh53q9so1cd6s3
-rw------- 1 www-data www-data     0 May 20 14:25 sess_ov6nrn16gg81i44u9angk3bls6

2. sudo delete all files

$ /var/lib/php/sessions$ sudo rm -rf ./*

3. But the session files still existed!

$ /var/lib/php/sessions$ sudo ls -la    
total 20
drwx-wx-wt 2 root     root     12288 May 20 14:26 .
drwxr-xr-x 4 root     root      4096 Feb 26 21:15 ..
-rw------- 1 www-data www-data     0 May 20 14:26 sess_716a24lf4tsnmfucpq50uf1pk7
-rw------- 1 www-data www-data   124 May 20 14:26 sess_fkif8ed8k85olh53q9so1cd6s3
-rw------- 1 www-data www-data     0 May 20 14:25 sess_ov6nrn16gg81i44u9angk3bls6

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 3.0

The problem here is that path name expansion occurs BEFORE the sudo. The path name expansion for ./* doesn't have permission to see the directory contents. Therefore, it would not get expanded to rm -rf ./sess_716a24lf4tsnmfucpq50uf1pk7 ./sess_fkif8ed8k85olh53q9so1cd6s3 ./sess_ov6nrn16gg81i44u9angk3bls6 as supposed – and as it would, if the whole command was run as root.

You can launch a new terminal inside sudo. Try:

sudo sh -c "rm -rf /var/lib/php/sessions/*"

Notice that I used the full path because it's much more safe than a relative path. Once a colleague typed / instead of ./ inside sudo rm -rf. You can imagine what happened: unnecessary restoring task for me... and one user less in the sudoers list.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。