Command line: piping find results to rm
You are actually piping rm
's output to the input of find
. What you want is to use the output of find
as arguments to rm
:
find -type f -name '*.sql' -mtime +15 | xargs rm
xargs
is the command that "converts" its standard input into arguments of another program, or, as they more accurately put it on the man
page,
build and execute command lines from standard input
Note that if file names can contain whitespace characters, you should correct for that:
find -type f -name '*.sql' -mtime +15 -print0 | xargs -0 rm
But actually, find
has a shortcut for this: the -delete
option:
find -type f -name '*.sql' -mtime +15 -delete
Please be aware of the following warnings in man find
:
Warnings: Don't forget that the find command line is evaluated
as an expression, so putting -delete first will make find try to
delete everything below the starting points you specified. When
testing a find command line that you later intend to use with
-delete, you should explicitly specify -depth in order to avoid
later surprises. Because -delete implies -depth, you cannot
usefully use -prune and -delete together.
P.S. Note that piping directly to rm
isn't an option, because rm
doesn't expect filenames on standard input. What you are currently doing is piping them backwards.
find /usr/www/bar/htdocs -mtime +15 -exec rm {} \;
Will select files in /usr/www/bar/htdocs
older than 15 days and remove them.