Linux rename files based on another file in the directory?

 ̄綄美尐妖づ 提交于 2019-12-03 12:19:07

Give the script below a shot. Right now the echo makes it benign so you can try before you buy so to speak. If you like what you see, remove the echo and run the script again to actually make the changes.

#!/bin/bash

while read file; do
 echo mv "${file%/*}/thumb.jpg" "${file%.*}_thumb.jpg"
done < <(find . -type f ! -name "thumb.jpg" -name "*.jpg")

Input

$ find . -type f -name "*.jpg"
./dir1/dir1_foo_bar.jpg
./dir1/thumb.jpg
./dir2/dir2_foo_bar.jpg
./dir2/thumb.jpg
./dir3/dir3_foo_bar.jpg
./dir3/thumb.jpg
./dir4/dir4_foo_bar.jpg
./dir4/thumb.jpg
./dir5/dir5_foo_bar.jpg
./dir5/thumb.jpg

Output

$ ./mvthumb.sh
mv ./dir1/thumb.jpg ./dir1/dir1_foo_bar_thumb.jpg
mv ./dir2/thumb.jpg ./dir2/dir2_foo_bar_thumb.jpg
mv ./dir3/thumb.jpg ./dir3/dir3_foo_bar_thumb.jpg
mv ./dir4/thumb.jpg ./dir4/dir4_foo_bar_thumb.jpg
mv ./dir5/thumb.jpg ./dir5/dir5_foo_bar_thumb.jpg
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!