24

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.

terdon
  • 98,183
  • 15
  • 197
  • 293
Jacco
  • 341
  • 1
  • 2
  • 5
  • `/home` usually contains a subfolder for each user. If you don’t want to store your backups as different users (not really good idea), you should consider another location, e.g. `/home/security/backup1` and so on. – Melebius Jun 21 '16 at 06:26
  • @Melebius thank you for your comment, every /securityfolder is a different user, i forgot to mention that. – Jacco Jun 21 '16 at 06:35

2 Answers2

39

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.

terdon
  • 98,183
  • 15
  • 197
  • 293
  • +1 Driving the nail a little deeper and maybe reiterating Melebius' suggestion to OP, that `find '/home/securityuser/*' -mtime +6 -type f -delete` (with all pertinent and appropriate changes in user creation) might generally be a better idea than `find '/home/securityuser*' -mtime +6 -type f -delete` (no slash in path)... ? – Cbhihe Jun 22 '16 at 06:46
  • @Cbhihe no, the target directories are called `/home/securityuserN`, so without the slash, they won't be found. – terdon Jun 22 '16 at 07:13
  • yup, saw that; just didn't think it good practice on OP's part (creating a separate user for each security video, etc...) along the line of Melebius' first comment, even though OP may have his reasons for doing so. If anything my comment was lame, not a criticism of yr good and complete answer. – Cbhihe Jun 22 '16 at 07:20
  • @Cbhihe no offense taken! And yes, using separate users for this is not a good idea. – terdon Jun 22 '16 at 07:24
  • 1
    +1 From me. Just note one detail as I was running this on RHEL: the wildcard didn't work on the path. I had to put it on the `-name`: `find /home/ -name 'securityuser*' -mtime +6 -type f -delete` – Stelios Adamantidis Jan 03 '19 at 10:04
  • 1
    @SteliosAdamantidis oh wow, I completely missed that! Yes, the original version wouldn't work since I had quoted `'securityuser*'`. The wildcard should be expanded by the shell, not by `find`, so it should have been `securityuser*` (no quotes). See updated answer. Thanks for pointing it out, Stelio, I can't believe nobody noticed before! Ti vlakas! – terdon Jan 03 '19 at 10:13
  • LOL you are anything but that! Keep up the good work. – Stelios Adamantidis Jan 03 '19 at 10:48
  • Can we add some exceptions to some files that we don't want to delete? – Newbie Jan 10 '20 at 03:51
  • @Newbie yes, but that's a different question. Look at the `-not` option of `find`. For example `find /home/securityuser* -mtime +6 -type f -not -name wantedFile -delete`. – terdon Jan 10 '20 at 09:34
5

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
terdon
  • 98,183
  • 15
  • 197
  • 293
Maulik Patel
  • 151
  • 2
  • thank you for your answer. Does ./dirc/* means: ./home/securityfolder1/* or is this wrong? – Jacco Jun 21 '16 at 06:38
  • I just tried it out in my virtualbox as `find /home/jacco/ -mtime +1 -type f -delete` and it seems to work. How can i automate this? – Jacco Jun 21 '16 at 06:42
  • make one script file and fire this command using script if it work than run your script file on startup process. – Maulik Patel Jun 21 '16 at 06:49
  • i am like the supernoob right here cuz the part of making the script, does this mean a file with `#!/bin/bash` and the code under that? or am i really stupid right here? – Jacco Jun 21 '16 at 06:51
  • @JaccovandeWijgaart After the command has been tested, you can just put it into that user’s [crontab](https://en.wikipedia.org/wiki/Cron) without making a script. And unlike km8295, I’d prefer absolute paths, especially when deleting. – Melebius Jun 21 '16 at 07:06
  • @Melebius so putting `find /home/securityfolder1/ -mtime +7 -type f -delete` `find /home/securityfolder2/ -mtime +7 -type f -delete` `find /home/securityfolder3/ -mtime +7 -type f -delete` in the crontab is good? or does it have to be: `@daily find /home/securityfolder1/ -mtime +7 -type f -delete`? – Jacco Jun 21 '16 at 07:13
  • @JaccovandeWijgaart You’ll definitely need `@daily` or something similar and the `*` at end of path. Add each command to the corresponding user’s `crontab`, or do it all as `root` (at your own risk). – Melebius Jun 21 '16 at 07:18
  • @km8295 The path in `find` command should be closed in quotes (preferably `''`), especially when containing [glob patterns](https://en.wikipedia.org/wiki/Glob_(programming)) like `*`. – Melebius Jun 21 '16 at 07:33
  • 2
    @Melebius no, tha path should not be quoted, especially when containing glob characters. You *want* it to be expanded by the shell and the quoting would block that. Try, for example: `find '/u*' -name local`. It's directives like `-name "foo*"` that should be quoted when containing glob characters. – terdon Jun 21 '16 at 08:06
  • @terdon You are right, my fault. I use mostly `.` as path for `find` and focus on `-name`. – Melebius Jun 21 '16 at 08:08
  • @km8295 this will find files that are 6 days or older, not 7 days or older. – terdon Jun 21 '16 at 08:11