Shell command line batch processing image file names
Foreword:
Downloaded a bunch of pictures from the Internet, some are *.jpg, some are *.jpeg. And the file name is long and short, which is very bad. Therefore, I want to organize all these files, of course, use the shell to process it!
Just do it.
loop through all files
First of all, I put all the messy pictures in the ./image/ folder.
Then write an i.sh shell file in the outer layer and enter the following content.
My file structure demo is as follows:
document |
---|
image/xxx.jpg |
image/xxx.jpeg |
i.sh |
1
2
3
|
for
f
in
$(
find
.
/image
-iname
"*.*"
);
do
echo
$f
done
|
Then execute the sh i.sh command, and successfully output all the image files in the command line.
Implement i++ digital effects
I want to name all the pictures as picture files like 1.jpg 2.jpg, so I need an i++ effect similar to js.
So, modify the above code to
1
2
3
4
5
6
7
8
9
|
# 搞一个i的变量
i=1
for
f
in
$(
find
.
/image
-iname
"*.*"
);
do
## 打印 i
echo
$i
echo
$f
## 计算i++
((i++))
done
|
OK, the numbers have been successfully output. At this step, it is obvious that we have obtained what we want. Just copy the file below.
Implement the renaming effect
1
2
3
4
5
6
7
8
|
# 搞一个i的变量
i=1
mkdir
img
for
f
in
$(
find
.
/image
-iname
"*.*"
);
do
cp
$f .
/img/
$i.jpg
## 计算i++
((i++))
done
|
Well, as above, we renamed all the pictures according to the numbers and put them in a new img folder. The effect we want is achieved.
Name images with MD5 values
It suddenly occurred to me that numbers are unreliable. The next time I accidentally execute it, it is easy to mess up the pictures. The MD5 value is still reliable, and pay attention to filtering the same pictures.
Well, the ideal is very full, let's take a look.
1
|
md5
-q
$file
|
The MD5 calculation value of the file can be output. Just use this. Modify the above code as follows:
1
2
3
4
5
6
7
|
mkdir
img
for
f
in
$(
find
.
/image
-iname
"*.*"
);
do
# 计算MD5值,并赋予一个变量
a=$(md5 -q $f)
# 复制文件
cp
$f .
/img/
$a.jpg
done
|
I want to repeat the implementation with this bunch of files, so I use copy. If I don't need to think about it like this, I can use mv to rename. The code is as follows:
1
2
3
4
5
6
7
|
mkdir
img
for
f
in
$(
find
.
/image
-iname
"*.*"
);
do
# 计算MD5值,并赋予一个变量
a=$(md5 -q $f)
# 复制文件
mv
$f .
/image/
$a.jpg
done
|