Created
May 23, 2012 05:49
-
-
Save imom0/2773457 to your computer and use it in GitHub Desktop.
rename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
发信人: sujunwei (su_junwei), 信区: linux | |
标 题: 跪求一段bash命令脚本 | |
发信站: 兵马俑BBS (Wed May 23 11:02:29 2012), 本站(bbs.xjtu.edu.cn) | |
如题 | |
我想改一个文件夹里面里面的文件,文件名字知道,路径需要查找,然后将对应的文件名修 | |
改一个名字 | |
比如需要将当前文件夹或者子文件夹中的 file1、 file2、files3 ...改为_file1、 | |
_file2、_files3 ... | |
fileName="file1 file2 file3" | |
for name in $finename | |
do | |
//查找 | |
// sed 替换 | |
// | |
mv ... | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
filename="file1 file2 file3" | |
function infilelist | |
{ | |
local key=$1 | |
shift | |
for value in $filename | |
do | |
# return ture when key in filelist | |
[[ $key == $value ]] && return 0 | |
done | |
return 1 | |
} | |
function myrename | |
{ | |
local i | |
cd $1 | |
for i in $(ls -1) | |
do | |
# handle dirs | |
if ([[ -d "$i" ]]) | |
then | |
myrename $i | |
fi | |
# rename | |
if ( infilelist $i ) | |
then | |
echo "rename $i to _$i" | |
mv "$i" "_$i" | |
fi | |
done | |
cd .. | |
} | |
# call | |
myrename . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment