bash 如何递归地使用 JpegTran(命令行)来优化子目录中的所有文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12831293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:30:56  来源:igfitidea点击:

How to recursivly use JpegTran (command line) to optimise all files in subdirs?

macosbashcommand-lineoperating-systemjpeg

提问by slycat

i have Photos in multiple directories. I want to use jpegtran (command line tool) to recursivly go through each one, optimise it, and save it (overwrite it)

我在多个目录中有照片。我想使用 jpegtran(命令行工具)递归遍历每一个,优化它,并保存(覆盖它)

if they are all in one folder i use this

如果它们都在一个文件夹中,我会使用它

for JPEG in *.jpg ; do jpegtran -optimize $JPEG > $JPEG; done

but i can't get it working recursivly and overwriting the same file (rather than to a new filename)

但我无法让它递归工作并覆盖同一个文件(而不是新文件名)

any tips?

有小费吗?

回答by Bas

jpegtran whole directory

jpegtran 整个目录

find /the/image/path -name "*.jpg" -type f -exec jpegtran -copy none -optimize -outfile {} {} \;

回答by Hai Vu

How about using the findcommand:

如何使用find命令:

find /your/dir -name '*.jpg' -exec echo jpegtran -optimize {} \;

Run the command, if you like the output, remove echoto execute it.

运行命令,如果您喜欢输出,请删除echo以执行它。

回答by Ярослав Рахматуллин

It is usually a whole lot easier to use findin such cases, because what you want to do is acton a few filenames. The findutility will give you those names. Assuming you have GNU (or maybe even BSD) tools, the following example will illustrate this common scenario.

它通常是更容易使用一大堆发现在这种情况下,因为你想做的事是行为上的一些文件名。该发现工具会给你这些名字。假设您拥有 GNU(甚至 BSD)工具,以下示例将说明这种常见情况。

For example:

例如:

$ find ~/images/wallpapers/TEMP/ -type f -iname '*jpg' \
    -exec sh -c 'jpegtran -outfile {}.out -optimize {}; mv {}.out {} ' \;
  1. findlooks for all files ending with jpg in the TEMP folder, recursively.
  2. For every full file pathfound (denoted by `{}'), findwill run the command given to -exec
  3. the -exec option cheats and runs several commands instead of one through sh
  1. find以递归方式查找 TEMP 文件夹中所有以 jpg 结尾的文件。
  2. 对于找到的每个完整文件路径(用“{}”表示),find将运行给 -exec 的命令
  3. -exec 选项会欺骗并运行多个命令,而不是通过sh运行一个命令

Notes

笔记

cat file > fileis not allowed because simultaneously reading from, and writing to the same file is not a well defined or even supported operation in bash.

cat file > file不允许,因为同时读取和写入同一个文件在bash 中定义不明确,甚至不受支持。