在sh脚本里执行ls -t /home/| sed -n '5,$p' | xargs -I {} rm -rf {},删除不了/home/下面的文件
时间: 2024-04-15 09:29:13 浏览: 80
如果你在Shell脚本中执行 `ls -t /home/| sed -n '5,$p' | xargs -I {} rm -rf {}` 命令来删除 `/home/` 目录下的文件,但是没有成功删除文件,可能是因为文件名中包含特殊字符或空格导致命令无法正确处理。
为了解决这个问题,你可以尝试使用 `find` 命令来删除目录下的文件,它可以更好地处理特殊字符和空格。你可以将以下内容保存为一个Shell脚本并执行:
```bash
#!/bin/bash
find /home/ -type f -printf '%T@ %p\n' | sort -nr | tail -n +5 | awk '{$1=""; print $0}' | xargs -I {} rm -rf {}
```
上述脚本使用 `find` 命令来获取 `/home/` 目录下的所有文件,并按照修改时间进行排序。然后使用 `tail` 命令来截取排序后的结果,保留从第5行开始的所有行。接下来使用 `awk` 命令去除每行中的时间戳部分,最后使用 `xargs` 命令来逐个删除文件。
请注意,在删除文件时要谨慎操作,确保你要删除的文件是正确的。在尝试运行脚本之前,最好先进行备份或测试。
阅读全文
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)