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 Jan 19, 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 Delete Files Older Than 30 Days with PowerShell

You can delete files older than 30 days with PowerShell, and there are other solutions to delete these files. We will introduce all the possible ways in this passage.

Dany

Jan 06, 2023

Quickly Delete Massive File Structure on Windows Server 2012

Normally, the deletion of large files takes a long time. Your computer needs to confirm and analyze the data, which will take time. The methods below may help you quickly delete massive files structure on Windows Server 2012.

Dany

Jan 10, 2023

Need a Deleted File Recovery Tool for Windows XP

While uninstalling a program on your computer or laptop, it is easy to lose files. You will need a data recovery program if you cannot find your lost files in Recycle Bin. We will recommend EaseUS Data Recovery Wizard to help you recover the deleted files on Windows XP.

Dany

Dec 29, 2022

Accidentally Deleted Large MKV File, How to Recover

MKV videos that have been lost or destroyed can be recovered using the third-party best video recovery software. We will introduce the best solution to assist you in recovering deleted MKV videos using powerful Video recovery software - EaseUS Data Recovery Wizard in the following article.

Dany

Jan 13, 2023

How to Delete Files Older Than X Days in Folders and Subfolders

Are you tired of useless files on your computer yet eating up space? Then use this simple approach to delete files older than x days using File Explorer, CMD, and PowerShell. We can also assist you in recovering accidentally lost files on Windows 11/10.

Dany

Jan 09, 2023

How to Delete Files Older Than 90 Days and Save A Copy

You may free up space and keep things organized by only deleting files in any folder that is older than a specific number of days. If you have many files in separate directories and want to clear them up by eliminating those older than a particular number of days, you can use the ForFiles command.

Dany

Jan 11, 2023