简体   繁体   English

用于查找,处理和重命名文件的Bash脚本?

[英]Bash Script to find, process and rename files?

I am trying to put together a script which will run through all the files on my server (under various subdirectories) , look for .jpeg files and run them through a translator which converts them to non progressive jpgs. 我试图整理一个脚本,它将运行我服务器上的所有文件(在各种子目录下),查找.jpeg文件并通过转换器运行它们将它们转换为非渐进式jpgs。

I have: 我有:

 find /home/disk2/ -type f -iname "*.jpg" 

Which finds all the files. 找到所有文件。

Then if it finds for example 1.jpg, I need to run: 然后,如果它找到例如1.jpg,我需要运行:

/usr/bin/jpegtrans /file location/1.jpg > /file location/1.jpg.temp

The jpegtrans app converts the file to a temp file which needs to replace the original file. jpegtrans应用程序将文件转换为临时文件,需要替换原始文件。 So then I need to delete the original and rename 1.jpg.temp to 1.jpg 所以我需要删除原文并将1.jpg.temp重命名为1.jpg

 rm /file location/1.jpg
 mv /file location/1.jpg.temp /file location/1.jpg

I can easily do this for single files but i need to do it for 100's on my server. 我可以轻松地为单个文件执行此操作,但我需要在我的服务器上执行100。

Use find with -exec : 使用find with -exec

find /home/disk2/ -type f -iname "*.jpg" -exec sh -c "/usr/bin/jpegtrans {} > {}.temp; mv -f {}.temp {}" \;

EDIT: For handling spaces in filenames, say: 编辑:对于处理文件名中的空格,请说:

find /home/disk2/ -type f -iname "*.jpg" -exec sh -c "/usr/bin/jpegtrans '{}' > '{}.temp'; mv -f '{}.temp' '{}'" \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM