How to delete files from a specific date?

Guys, I am wondering how to remove files for a specific date in a directory?

for instance when I do ls -l , i see many files. And i want to delete files for date May 15:

58252015 May 10 03:45 my_05102012.log
58252015 May 15 06:45 my_05152012.log

Thanks

based on your sample:

cd /directory
rm *05152012.log

I am assuming you mean to delete based on file name not UNIX file time/date

Not the file name.

I am looking for the Unix file time and date.

One way:

touch -t 201205150000.00 /tmp/PID$$.marker1
touch -t 201205160000.00 /tmp/PID$$.marker2
find /your/directory -type f -newer /tmp/PID$$.marker1 ! -newer /tmp/PID$$.marker2 -ls
rm /tmp/PID$$.*

This code justs lists the regular files that were modified on 2012/05/15. You can drop the -ls from the find and pipe it to xargs rm -f once you are sure that the list of files produced is what you want to delete.

Thanks agama.

Any simpler way to do?

Maybe, but more room for error:

ls -l /your/directory |grep "^-.*May 15" #|xargs rm

Remove the hash when you're sure. It deletes all that appear to be regular files (leading dash in the ls output) and have the date (May 15) somewhere in the rest of the record. Realise that it will delete files from May 15th of previous years, as well as any files that have that string in the name. Like I said, not exact and lots of room for error.

find /path/to/directory -ctime <integer-number-of-days-from-today> -type f |xargs rm

e.g. today is May 16 and May 12 is the target day (4 days ago) and my files are in folder mydir

cd mydir
find . -ctime -4 -type f |xargs rm

Please test it by making a prototype directory (or making a backup of your current directories) before you execute the command.

Update: it will delete all files made between now and 4 days ago (perhaps not what you're looking for - apologies).
There's something mentioned here: http://www.linuxquestions.org/questions/red-hat-31/delete-move-copy-files-of-specific-date-245888/\#15

Finally:

cd mydir
find  . -maxdepth 1 -ctime -4 -type f -not -ctime -3 |xargs rm

As mentioned earlier, please use with caution :slight_smile:

i seriously do not recommend using "find" for this problem, as (as you know) it searches recursively...
i prefer the straightforward approach:

ls -l | grep "May 15" | awk '{print $9;}' | tee /tmp/log

once i'm happy with that output:

/bin/rm `cat /tmp/log`

This is using some of the ideas also offered above. Primarily, the idea that you are able to check your work (multiple times) before running the final 'rm' command.

oh... and you can add "| grep -v ^d" in that pipeline to avoid grabbing directories.

I seriously recommend against recommending to not use find for this task ;).

find does not have to recurse into subdirectories if that is not desired. On GNU/Linux and *BSD systems, all finds support -maxdepth n . On systems that do not support that primary, the following gets the job done:

find "$path" ! -name "$path" -prune ...

Your "straightforward approach" is actually more complicated, brittle, and error prone. In addition to all the reasons mentioned by agama in post #6, filenames with whitespace will not make it through intact.

grep/awk are misc text processing tools. find's primaries know what they're looking at. In my opinion, find is the right tool for the job.

Regards,
Alister