找到文件名时间戳之间的文件。

huangapple go评论50阅读模式
英文:

Find files between time from file names timestamp

问题

我有一个间隔几分钟拍照的延时摄像机,无论是白天还是黑夜,都会拍照。我想只传递白天的照片给ffmpeg来制作电影。
因此,我有一个文件夹,里面装满了带有时间戳的图像文件名:

```bash
photo_2023-02-24_10-48-23.jpg
photo_2023-02-24_03-42.jpg
photo_2023-02-23_23-12-06.jpg
photo_2023-02-23_12-22.jpg
photo_2023-02-23_12-22-03.jpg
...

我想获取所有白天拍摄的图像(例如在08:00和17:00之间)。

到目前为止,我想出了这个正则表达式。我想选择文件名中的“小时”部分。我如何重复使用该部分以在下一步中进行比较?

find -E . -type f -regex ".*/photo_[0-9]{4}-[0-9]{2}-[0-9]{2}_([0-9]{2}).*" -exec bash -c \
'fn=${0##*/}; h=${HOUR};
 [[ $h -ge 8 && $h -le 17 ]] && echo "$0"' {} \;

到目前为止,这是有帮助的:
https://stackoverflow.com/questions/46951256/find-all-files-containing-the-filename-of-specific-date-range-on-terminal-linux


<details>
<summary>英文:</summary>

I have a timelapse cam taking pictures every few minutes, no matter if its day or night. I want to pass only the daytime images to ffmpeg to create a movie.
So I have a folder full of images with their timestamps in the filename:

photo_2023-02-24_10-48-23.jpg
photo_2023-02-24_03-42.jpg
photo_2023-02-23_23-12-06.jpg
photo_2023-02-23_12-22.jpg
photo_2023-02-23_12-22-03.jpg
...


I want to get all images that were taken in daytime (e.g. between 08-00 and 17-00).

So far I came up with this regex. I want to select the &quot;hour&quot; in the name. How do I reuse that part to compare it in the next step?

```sh
find -E . -type f -regex &quot;.*/photo_[0-9]{4}-[0-9]{2}-[0-9]{2}_([0-9]{2}).*&quot; -exec bash -c \
&#39;fn=${0##*/}; h=${HOUR};
 [[ $h -ge 8 &amp;&amp; $h -le 17 ]] &amp;&amp; echo &quot;$0&quot;&#39; {} \;

Helpful so far:
https://stackoverflow.com/questions/46951256/find-all-files-containing-the-filename-of-specific-date-range-on-terminal-linux

答案1

得分: 1

要获取在小时位置包含从08到16(包括)的所有文件,您可以使用以下命令:
```sh
find . -type f -regex &quot;.*/photo_[0-9]{4}-[0-9]{2}-[0-9]{2}_(0[89]|1[0-6]).*&quot;
英文:

If you want to get all files containing number from 08 to 16 (including) in hour position, you could use this command:

find . -type f -regex &quot;.*/photo_[0-9]{4}-[0-9]{2}-[0-9]{2}_(0[89]|1[0-6]).*&quot; 

huangapple
  • 本文由 发表于 2023年3月31日 21:29:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定