简体   繁体   中英

Using command “ls -t | tail -n +4 | xargs rm --” to remove files in another location

I have RaspBerry Pi project where A/D converter collects 5 minutes long data files into data folder and all data except newest one will be removed in every hour by dataFlush.py script and crontab. The path of dataFlush.py is "/home/pi" and the path of the data folder is "/home/pi/dataLog". So how do I add dataLog path into this command:

os.system('ls -t | tail -n +2 | xargs rm --')

I tried something like this but it didn't work:

os.system('ls /home/pi/dataLog -t | tail -n +2 | xargs rm --')

ls(1) won't generate full-path outputs in your case, so you might want to prepend every line with the "prefix":

os.system('ls /home/pi/dataLog -t | tail -n +2 | sed 's|^|/home/pi/dataLog/|' | xargs rm --')

Another options is to use find(1) :

os.system('find /home/pi/dataLog | xargs ls -t -- | tail -n +2 | xargs rm --')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM