【问题标题】:Using command "ls -t | tail -n +4 | xargs rm --" to remove files in another location使用命令“ls -t | tail -n +4 | xargs rm --”删除另一个位置的文件
【发布时间】:2020-03-12 10:48:55
【问题描述】:

我有 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 --')

【问题讨论】:

    标签: python-3.x linux python-2.7 unix raspberry-pi3


    【解决方案1】:

    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 --')
    

    【讨论】:

      猜你喜欢
      • 2017-08-07
      • 2012-07-19
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2016-08-11
      相关资源
      最近更新 更多