根据目录中的另一个文件Linux的重命名文件? [英] Linux rename files based on another file in the directory?
问题描述
我已经得到了约750目录中包含每两个文件:
I've got about 750 directories that contain two files each:
long_somewhat_random_filename.jpg
thumb.jpg
我想要做的是使用找到
或类似的东西改名 thumb.jpg
到 long_somewhat_random_filename_thumb.jpg
。我的脑子有点儿模糊的时刻。
What I'd like to do is use find
or something similar to rename thumb.jpg
to long_somewhat_random_filename_thumb.jpg
. My brain's kinda fuzzy at the moment.
我可以用一个perl脚本做到这一点,但如果有一个有点简单的方法来做到这在bash,这是更简单。
I could do it with a perl script, but if there's a somewhat easy way to do it in bash, that's easier.
推荐答案
下面举一个镜头脚本。眼下回声
使它良性所以你可以的先试后买的这么说。如果你喜欢你所看到的,删除回声
并再次运行该脚本实际上所做的更改。
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")
输入
$ 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
输出
$ ./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
这篇关于根据目录中的另一个文件Linux的重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!