Jesse Fulton
Jesse Fulton

Reputation: 2218

Finding files in subdirectories created after a certain date

I'm in the process of writing a bash script (just learning it) which needs to find files in subdirectories created after a certain date. I have a folder /images/ with jpegs in various subfolders - I want to find all jpegs uploaded to that directory (or any subdirectories) after a certain date. I know about the -mtime flag, but my "last import" date is stored in %Y-%m-%d format and it'd be nice to use that if possible?

Also, each file/pathname will then be used to generate a MySQL SELECT query. I know find generally outputs the filenames found, line-by-line. But if find isn't actually the command that I should be using, it'd be nice to have a similar output format I could use to generate the SELECT query (WHERE image.file_name IN (...))

Upvotes: 0

Views: 280

Answers (2)

chanchal1987
chanchal1987

Reputation: 2357

Try below script:

DATE=<<date>>
SEARCH_PATH=/images/
DATE=`echo $DATE|sed 's/-//g'`
DATE=$DATE"0000"
FILE=~/timecheck_${RANDOM}_$(date +"%Y%m%d%H%M")
touch -t $DATE $FILE
find $SEARCH_PATH -newer $FILE 2>/dev/null|awk 'BEGIN{f=0}{if(f==1)printf("\"%s\", ",l);l=$0;f=1}END{printf("\"%s\"",l)}'
rm -f $FILE

Upvotes: 1

Borealid
Borealid

Reputation: 98469

You can convert your date into the "last X days" format that find -mtime expects.

find is the correct command for this task. Send its output somewhere, then parse the file into the query.

Beware of SQL injection attacks if the files were uploaded by users. Beware of special-character quoting even if they weren't.

Upvotes: 1

Related Questions