I write below command to delete all files that are older than 7 days, but it doesn't work:
find /media/bkfolder/ -mtime +7 -name'*.gz' -exec rm {} \;
How can I remove these files?
command linedeletefindrm
I write below command to delete all files that are older than 7 days, but it doesn't work:
find /media/bkfolder/ -mtime +7 -name'*.gz' -exec rm {} \;
How can I remove these files?
Best Answer
As @Jos pointed out you missed a space between
name
and'*.gz'
; also for speeding up the command use-type f
option to running the command on files only.So the fixed command would be:
Explanation:
find
: the unix command for finding files/directories/links and etc./path/to/
: the directory to start your search in.-type f
: only find files.-name '*.gz'
: list files that ends with.gz
.-mtime +7
: only consider the ones with modification time older than 7 days.-execdir ... \;
: for each such result found, do the following command in...
.rm -- '{}'
: remove the file; the{}
part is where the find result gets substituted into from the previous part.--
means end of command parameters avoid prompting error for those files starting with hyphen.Alternatively, use:
From man find:
Which is a bit more efficient, because it amounts to:
as opposed to:
as in the
-exec
method.An alternative and also faster command is using exec's
+
terminator instead of\;
:This command will run
rm
only once at the end instead of each time a file is found and this command is almost as fast as using-delete
option as following in modernfind
: