39

I want to delete file and folder older than 7 days so I tried

[17:07:14 root@client01.abc.com:~]# find /tmp/ -mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} \;

So when I run find /tmp/ -mindepth 1 -maxdepth 1 -ctime +7 -exec ls -l {} \; it doesnt show any dir, but for find /tmp/ -mindepth 1 -maxdepth 2 -ctime +7 -exec ls -l {} \; it does show few files in subdir.

Whats is the right way to delete files/folders older than 7 days in one specific dir ?

jww
  • 97,681
  • 90
  • 411
  • 885
roy
  • 6,344
  • 24
  • 92
  • 174
  • Possible duplicate of [Shell script to delete directories older than n days](https://stackoverflow.com/q/13868821/608639) – jww Jul 20 '18 at 13:41
  • Do you want to delete directories *and* files with a single find command, or are you ok with two separate commands? – chrisinmtown Feb 10 '22 at 13:11

4 Answers4

83

You can make use of this piece of code

find /tmp/* -mtime +7 -exec rm {} \;

Explanation

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.

  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +7, it will find files older than 7 days.

  • The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

Source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

For deleting folders, after emptying inside of them you can rmdirinstad of rm in the piece of code, also if you only want to see directories you can add

-type d

to piece of code such as below:

find /tmp/*/* -mtime +7 -type d -exec rmdir {} \;
Otiel
  • 18,404
  • 16
  • 78
  • 126
Tolga Varol
  • 1,036
  • 1
  • 16
  • 21
  • is it going to delete dir inside /tmp/ older than +7 days too ? – roy Jul 13 '15 at 18:39
  • I'm editing for removing directory info too, check the bottom @roy – Tolga Varol Jul 13 '15 at 19:13
  • 9
    I'm going to say it. Find is the most terrifying & dangerous command I ever run. A misplaced order of operations, a lack of quotes in the execute string, a simple logic error? And you can end up deleting or modifying a whole host of items you didn't expect. And it doesn't help if you run it without an -exec first, because the unquoted items aren't being passed to a command. I highly recommend running it through '-exec ls {} \;' first to make sure that you aren't getting strange separations due to white space or other characters causing your * to target a whole slew of things you didn't expect. – EricDAltman May 09 '18 at 18:24
  • 1
    I I think this is the best way to achieve what was asked from Roy. This because in first you are thinking to deleted files, then you safely can remove empty directories. For this purpose you can simply use this command: find /tmp/ -type d -empty -delete – Paolovip Dec 08 '19 at 11:10
  • Probably do a dry run first by leaving off the "-exec rmdir () \;" - just to be safe – Sp4cecat Apr 18 '22 at 08:14
17

Easier to just do

find /tmp/* -mtime +7 -exec rm -rf {} \; 

Which will del files and dirs

maazza
  • 7,016
  • 15
  • 63
  • 96
user6406452
  • 179
  • 1
  • 4
7

my easy way:

find /tmp/* -daystart -mtime +7 -delete

the daystart option measure times from the beginning of today rather than from 24 hours ago

ref: official_doc

alfiogang
  • 435
  • 5
  • 7
4
find /tmp/* -mtime +7 -type f -exec rm {} \;

Remove files.

find /tmp/ -empty -type d -delete

Remove empty directories.

sj59
  • 2,072
  • 3
  • 22
  • 23