3

I am trying to find a way to code a batch script to delete x-xxx.jpg that were created before a certain date.

How would this be done? So basically say I have 20k.jpg and I want to create a batch script on Linux to run which will delete all images created let's say before 12th December 2011.

1
  • Files created before a certain date? Or modified before a certain date? Because in general Linux filesystems do not store the creation date of a file - unlike Windows. Well, ext4 does store the creation date IIRC but the userspace has no standardized access to that timestamp... Commented Dec 22, 2012 at 9:47

5 Answers 5

4

Instead of depending on a date, why cant we delete file those are 'N' days old?

For example here is the 'find' command to 'print' *.jpg files those were created before 2 days.

find /folder_path/ -iname '*.jpg' -type f -ctime  +1 -print 

Note: replace the '-print' option with '-delete' if you want to delete files returned from the search result.

Here is an excellent tutorial for 'find' command

Sign up to request clarification or add additional context in comments.

2 Comments

This is awesome. Better than using a file straight from putty thanks!
+1 for correctly addressing the actual problem.
1

The following script uses the -ot test ("older to"):

#/bin/bash
touch -d "$1" /tmp/D
shift
for file in "$@" ; do
    [[ $file -ot /tmp/D ]] && rm "$file"
done
rm /tmp/D

Save it as delete-older.sh, make it executable chmod u+x delete-older.sh, run it as

path/to/script/delete-older.sh 2011/12/12 *.jpg

Comments

1

find . -name "*.jpg" -mtime +1 -ok rm {} \;

Comments

0

To delete jpg file older than, for example, 20 days:

find . -mtime +1 -name '*.jpg'

Comments

0

on centos 4.4, you have to use single quote for {},e.g.

# delete the "log/*.log" files that are 7 days ago. 
find log/*.log -mtime +7 -exec rm '{}' \; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.