Reputation: 1103
Any idea how to I remove my old file between by which month until which month, which is using linux command?
example what I want to do is:
rm oldfile.txt april until may 2012
Any idea how to solve my problem ?
thanks
Upvotes: 0
Views: 3723
Reputation: 54512
A three step process, using find
:
touch -d '2012-04-01 00:00:00' STARTING
touch -d '2012-04-30 23:59:59' STOPPING
find . -type f -newer STARTING -not -newer STOPPING -exec rm {} \;
Edit: Note that this will remove files between April and May. Therefore files between the 1st April 2012 and 30th April 2012 will be removed. Also, it's a good idea to view the list of files you'll be deleting before actually deleting them. To do this, change rm
to ls -la
.
Upvotes: 2
Reputation: 16185
You can start by displaying the files and also their last modification time in a date that you can easily recognize using stat
:
$ stat -c '%n %z' foo bar
2012-02-25 23:39:31.000000000 +0000 foo
2012-02-25 23:43:35.000000000 +0000 bar
Combine this with grep
you can filter the list to the months that you want:
$ stat -c '%n %z' foo bar | grep -E '^2012-0[45]-.. '
Which should give you files that were modified between April and May 2012. Then you'll want to extract only the file name (the fourth field onwards) for deletion using cut
:
$ stat -c '%n %z' foo bar | grep -E '^2012-0[45]-.. ' | cut -f 4- -d' '
Once you're satisfied with the output, then delete them like so (this is the point of no return, so put your seatbelt on :):
$ !! | while read filename; do rm -f "$filename"; done
Note: that !!
notation repeats the immediate last command so you don't have to type it all over again. In this case it will repeat the stat command with the final cut at the end.
Upvotes: 0
Reputation: 103
awk can help you delete files from a single month (or a set of months)
ls -l | awk '{if ($6=="Apr" || $6=="Mai") print $9}' | xargs rm
Upvotes: 0