Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have dump directory that is filled at the rate of one file per 4 hours.

I wish to keep all files for a week then delete all except the newest of each day.

Bash is preferred though other solution are also welcomed.

share|improve this question
I would do this by making sure that the files have a consistent naming convention and then use find's ! operator to delete all bar the files I wanted to keep. –  Iain Aug 1 at 12:34
@Iain, Will do that unless its possible to achieve simply by relaying on the creation time. –  michaelbn Aug 1 at 12:46
At the end I used name conventions as suggested by @lain. I would prefer something more 'less responsible' but for the sake of simplicity that will be enough. –  michaelbn Aug 4 at 7:59

2 Answers 2

The trick is the --full-time flag, which can be given to an ls command, which gives back a file list with very easily scriptable date fields. We can easily sort it by the date.

$ ls -l --full-time|sort -k +7
-rwxr-xr-x  1 cica cica  16536 2014-07-10 10:47:32.448349200 +0200 epl-v10.html
-rwxr-xr-x  1 cica cica   9013 2014-07-10 10:47:32.495149500 +0200 notice.html
drwxr-xr-x+ 1 cica cica      0 2014-07-07 14:12:11.502336700 +0200 readme
drwxr-xr-x+ 1 cica cica      0 2014-07-31 14:38:20.800181400 +0200 p2
drwxr-xr-x+ 1 cica cica      0 2014-07-31 15:15:06.506730000 +0200 features
drwxr-xr-x+ 1 cica cica      0 2014-07-31 15:15:06.680747400 +0200 plugins
-rw-r--r--  1 cica cica 368634 2014-07-31 15:15:06.826762000 +0200 artifacts.xml
-rw-r--r--  1 cica cica    329 2014-07-31 15:15:12.816360900 +0200 eclipse.ini
-rwxr-xr-x  1 cica cica   7929 2014-07-14 16:01:58.698363500 +0200 system_catalog.xml
drwxr-xr-x+ 1 cica cica      0 2014-07-31 17:41:59.205940000 +0200 configuration
-rwxr-xr-x  1 cica cica 312320 2014-06-01 20:12:16.000000000 +0200 eclipse.exe
-rwxr-xr-x  1 cica cica  17920 2014-06-01 20:12:16.000000000 +0200 eclipsec.exe

First, we sort it by the full date!

The trick is, that after the each days last file, the date field will replay after the previous line. This can be handled by awk very easily.

Second, we get it further to a simple awk script: awk '{if ($6 == EX) print $9; EX=$6}'

Finally, we are using xargs to let delete every file with an rm command.

The full command is:

ls -l --full-time|sort -k +6|awk '{if ($6 == EX) print $9; EX=$6}'|xargs -P 1 -n 1 echo rm -vf

This command is what you need to call periodically, ideally from a cron. Ideally, you can give into a crontab -e, you call this every day at 2:37 :

37 2 * * *     ls -l --full-time|sort -k +6|awk '{if ($6 == EX) print $9; EX=$6}'|xargs -P 1 -n 1 echo rm -vf

Of course you could put this in a script and call only the script from cron.

share|improve this answer
But this doesn't do what the requestor asked! –  MadHatter Aug 1 at 12:32
@MadHatter Yes, it is true. And now? –  Peter Horvath Aug 1 at 12:35
Umm, still not. After a week he wants to delete all except for each day's youngest (newest) file. –  MadHatter Aug 1 at 12:35
As an aside linux find has a -delete option which is quite handy –  Iain Aug 1 at 12:38
@MadHatter You have right! I significantly reworked/extended my answer, maybe it deserves a change to up? :-) –  Peter Horvath Aug 1 at 12:53

you can also use find command. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

find <path of file> -typf f -mtime +0 -exec rm {} \;

it will delete files which are dated yesterday.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.