Auto delete files older than 7 days

I am a complete noob at linux but I am starting to get the hang of it. I have an Ubuntu Server 16.04 running an FTP server to backup security video files. The files will be stored in folders like: /home/securityfolder1, /home/securityfolder2, /home/securityfolder3 and so on.

Note that each securityfolderN is a different user.

Because I don't want my hard drives to be full all of the time, I want to delete files older than 7 days in these folders daily.

2

2 Answers

First, this command will find and delete all files older than 7 days in any subdirectory in /home whose name starts with securityuser:

find /home/securityuser* -mtime +6 -type f -delete

You need -mtime +6 and not +7 because -mtime counts 24h periods. As explained in the -atime section of man find (-mtime works in the same way):

 -atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

So, to find a file that was modified 7 or more days ago, you need to find files that were modified more than 6 days ago, hence -mtime +6.

The next step is to have this command run once a day. Since each securityuserN is a different user (you might want to rethink that setup, it makes everything more complicated), this must be run as root. So, edit /etc/crontab:

sudo nano /etc/crontab

And add this line:

@daily root find /home/securityuser* -mtime +6 -type f -delete

That will run the find command once a day and delete the files.

9

as per i my knowledge:

try find command like this:

find ./dirc/* -mtime +6 -type f -delete
./dirc/* : is your directory (Path)
-mtime +6 : modified more than 6 days ago (therefore, at least 7 days ago)
-type f : only files
-delete : no surprise. Remove it to test before like rm
11

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like