简体   繁体   中英

shell script to delete all files except the last updated file in different folders

My application logs will be created in below folders in linux system.

Folder 1: 100001_1001
folder 2 : 200001_1002
folder 3 :300061_1003
folder 4: 300001_1004
folder 5 :400011_1008

want to delete all files except the latest file in above folders and want to add this to cron job.

i tried below one not working need help

30 1 * * * ls -lt /abc/cde/etc/100* | awk '{if(NR!=1) print $9}' | xargs -i rm -rf {} \;
30 1 * * * ls -lt /abc/cde/etc/200* | awk '{if(NR!=1) print $9}' | xargs -i rm -rf {} \;
30 1 * * * ls -lt /abc/cde/etc/300* | awk '{if(NR!=1) print $9}' | xargs -i rm -rf {} \;
30 1 * * * ls -lt /abc/cde/etc/400* | awk '{if(NR!=1) print $9}' | xargs -i rm -rf {} \;

You can use this pipeline consisting all gnu utilities (so that we can also handle file paths with special characters, whitespaces and glob characters)

find /parent/log/dir -type f -name '*.zip' -printf '%T@\t%p\0' |
sort -zk1,1rn |
cut -zf2 |
tail -z -n +2 |
xargs -0 rm -f

Using a slightly modified approach to your own:

find /abc/cde/etc/100* -printf "%A+\t%p\n" | sort -k1,1r| awk 'NR!=1{print $2}' | xargs -i rm "{}" 

The find version doesn't suffer the lack of paths, so this MIGHT work (I don't know anything about the directory structure, and whether 100* points at a directory, a file or a group of files ...

You should use find , instead. It has a -delete action that deletes he files it found that match you specification. Warning : it is very easy to go wrong with -delete . Test your command first. Example, to find all files named *.zip under a/b/c (and only files):

find a/b/c -depth -name '*.zip' -type f -print

This is the test, it will print all files that the final command will delete (do not forget the -depth , it is important). And once you are sure, the command that does the deletion is:

find a/b/c -depth -name '*.zip' -type f -delete

find also has options to select files by last modification date, by size... You could, for instance, find all files that were modified at least 24 hours ago:

find a/b/c -depth -type f -mtime +0 -print

and, after careful check, delete them:

find a/b/c -depth -type f -mtime +0 -delete

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