Delete files that are old than x days
I have a server to store the built apks, it will generate an apk when there is a new commit, so there are a lot of apks on the server, and…
I have a server to store the built apks, it will generate an apk when there is a new commit, so there are a lot of apks on the server, and I want to keep the files for one week. This is the script that I used to delete the files that are created more than one week.# The command following can find the files that are modified one week ago
find . -type f -mtime +7
To delete the files that are match, just added -delete
option on the find command like below:find . -type f -mtime +7 -delete
The -mtime
option
We use the -mtime
option on the find command, there are some other options we can use.
-atime n
Files that were last accessedn*24
hours ago.-ctime n
Files that were last changedn*24
hours ago.-mtime n
Files that were last modifiedn*24
hours ago.
The -type option
I also use the -type
option to filter only the regular files, because I want to delete the apk files, so I can use -type f
to filter them.
We can also use -type d
to filter the directories.
The -delete option
I also use -delete
option to delete the filtered files, we can also use -exec
to delete the files, the -exec
command is like below:find . -type f -mtime +7 -exec rm -rv {} \;