Use Shell Script to Delete the Files from Directory Based on Date Parameter

I have a circumstance where I want to remove all files older than 2000 from a directory. The names of the files are file1, name YYYY MM DD, file2 name YYYY MM DD, etc. Just as we have a lot of files in the directory, is there any way to delete files older than 2000?

Best Answered by

Dany· Answered on Feb 22, 2023

You should be extremely cautious when deleting files older than 2000. Finding a date depends on filenames that aren't actually a date is dangerous. It might be good to start with a find of all the files in the directory, assuming that everything is contained within it.

The favored method and the safest for removing files older than 2000 is still the find command. Making a file with the time-stamp within is one approach to accomplish this, like this touch -t 200001010000 /tmp/timestamp. Delete the files matching the timestamp using GNU Find (assuming they are in the current directory) now, for example: find . -type f ! -newer /tmp/timestamp -delete, or non GNU find : find . -type f ! -newer /tmp/timestamp -exec rm {} \;. Be aware that it will also destroy files generated at the precise moment of 2000-01-01 00:00:00.0000000000, which might be problematic for files whose timestamp was set arbitrarily.

Using a script to delete files is not always going well, the files earlier than 2000 may get lost by accident. You should not worry too much about the lost files, and we can still get them back by using a third-party file recovery tool – EaseUS Data Recovery Wizard.

1. In Windows 11/10/8/7, open the EaseUS Data Recovery Wizard and choose the location of the lost data. Then select "Scan" to start to recover deleted files on Windows.

2. Utilize the file format filter on the left or upper right corner to locate the files you want after the scan. Then, you may double-click a file to obtain a preview of its contents or select the "Preview" button.

3. To recover the lost data to a secure place, check the box next to the file and then click "Recover".

Use EaseUS Data Recovery Wizard to recover deleted or lost files easily and quickly.

Tutorials on how to recover lost, deleted, or formatted data from HDD, SSD, USB, SD card, or other storage media on Windows and Mac.

People Also Ask

How to Recover Shredded Files in Windows | Deleted by AVG, Avast and Norton

You can recover shredded files in both Windows and Mac quickly. Files shredded by Windows or third-party antivirus programs like AVG, Mcafee, Avast, and Norton can be effectively retrieved by EaseUS Data Recovery Wizard.


Mar 10, 2023

Deleted My Partitions by Mistake While Installing Windows 7

Before installing Windows 7 on your computer, you should back up your data. If you don't have a backup, you can recover data from the HDD which lost partitions with EaseUS Data Recovery Wizard.


Mar 09, 2023

How to Delete Files from Archive by Text File

Renaming a file within a zip bundle is similar. Even in these circumstances, you can extract everything before zipping it back into a new file or overwriting the old one. So, keep reading this post to acquire the 2023 guide.


Feb 22, 2023

Recover Files Deleted by Avast Antivirus on Windows

Most users will install antivirus software on their computer, but sometimes this kind of virus eraser will also delete innocent files on the PC. How to recover files deleted by Avast Antivirus? Reading this passage to learn more.


Mar 09, 2023

How to Recover Bitdefender Deleted Files Without Asking

Bitdefender deleted files without asking? Want to restore files deleted by Bitdefender? Read this post to learn why this issue happens, how to use the EaseUS Data Recovery Wizard to recover deleted files, and how to stop important files from being deleted by Bitdefender.


Mar 10, 2023

Can I Delete the Default Files on My External HDD Newly Bought

What should you do when you get a new external HDD? Is it necessary to delete the default files on your HDD? You will find the answer in this passage.


Mar 09, 2023