Reputation: 55
How to Delete file created in a specific date??
ls -ltr | grep "Nov 22" | rm
-- why this is not wrking??
Upvotes: 5
Views: 24083
Reputation: 1
Mb better
#!/bin/bash
for i in $(ls -ltr | grep "NOV 23" | awk '{print $9}')
do
rm -rf $i
done
then in the previous comment
Upvotes: 0
Reputation: 1
for i in `ls -ltr | grep "NOV 23" | awk '{print $9}'`
do
rm -rf $i
done
Upvotes: 0
Reputation: 107739
There are three problems with your code:
rm
takes its arguments on its command line, but you're not passing any file name on the command line. You are passing data on the standard input, but rm
doesn't read that¹. There are ways around this.ls -ltr | grep "Nov 22"
doesn't just consist of file names, it consists of mangled file names plus a bunch of other information such as the time.Nov 22
, amongst others. It also won't catch the files you want in a locale that displays dates differently.The find
command lets you search files according to criteria such as their name matching a certain pattern or their date being in a certain range. For example, the following command will list the files in the current directory and its subdirectories that were modified today (going by GMT calendar date). Replace echo
by rm --
once you've checked you have the right files.
find . -type f -mtime -2 -exec echo {} +
With GNU find, such as found on Linux and Cygwin, there are a few options that might do a better job:
-maxdepth 1
(must be specified before other criteria) limits the search to the specified directory (i.e. it doesn't recurse).-mmin -43
matches files modified at most 42 minutes ago.-newermt "Nov 22"
matches files modified on or after November 22 (local time).Thus:
find . -maxdepth 1 -type f -newermt "Nov 22" \! -newermt "Nov 23" -exec echo {} +
or, further abbreviated:
find -maxdepth 1 -type f -newermt "Nov 22" \! -newermt "Nov 23" -delete
With zsh, the m
glob qualifier limits a pattern to files modified within a certain relative date range. For example, *(m1)
expands to the files modified within the last 24 hours; *(m-3)
expands to the files modified within the last 48 hours (first the number of days is rounded up to an integer, then -
denotes a strict inequality); *(mm-6)
expands to the files modified within the last 5 minutes, and so on.
¹ rm -i
(and plain rm
for read-only files) uses it to read a confirmation y
before deletion.
Upvotes: 9
Reputation: 359905
If your find
supports it, as GNU find
does, you can use:
find -type f -newermt "Nov 21" ! -newermt "Nov 22" -delete
which will find files that were modified on November 21.
Upvotes: 2
Reputation: 47311
If you need to try find for any given day,
this might help
touch -d "2010-11-21 23:59:59" /tmp/date.start;
touch -d "2010-11-23 00:00:00" /tmp/date.end;
find ./ -type f -newer /tmp/date.start ! -newer /tmp/date.end -exec rm {} \;
Upvotes: 2
Reputation: 140237
You would be better suited to use the find
command:
find . -type f -mtime 1 -exec echo rm {} +
This will delete all files one day old in the current directory and recursing down into its sub-directories. You can use '0' if you want to delete files created today. Once you are satisfied with the output, remove the echo
and the files will truly be deleted.
Upvotes: 1