Delete files older than 1week(dates need to be calculate based on file name)
Objective: We have multiple files in a folder and we want to delete all files except for last 1 week files.
Note: We are copying these files from original location to this temporary location. So time shown for these files are time when we copied to this location. Not that when file was created.
Files are:
The date of the file can be found in file name itself.
tps-20170525171625377-45475.tps = 2017-05-25
Query: In above example we have files from 16-05-2017 to 25-05-2017. How can keep files only for last seven days i.e. 19th to 25th may and remove rest of them.
For a zero'th approximation, try
Please not that this is just a quick and dirty idea on how to proceed; it assumes the file sorted last is the newest one and the one to start calculating backwards from, it is not month end nor year end safe, and it requires shell arithmetic as provided by e.g. bash (recent).
Would it be a problem to extract the file creation time from the file name and use touch to set the time correctly? You can then use find far easier.
Is the date format year, month, day, hour, minute, second, millisecond? You can grab that bit and make a decision on that if you calculate the date before you want to erase the files, something like this:-
It doesn't work with milliseconds though and relies on GNU-date. Are these a problem?
Does this get you close?
Robin
Last edited by rbatte1; 06-23-2017 at 12:02 PM..
Reason: Added a code comment
I need to list and delete all files in current older which are olderthan 7 days. But my file names have white spaces. Before deleting I want to list all the files, so that I can verify.find . -type f -mtime +7 | xargs ls -l {}
But the ls command is the working on the files which have white... (16 Replies)
Hi,
I need a command for deleting all the compress files *.Z that are older than the current date - 5 days. Basically I have a directory where daily I meet some back up files and I want to remove automatically the ones 5 days (or more) older than the current date. How can I write a 'rm' command... (1 Reply)
I have to retain only 1 day files in my system an I have to delete all the other files which are older than 24 hrs. Please let me know the option I have to give in the find -mtime command. (3 Replies)
Guys,
My log files stored in the date format format below(log_20080714072942):
TIMESTAMP=`date +%Y%m%d%H%M%S`
LOG=/log/log_${TIMESTAMP}.log
I'm looking for a shell script which deletes all files which is older than 3 months from today.
Regards,
Bhagat (3 Replies)
Hi all,
i have a simple question that i want to find out the 30 minutes older files and delete those files from the particular location(Folder)
Generally for this purpose used to retreive the files with "atime" command
For example: find and delete the 2 days older log files use this below... (2 Replies)
I will like to write a script that delete all files that are older than 7 days in a directory and it's subdirectories. Can any one help me out witht the magic command or script?
Thanks in advance,
Odogboly98:confused: (3 Replies)