通过bash脚本根据文件夹名称重命名带.jpg扩展名的文件

时间:2022-02-07 10:31:44

I have .jpg file in my folder and its sub folders.

我的文件夹及其子文件夹中有.jpg文件。

image/1/large/imagexyz.jpg 
image/1/medium/imageabc.jpg
image/1/small/imagedef.jpg

and so on for 2,3,4 ...

等2,3,4 ......

I need to rename all image files with its folder name. ie. imagexyz.jpg should be large_1.jpg and imageabc.jpg should be medium_1.jpg and so on.

我需要用其文件夹名称重命名所有图像文件。即。 imagexyz.jpg应该是large_1.jpg,imageabc.jpg应该是medium_1.jpg等等。

4 个解决方案

#1


3  

oldIFS="$IFS"
IFS=/
while read -r -d $'\0' pathname; do
  # expect pathname to look like "image/1/large/file.name.jpg"
  set -- $pathname
  mv "$pathname" "$(dirname "$pathname")/${3}_${2}.jpg"
done < <(find . -name \*.jpg -print0)
IFS="$oldIFS"

#2


3  

#!/bin/sh
find . -type f -name "*.$1" > list
while read line
do
echo $line
first=`echo $line | awk -F/ '{print $2}'`
echo $first 
second=`echo $line | awk -F/ '{print $3}'`
echo $second
name=`echo $line | awk -F/ '{print $4}'`
echo $name

mv "./$first/$second/$name" ./$first/$second/${first}_${second}.$1

done < list

If you save this file as rename.sh then run rename.sh jpg to replace jpg files and rename.sh png to replace png and so on.

如果将此文件另存为rename.sh,则运行rename.sh jpg以替换jpg文件,并运行rename.sh png以替换png,依此类推。

#3


2  

A solution based upon native bash functions (well, except find, then ;-) )

基于本机bash函数的解决方案(好吧,除了查找,然后;-))

#!/bin/bash

files=`find . -type f -name *.jpg`
for f in $files
do
     echo
     echo $f
     # convert f to an array
     IFS='/'
     a=($f)
     unset IFS
     # now, the folder containing a digit
     # are @ index [2]
     # small, medium, large are @ [3]
     # and name of file @ [4]

     echo ${a[2]} ${a[3]} ${a[4]}
     echo ${a[3]}_${a[2]}.jpg
done

#4


1  

Did you mean something like that?

你的意思是那样的吗?

for i in $(find image/ -type f); do 
  mv $i $(echo $i | sed -r 's#image/([0-9]+)/([^/]+)/[^/]+.jpg#\2_\1.jpg#'); 
done

This will move all files from image/$number/$size/$file.jpg to ./${size}_${number}.jpg.

这会将所有文件从image / $ number / $ size / $ file.jpg移动到./${size}_${number}.jpg。

But be aware that you will overwrite your files if there is more than one .jpg file in each image/$number/$size directory (see comment of kurumi).

但请注意,如果每个图像/ $ number / $ size目录中有多个.jpg文件,则会覆盖您的文件(请参阅kurumi的评论)。

#1


3  

oldIFS="$IFS"
IFS=/
while read -r -d $'\0' pathname; do
  # expect pathname to look like "image/1/large/file.name.jpg"
  set -- $pathname
  mv "$pathname" "$(dirname "$pathname")/${3}_${2}.jpg"
done < <(find . -name \*.jpg -print0)
IFS="$oldIFS"

#2


3  

#!/bin/sh
find . -type f -name "*.$1" > list
while read line
do
echo $line
first=`echo $line | awk -F/ '{print $2}'`
echo $first 
second=`echo $line | awk -F/ '{print $3}'`
echo $second
name=`echo $line | awk -F/ '{print $4}'`
echo $name

mv "./$first/$second/$name" ./$first/$second/${first}_${second}.$1

done < list

If you save this file as rename.sh then run rename.sh jpg to replace jpg files and rename.sh png to replace png and so on.

如果将此文件另存为rename.sh,则运行rename.sh jpg以替换jpg文件,并运行rename.sh png以替换png,依此类推。

#3


2  

A solution based upon native bash functions (well, except find, then ;-) )

基于本机bash函数的解决方案(好吧,除了查找,然后;-))

#!/bin/bash

files=`find . -type f -name *.jpg`
for f in $files
do
     echo
     echo $f
     # convert f to an array
     IFS='/'
     a=($f)
     unset IFS
     # now, the folder containing a digit
     # are @ index [2]
     # small, medium, large are @ [3]
     # and name of file @ [4]

     echo ${a[2]} ${a[3]} ${a[4]}
     echo ${a[3]}_${a[2]}.jpg
done

#4


1  

Did you mean something like that?

你的意思是那样的吗?

for i in $(find image/ -type f); do 
  mv $i $(echo $i | sed -r 's#image/([0-9]+)/([^/]+)/[^/]+.jpg#\2_\1.jpg#'); 
done

This will move all files from image/$number/$size/$file.jpg to ./${size}_${number}.jpg.

这会将所有文件从image / $ number / $ size / $ file.jpg移动到./${size}_${number}.jpg。

But be aware that you will overwrite your files if there is more than one .jpg file in each image/$number/$size directory (see comment of kurumi).

但请注意,如果每个图像/ $ number / $ size目录中有多个.jpg文件,则会覆盖您的文件(请参阅kurumi的评论)。