Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all files containing the filename of specific date range on Terminal/Linux

I have a surveillance camera which is capturing image base on my given condition. The images are saved on my Linux. Image naming convention are given below-

CAPTURE04.YYYYMMDDHHMMSS.jpg

The directory contains the following files -

CAPTURE04.20171020080501.jpg
CAPTURE04.20171021101309.jpg
CAPTURE04.20171021101913.jpg
CAPTURE04.20171021102517.jpg
CAPTURE04.20171021103422.jpg
CAPTURE04.20171022103909.jpg
CAPTURE04.20171022104512.jpg
CAPTURE04.20171022105604.jpg
CAPTURE04.20171022110101.jpg
CAPTURE04.20171022112513.jpg ... and so on.

However, Actually, now I'm trying to find a way to get all files between a specific date time (filename) range by using the terminal command. Note: Need to follow the filename (YYYYMMDDHHMMSS), not the file created/modified time.

Such as I need to get all files whose file name is between 2017-10-20 08:30:00 and 2017-10-22 09:30:00

I'm trying and searching google around and got the following command -

find -type f -newermt "2017-10-20 08:30:00" \! -newermt "2017-10-22 09:30:00" -name '*.jpg'

It returns the files which are created/modified on that given date range. But I need to find files base on the given filenames range. So I think it does not work on my condition.

Also trying with the following command-

find . -maxdepth 1 -size +1c -type f \( -name 'CAPTURE04.20171020083000*.jpg' -o -name 'CAPTURE04.2017102209300*.jpg' \) | sort -n

This is not working.. :(

Please help me to write the actual command. Thanks, in advance.

like image 329
Sharif Avatar asked Oct 26 '17 09:10

Sharif


1 Answers

Complete find + bash solution:

find . -type f -regextype posix-egrep -regex ".*CAPTURE04\.[0-9]{14}\.jpg" -exec bash -c \
'fn=${0##*/}; d=${fn:10:-4}; 
 [[ $d -ge 20171020083000 && $d -le 20171022093000 ]] && echo "$0"' {} \;

  • fn=${0##*/} - obtaining file basename

  • d=${fn:10:-4} - extracting datetime section from the file's basename

  • [[ $d -ge 20171020083000 && $d -le 20171022093000 ]] && echo "$0" - print the filepath only if its datetime "section" is in specified range

like image 174
RomanPerekhrest Avatar answered Sep 18 '22 00:09

RomanPerekhrest