6

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.

Sharif
  • 623
  • 4
  • 12

3 Answers3

4

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

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • I'm working with your given command. I have lots of file in the folder. Is this is the efficient way to do that type of searching? Thank you for your answer. – Sharif Oct 26 '17 at 10:55
  • @Sharif, this solution is unified to search the crucial files `CAPTURE04.YYYYMMDDHHMMSS.jpg` that can be placed in any location (i.e. subfolders/ same-level directories). For global search you can just specify the "root" starting point `find / -type f ....`. As for the perfprmance: you are free to compare and measure through all the posted answers on your own – RomanPerekhrest Oct 26 '17 at 11:15
  • @RomanPerekhrest If my filename in this format **CAM04.YYYYMMDDHHMMSS.jpg**. What will be the code? Would you plz have look the follwing code... `find . -type f -regextype posix-egrep -regex ".*CAM04\.[0-9]{14}\.jpg" -exec bash -c \ 'fn=${0##*/}; d=${fn:6:-4}; [[ $d -ge 20171020083000 && $d -le 20171022093000 ]] && echo "$0"' {} \;` Thanks again for your time. – Sharif Oct 26 '17 at 12:16
  • @Sharif, yes, that looks operable – RomanPerekhrest Oct 26 '17 at 12:18
  • @RomanPerekhrest Need help... I got an error when trying by the following filename format - **CAM.04.YYYYMMDDHHMMSS.mp4.vRandomString.th.jpg** Such as _CAM.04.20171021170115.mp4.v2474.th.jpg_ , _CAM.04.20171021170022.mp4.v2473.th.jpg_ etc. Trying with the following code `find . -type f -regextype posix-egrep -regex ".*CAM\.04\.[0-9]{14}\.mp4\.[a-z0-9]*\.th\.jpg" -exec bash -c \ 'fn=${0##*/}; d=${fn:7:-4}; [[ $d -ge 20171020083000 && $d -le 20171022235959 ]] && echo "$0"' {} \;` – Sharif Oct 29 '17 at 06:38
  • _Error_ **./CAM.04.20170301235959.mp4.v2475.th.jpg: [[: 20170301235959.mp4.v2475.th: syntax error: invalid arithmetic operator (error token is ".mp4.v2475.th")** Eagerly waiting for your response. – Sharif Oct 29 '17 at 06:39
  • @Sharif, that's obvious that you got errors, cause you have changed your filename convention. That's another case – RomanPerekhrest Oct 29 '17 at 06:48
  • @RomanPerekhrest Some files are in that format. Would you plz tell me what I need to add/modify in the code to work that type of filename format. – Sharif Oct 29 '17 at 06:54
  • 1
    @Sharif, the crucial slice for new filenames should be `d=${fn:7:14};` Here you go – RomanPerekhrest Oct 29 '17 at 07:20
1

One way(bash), not an elegant one:

ls  CAPTURE04.2017102008{30..59}*.jpg CAPTURE04.2017102009{00..30}*.jpg 2>/dev/null
Guru
  • 16,456
  • 2
  • 33
  • 46
0

as maxdepth option is used means all files are in current directory so can be done in a loop with globs

for file in CAPTURE04.201710{20..22}*.jpg; do
    if [[ $file > CAPTURE04.20171020083000 && $file < CAPTURE04.2017102209300 ]]; then
        ... do something with "$file"
    fi
done
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36