Find all files containing the file name of a specific date range in terminal / Linux

I have a surveillance camera that captures the database of images in my current state. Images are saved on my Linux. Image naming convention is 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, in fact, now I'm trying to find a way to get all the files between a specific date range (filename) using a terminal command. Note. It is necessary to monitor the file name (YYYYMMDDHHMMSS), and not the created / modified file time.

For example, I need to get all the files whose file name is between 2017-10-20 08:30:00 and 2017-10-22 09:30:00

I try to search google and get the following command -

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

It returns files created or modified in the specified date range. But I need to find the file base in this range of file names. Therefore, I think this does not work in my condition.

Also try the following command:

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

This does not work..:(

Please help me write a real team. Thanks in advance.

+5
source share
3 answers

Finish finding a + 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##*/} - get the base file name

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

  • [[ $d -ge 20171020083000 && $d -le 20171022093000 ]] && echo "$0" - print the path to the file only if its "date" "date" is in the specified range

+1
source

One way (bash), not elegant:

 ls CAPTURE04.2017102008{30..59}*.jpg CAPTURE04.2017102009{00..30}*.jpg 2>/dev/null 
0
source

since the maxdepth option is used, all files are in the current directory, so they can be executed in a loop with globes

 for file in CAPTURE04.201710{20..22}*.jpg; do if [[ $file > CAPTURE04.20171020083000 && $file < CAPTURE04.2017102209300 ]]; then ... do something with "$file" fi done 
0
source

Source: https://habr.com/ru/post/1272908/


All Articles