Archive and remove files older than a specified date

This small script realizes the routine task of finding and archiving files older than a given date from a directory structure.

#!/bin/bash
 
if [ ! -d "$2" ]; then
    echo "usage: $0 <timestamp> <directory>"
    exit 0
fi
 
ts="$1"
dir="$2"
 
tmpdir=`mktemp -d`
trap "rm -rf '$tmpdir'" exit
 
touch -d "$ts" "$tmpdir/tsfile" || exit
 
echo "+ finding files older than $ts in $dir"
find "$dir" -type f -not -cnewer "$tmpdir/tsfile" > "$tmpdir/filelist"
 
archive="archive-$ts-"`date +%s`".tgz"
 
echo "+ packing contents to $archive"
tar -czf "$archive" --files-from "$tmpdir/filelist" && { echo "+ removing files" ; cat "$tmpdir/filelist" | xargs -n1 rm -f ;}
 
echo "+ done"

Linkbacks

Use the following URL for manually sending trackbacks: http://rigo.info/lib/plugins/linkback/exe/trackback.php/en:blog:archive_and_remove_files_older_than_a_specified_date
en/blog/archive_and_remove_files_older_than_a_specified_date.txt · Utolsó módosítás: 2009-07-17 00:00 (külső szerkesztés)
CC Attribution-Noncommercial-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0