I'm trying to work out a command which deletes sql files older than 15 days.
The find part is working but not the rm.
rm -f | find -L /usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups -type f \( -name '*.sql' \) -mtime +15
It kicks out a list of exactly the files I want deleted but is not deleting them. The paths are correct.
usage: rm [-f | -i] [-dIPRrvW] file ...
unlink file
/usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/20120601.backup.sql
...
/usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/20120610.backup.sql
What am I doing wrong?
Another simpler method is to use
locate
command. Then, pipe the result toxargs
.For example,
Assuming you aren't in the directory containing the *.sql backup files:
The -v option above is handy it will verbosely output which files are being deleted as they are removed.
I like to list the files that will be deleted first to be sure. E.g:
Will select files in
/usr/www/bar/htdocs
older than 15 days and remove them.You are actually piping
rm
's output to the input offind
. What you want is to use the output offind
as arguments torm
:xargs
is the command that "converts" its standard input into arguments of another program, or, as they more accurately put it on theman
page,Note that if file names can contain whitespace characters, you should correct for that:
But actually,
find
has a shortcut for this: the-delete
option:Please be aware of the following warnings in
man find
:P.S. Note that piping directly to
rm
isn't an option, becauserm
doesn't expect filenames on standard input. What you are currently doing is piping them backwards.