Skip to main content
 首页 » 编程设计

bash中重命名网络摄像机中的最新图像并删除其他图像

2025年02月15日164leader

我没有 bash 编程经验,相机的固件也很糟糕。我已经完成了我想要在这里完成的项目:

http://www.computer-wiz.ca/snowmobile-test-page

  • 使用具有正确固件的不同相机,将图片上传到 ftp 并重命名,覆盖之前的图片并允许我在网页上显示该图片,因为它具有相同的名称,但它是在过去几分钟内更新的。这是我目前正在进行的一个项目的演示,现在使用相同品牌的相机,并且固件不足,无法重命名文件。因此,我留下了一个充满文件的目录,命名如下:
Schedule_20141111_044312.jpg 
Schedule_20141111_045312.jpg 

As it uploads every 10 minutes. My godaddy server only runs a cronjob twice an hour. I need to:

  • rename the most recent file in this folder to something constant that my webpage can display- ex: image.jpg
  • move it to a folder called watermark where my php script can watermark it, and overwrite whatever file was there from the last move
  • delete all the other pictures in the folder the camera uploads them to, so the server doesn't fill up

I realize this is a lot to ask and that third party software is out there, but I've already tried some and they slap branding all over the picture. The current code I have is this:

#!/bin/sh 
cd myhomedirectory/webcamuploads/FI9828W_00626E52FF8D/snap/ 
mv `ls -1t | head -1` myhomedirectory/html/webroot/webcamuploads/watermark/ 

它甚至不喜欢这个标题。我也不断收到文件不存在的错误...

帮忙?

请您参考如下方法:

我只是依次将每个文件移动到位,让新文件覆盖旧文件。在文件系统中移动文件并不昂贵;这只是对目录条目的更改,而不是实际将实际文件从一个地方复制到另一个地方。

由于文件的命名合理,因此无论您按字典顺序(这就是 Schedule_*.jpg 的作用)还是按时间顺序排序,顺序都是相同的。

cd myhomedirectory/webcamuploads/FI9828W_00626E52FF8D/snap/ 
for f in Schedule_*.jpg; do 
    mv "$f" myhomedirectory/html/webroot/webcamuploads/watermark/ 
done 

无需显式删除任何内容,因为较旧的文件只会被较新的文件“覆盖”。循环完成后,仅保留最新的文件。