24

Possible Duplicate:
Delete all but the most recent X files in bash

I have a script to create incremental backups daily and I need to delete all backups but last 5.

For example, I have this folders:

drwxr-xr-x  4 root root 4096 Oct 29 01:10 2010-10-29
drwxr-xr-x  4 root root 4096 Oct 30 01:10 2010-10-30
drwxr-xr-x  4 root root 4096 Oct 31 01:10 2010-10-31
drwxr-xr-x  4 root root 4096 Nov  1 01:10 2010-11-01
drwxr-xr-x  4 root root 4096 Nov  2 01:10 2010-11-02
drwxr-xr-x  4 root root 4096 Nov  3 01:10 2010-11-03
drwxr-xr-x  4 root root 4096 Nov  4 01:10 2010-11-04
drwxr-xr-x  4 root root 4096 Nov  5 01:10 2010-11-05
drwxr-xr-x  4 root root 4096 Nov  6 01:10 2010-11-06
drwxr-xr-x  4 root root 4096 Nov  7 01:10 2010-11-07
drwxr-xr-x  4 root root 4096 Nov  8 01:10 2010-11-08

And I need to maintain only the last 5 directories and delete the others. After command execute, I need to have only this:

drwxr-xr-x  4 root root 4096 Nov  4 01:10 2010-11-04
drwxr-xr-x  4 root root 4096 Nov  5 01:10 2010-11-05
drwxr-xr-x  4 root root 4096 Nov  6 01:10 2010-11-06
drwxr-xr-x  4 root root 4096 Nov  7 01:10 2010-11-07
drwxr-xr-x  4 root root 4096 Nov  8 01:10 2010-11-08

I don't need to delete previous to 5 days, I need to delete all except 5 last directories :)

Now I'm using:

find /backup/increment -maxdepth 1 -type d -mtime +5 -exec rm -rf {} \;

But I need to improved not based in time :)

EDIT: This is an example for a server that do backups all days, but I need an script that delete all folders previous to last 5 because my computer do backups at 00:10 at night, but not all nights the backup is done it, because my computer isn't working all days, and I need to have always the last 5 backups :)

Community
  • 1
  • 1
Lito
  • 1,262
  • 2
  • 17
  • 25

5 Answers5

42

use the tail command to print lines starting with the n th line (Option -n +N):

rm `ls -t | tail -n +6`

ls -t outputs the current directory sorted by time. tail -n +6 takes al lines starting with the 6th line. Quoting with backticks feeds the result of the pipe into the rm command.

OLD SOLUTION, not correct ...

use the head command, which prints the first n lines of some output:

rm `ls -t1 | head -n 5`

ls -t outputs the current directory sorted by time. head -n 5 takes the first five entries of the previous output. Quoting with backticks feeds the result of the pipe into the rm command.

Please try out first before applying to live data :) ...

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
  • 1
    You'd want to use tail instead, for the *last* n lines. But yeah, that solution is way simpler and better than mine. :) – Max E. Nov 08 '10 at 19:06
  • 1
    What about: `rm $(ls | sort -r | sed 1,5d)`; list the names in date order; sort them into reverse order (most recent first); delete the first five lines; remove whatever is left. – Jonathan Leffler Nov 08 '10 at 19:27
  • @Max: Thanks for pointing out my mistake. Fortunately, there is the +N option to `tail` (which I wasn't aware of), so we can get all lines *but the first n*. – MartinStettner Nov 09 '10 at 03:06
  • @Jonathan: You have to use `ls -t` in order to get results sorted by time (or you have to relay on the files having correct names). You could also use `ls -t -r` to reverse the sorting, so `sort` is not needed. – MartinStettner Nov 09 '10 at 03:08
  • @MartinStettner: given the naming convention used, there is no need for '-t'; indeed, it might even give the wrong results. Suppose the directory for 2010-10-31 had been modified two days ago; then '`ls -t`' would give the wrong directory sequence. – Jonathan Leffler Nov 09 '10 at 03:15
  • @Jonathan: Partially agreed. But given the sample code (using `find`) in the question, Lito wanted the last five directories *based on time* to be retained. I hope that, reading these comments, he'll know which version to use :) ... – MartinStettner Nov 09 '10 at 11:22
  • 1
    In case the command return empty result, we can prefer this syntax to avoid command error `ls -t | tail -n +6 | xargs --no-run-if-empty rm` – Cédric Sep 18 '17 at 20:03
1

The first thing that came to my mind. It's not elegant:

a=0;
for i in `ls -t`;
do
    a=`expr $a + 1`;
    if [ $a  -gt 5 ]; then
          echo "removing $i";
          rm -rf $i
    fi;
done
emrea
  • 1,335
  • 9
  • 18
0

create two dummy files with the start and the end date

touch -t 1010290000 before
touch -t 2011042359 after

find all the files between the 2 dummy files and "rm -rf" the result

find . -newer before \! -newer after -exec rm -rf {} \;
zengr
  • 38,346
  • 37
  • 130
  • 192
0

ls -tr | perl -ne '{@files = <>; print @files[0..$#files-5 ]}' | xargs -n1 echo rm -rf

You would remove the echo before the rm -rf to get it to work.

frankc
  • 11,290
  • 4
  • 32
  • 49
0

The trick will be the -t option to ls, which sorts by modification time, from newest to oldest.

A really naive solution, using a temporary file, might go this way:

ls -t > /tmp/file_list
num_files_to_keep=5
# wc -l gets the line count of a file
# first word of wc output is the actual line count, and that's all we need, so
# delete everything after the space.
num_files=`wc -l /tmp/file_list | sed "s/ .*//"`
#if appropriate you should add a check for num_files < num_files_to_keep
num_files_to_delete=$(( $num_files - $num_files_to_keep ))
rm `tail -n $num_files_to_delete /tmp/file_list`
Max E.
  • 1,837
  • 13
  • 15