15

So, I have a folder with alot of files, they are generated by second and need to be kept in I only can delete them after 90 days, so as you may guess, I would generate tons of files and then after the 90 days I'm able to delete those files that is older than 90 days. But, I'm stuck in the part where I search for those files, since I have alot, the system complains that the list is too large and thus I can't remove them.

What is the best solution so I can pass this? The file names are in timestamp mode, so I could start by it, but I want to make sure all files are deleted after some time....

I have tried these methods

rm -rf *
find /path/to/files/ -type f -name '*.ts' -mtime +90 -exec rm {} \; 

I have also managed to create a script to delete by filename, but with this method I have no guarantee that all the files are deleted.

8
  • What methods have you tried that the system complains about?
    – jesse_b
    Commented Jan 3, 2018 at 12:24
  • Since I'm still developing this, yesterday I generated about 5 days of files and want to erase them, I just did a rm -rf * and the system yelled at me. Commented Jan 3, 2018 at 12:25
  • 1
    The problem is not only deleting them....think ahead, and create a directory structure YEAR/MONTH/DATE/HOUR automatically, it will be easier to manage. Commented Jan 3, 2018 at 12:25
  • 2
    Please don't ever do rm -rf *. You should always be as specific as possible when removing files with globbing. rm -- ./* would be better for you, no need for -r as you want to remove files not directories, and you should only use -f if you have to.
    – jesse_b
    Commented Jan 3, 2018 at 12:30
  • 1
    When you say several methods weren't effective, please provide more detail. Edit your question to include exactly the command you issued and any error messages or other relevant output produced. Commented Jan 3, 2018 at 12:33

3 Answers 3

25

If the files are not modified after initial creation, you could delete if they have not been modified in over 90 days:

find /path/to/folder ! -type d -mtime +90 -delete

or

find /path/to/folder ! -type d -mtime +90 -exec rm -f {} +

(for versions of find which do not support the -delete action).

As a matter of safety, you should use a non-destructive version of this command first and ensure it will delete exactly what you want deleted, especially if you intend to automate this action via cron or similar, e.g.:

find /path/to/folder ! -type d -mtime +90 -print

Note that -mtime +90 checks the modification time of the files and returns true if the age, rounded down to an integer number of days (86400 second units) is strictly greater than 90, so matches on files that have been last-modified 91 days ago or earlier.

3
  • 1
    +1 for non-destructive
    – jim smith
    Commented Feb 20, 2022 at 8:37
  • If you'd like to see the files that match before you actually delete them, you can switch the -delete flag to be -print to view the files that will be removed -- like a dry run. Commented Aug 3, 2022 at 16:44
  • Is this recursive (subfolders included)?
    – Antonello
    Commented Feb 5 at 18:49
1

Disclaimer: I'm the current author or rawhide (rh) (see https://github.com/raforg/rawhide).

With rawhide (rh) you can do it like this:

rh -UUU /path/to/files 'f && "*.ts" && old(90*days)'

-UUU unlinks/deletes/removes matching files.

/path/to/files is a search path.

The rest is the search criteria.

f matches regular files.

"*.ts" matches files whose names end with .ts.

old(90*days) matches files last modified at least 90 days ago.

0

Based on the previous answer, if you need to delete directories, you can use the option -maxdepth.

Preview the things that you are going to delete:

find . -maxdepth 1 -mtime +1 -print

Remove them all:

find . -maxdepth 1 -mtime +1 -exec rm -rf {} +

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .