rp346
rp346

Reputation: 7028

find and delete file or folder older than x days

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

[17:07:14 [email protected]:~]# 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 ?

Upvotes: 52

Views: 192578

Answers (6)

Joshua Johns
Joshua Johns

Reputation: 373

Adding alternatives here just for future references

-mtime +240 means 240 days (approximately 8 months)

  1. deleting file older than 8 months

rm $(find folder/* -type f -mtime +240)

  1. Deleting folders older than 8 months

    rm $(find folder/* -type d -mtime +240)

Upvotes: 0

thoredge
thoredge

Reputation: 12591

I recommend this oneliner to delete all old files within 8 days:

find /tmp/ -mtime +8 -type f -exec rm {} \; -o \
  -mtime +8 -type d -empty -exec rmdir {} \;

The -o splits the files and directories so that we don't delete the directories and then get an error because the files doesn't exist anymore. It will also delete old files in a directory with newer files.

This may result in empty directories staying 8 more days since deletion of a files updates the directory.

Upvotes: 0

Tolga Varol
Tolga Varol

Reputation: 1236

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 {} \;

Upvotes: 103

sj59
sj59

Reputation: 2162

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

Remove files.

find /tmp/ -empty -type d -delete

Remove empty directories.

Upvotes: 6

alfiogang
alfiogang

Reputation: 494

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

Upvotes: 13

user6406452
user6406452

Reputation: 199

Easier to just do

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

Which will del files and dirs

Upvotes: 19

Related Questions