我有这个文件夹结构,每个子文件夹中都有非常重的高质量图像
tree -d ./
输出:
./
├── 1_2dia-pedro
├── 3dia-pedro
│ ├── 101MSDCF
│ └── 102MSDCF
├── 4dia-pedro
└── Wagner
├── 410_0601
├── 411_0701
├── 412_0801
├── 413_2101
├── 414_2801
├── 415_0802
├── 416_0902
├── 417_1502
├── 418_1602
├── 419_1702
├── 420_2502
└── 421_0103
18 directories
而且,我想压缩它,就像使用 ffmpeg 或 imagemagick 一样。 例如,
ffmpeg -i %%F -q:v 10 processed/%%F"
mogrify -quality 3 $F.png
我目前正在考虑使用 shopt、as discussed here
创建目录的向量shopt -s globstar dotglob nullglob
printf '%q\n' **/*/
然后,创建一个新的压缩文件夹,结构相同
mkdir folder-compressed
<<iterate the array-vector-out-of-shopt>>
最后,为每个子文件夹压缩in the lines of
mkdir processed
for f in *.jpg;
do ffmpeg -i "$f" -q:v 1 processed/"${f%.jpg}.jpg";
done
另外,我阅读了this question,这个过程似乎接近我想要的,
for f in mydir/**/*
do
# operations here
done
主要问题:我是 bash 新手。我知道我可以使用所有需要的工具!
编辑:有 a program,为了以无损质量压缩图像的特定目的,为我们提供了一个线性,以及这种压缩的许多选项。警告:制作原始文件夹结构文件的另一个副本,因为它会在您提供的文件夹结构文件中永久更改它们。
cp -r ./<folder-structure-files> ./<folder-structure-files-copy>
image_optim -r ./<folder-structure-files-copy>
不过,我认为@m0hithreddy 解决方案非常酷。当然,我很快就会在其他地方使用这种逻辑。
答案 0 :(得分:1)
您可以动态创建所需的目录,而不是预先mkdir
创建目录。与循环相比,递归解决方案对我来说看起来很优雅。这是一个直接的方法。我echo
编辑了文件名和目录以跟踪正在发生的事情。我不是 ffmpeg
专业人士,我使用了 cp
,但应该适合您的用例。
Shell 脚本:
source=original/
destination=compressed/
f1() {
mkdir -p ${destination}${1}
for file in `ls ${source}${1}*.jpg 2>/dev/null`
do
echo 'Original Path:' ${file}
echo 'Compressed Path:' ${destination}${1}$(basename $file) '\n'
cp ${file} ${destination}${1}$(basename $file)
done
for dir in `ls -d ${source}${1}*/ 2>/dev/null`
do
echo 'Enter sub-directory:' ${dir} '\n'
f1 ${dir#*/}
done
}
f1 ''
终端会议:
$ ls
original script.sh
$ tree original/
original/
├── f1
│ ├── f16
│ │ └── f12.jpg
│ ├── f5
│ │ └── t4.jpg
│ └── t3.txt
├── f2
│ └── t5.txt
├── f3
├── f4
│ └── f10
│ ├── f2
│ │ └── f6.jpg
│ └── f3.jpg
├── t1.jpg
└── t2.txt
8 directories, 8 files
$ sh script.sh
Original Path: original/t1.jpg
Compressed Path: compressed/t1.jpg
Enter sub-directory: original/f1/
Enter sub-directory: original/f1/f16/
Original Path: original/f1/f16/f12.jpg
Compressed Path: compressed/f1/f16/f12.jpg
Enter sub-directory: original/f1/f5/
Original Path: original/f1/f5/t4.jpg
Compressed Path: compressed/f1/f5/t4.jpg
Enter sub-directory: original/f2/
Enter sub-directory: original/f3/
Enter sub-directory: original/f4/
Enter sub-directory: original/f4/f10/
Original Path: original/f4/f10/f3.jpg
Compressed Path: compressed/f4/f10/f3.jpg
Enter sub-directory: original/f4/f10/f2/
Original Path: original/f4/f10/f2/f6.jpg
Compressed Path: compressed/f4/f10/f2/f6.jpg
$ tree compressed/
compressed/
├── f1
│ ├── f16
│ │ └── f12.jpg
│ └── f5
│ └── t4.jpg
├── f2
├── f3
├── f4
│ └── f10
│ ├── f2
│ │ └── f6.jpg
│ └── f3.jpg
└── t1.jpg
8 directories, 5 files