我在重命名文件夹中的一堆文件时遇到了一些问题。剧本:
for file in $1
do
mv $file $file | sed -r 's/^.{20}//' | sed 's/.\{16\}$//'
done
输出:
mv: cannot move `/home/timothy/Videos/DB/' to a subdirectory of itself, `/home/timothy/Videos/DB/DB'
SED命令是正确的,只是我在使用mv参数做错了什么。
这些是前5个文件的名称:
[a-s]_dragon_ball_-_001_-_the_secret_of_the_dragon_balls__rs2_[4FC1375C]
[a-s]_dragon_ball_-_002_-_the_emperors_quest__rs2_[59F9C743]
[a-s]_dragon_ball_-_003_-_the_nimbus_cloud_of_roshi__rs2_[0C592F5F]
[a-s]_dragon_ball_-_004_-_oolong_the_terrible__rs2_[47CE4923]
[a-s]_dragon_ball_-_005_-_yamcha_the_desert_bandit__rs2_[B6A035BF]
它应该变成这样:
001_-_the_secret_of_the_dragon_balls
002_-_the_emperors_quest
003_-_the_nimbus_cloud_of_roshi
004_-_oolong_the_terrible
005_-_yamcha_the_desert_bandit
我假设您有类似的文件,
[a-s]_dragon_ball_-_001_-_the_secret_of_the_dragon_balls__rs2_[4FC1375C]
[a-s]_dragon_ball_-_002_-_the_emperors_quest__rs2_[59F9C743]
[a-s]_dragon_ball_-_003_-_the_nimbus_cloud_of_roshi__rs2_[0C592F5F]
[a-s]_dragon_ball_-_004_-_oolong_the_terrible__rs2_[47CE4923]
[a-s]_dragon_ball_-_005_-_yamcha_the_desert_bandit__rs2_[B6A035BF]
这些文件的开头为[as]。然后您希望将它们重命名为,(根据您的评论)
001_-_the_secret_of_the_dragon_balls
002_-_the_emperors_quest
003_-_the_nimbus_cloud_of_roshi
004_-_oolong_the_terrible
005_-_yamcha_the_desert_bandit
在下面使用此脚本,
#!/bin/bash
for file in [\[a\-s\]]*
do
newfile=`echo "$file" | sed -r "s/^.{20}//" | awk -F "__rs2" '{print $1}'`
mv $file $newfile
done
它是如何工作的
该脚本扫描以“ [as]”开头的当前目录中的所有文件名。接下来,对于每个扫描的文件名,for循环内的脚本将创建新文件名,并保留前20个字符,sed
并以字符串“ __rs2”开头。这样就删除了名称中不需要的部分。最后,将文件一一重命名为新文件名。
用法
将代码另存为rename_file.sh
。(说)接下来,将其放在所有这些文件所在的目录中。授予脚本执行权限。在终端上写
chmod +x rename_file.sh
最后要重命名文件,只需在终端中写入,
./rename_file.sh
完毕。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句