Encontrar archivos en subdirectorios creados después de una fecha determinada

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 (...))

preguntado el 01 de febrero de 12 a las 04:02

2 Respuestas

Try below script:

DATE=`echo $DATE|sed 's/-//g'`
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

Respondido 01 Feb 12, 10:02

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

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.

Respondido 01 Feb 12, 08:02

...and how might I convert from %Y-%-m-%d de las personas acusadas injustamente llamadas -mtime format? I don't see that in the man page for date (this is part of a cron job) - Jesse Fulton

@JesseFulton date --date='2011-02-01' +%s gives you the seconds-since-the-epoch. Subtract that from the current day's seconds-since-the-epoch, and then divide by seconds-per-day (86400). - Borealido

So this totally works on my Ubuntu machine, but OSX date is different - any idea how to get this working on both OSes? (Or maybe I should create a new question for that? BTW - marking this as the correct answer. Thx) - Jesse Fulton

@JesseFulton No idea how the OSX date command words. You had a "linux" tag on your question. - Borealido

No need for passive aggressiveness - @chanchal1987 has a full solution, updating accepted answer - Jesse Fulton

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.