16

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? 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. 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

28

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 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)? Commented Feb 5, 2024 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.

3
  • Propose it to Debian or provide packages yourself. Commented Nov 21, 2024 at 16:35
  • @WGRM Thanks for the suggestion. I've just submitted a RFP (request for package) bugreport for wnpp. But there are thousands of others there. They might never see it. I should probably ask some debian developers directly. A macports package is available, and it's on Arch's AUR, and there's a new spec file for creating rpm packages. Commented Nov 23, 2024 at 1:52
  • I know, i found it via Arch. ๐Ÿ˜ In Debian things may take a while, my comment here is: it's small, easy and good. Take on the request. I compiled it and it will stay. Btw, expect your first bug report, .acl isn't working somehow. ๐Ÿ˜ Commented Nov 24, 2024 at 9:28
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.