[英]Using command “ls -t | tail -n +4 | xargs rm --” to remove files in another location
我有 RaspBerry Pi 项目,其中 A/D 转换器将 5 分钟长的数据文件收集到数据文件夹中,除最新数据之外的所有数据将每小时通过 dataFlush.py 脚本和 crontab 删除。 dataFlush.py 的路径是“/home/pi”,数据文件夹的路径是“/home/pi/dataLog”。 那么如何将 dataLog 路径添加到此命令中:
os.system('ls -t | tail -n +2 | xargs rm --')
我尝试了这样的事情,但没有奏效:
os.system('ls /home/pi/dataLog -t | tail -n +2 | xargs rm --')
ls(1)
在您的情况下不会生成完整路径输出,因此您可能希望在每一行前面加上“前缀”:
os.system('ls /home/pi/dataLog -t | tail -n +2 | sed 's|^|/home/pi/dataLog/|' | xargs rm --')
另一种选择是使用find(1)
:
os.system('find /home/pi/dataLog | xargs ls -t -- | tail -n +2 | xargs rm --')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.