2009年2月16日星期一

more multiple file renaming goodness

From:http://www.basicallytech.com/blog/index.php?/archives/11-Shell-stuff-more-multiple-file-renaming-goodness.html

I had an e-mail from a guy named Devon telling me yet more ways to rename
multiple files. I thought they were pretty good (damn it!) and had to
share these new (to me) techniques.

These alternative methods seem to require the use of the Bash shell, so
some people may not wish to read any further!
remove a suffix

$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra.txt
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus.txt
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations.txt
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry.txt
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 matrices.txt

Note the use of the "%" symbol to denote a suffix.

$ for FILE in *.txt; do mv -i "$FILE" "${FILE%.txt}"; done
$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 matrices
change a suffix

$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra.txt
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus.txt
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations.txt
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry.txt
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 matrices.txt

$ for FILE in *.txt; do mv -i "$FILE" "${FILE%.txt}.TXT"; done
$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra.TXT
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus.TXT
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations.TXT
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry.TXT
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 matrices.TXT
remove a prefix

$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 maths_algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 maths_calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 maths_equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 maths_geometry
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 maths_matrices

For prefixes, replace the "%" with a "#".

$ for FILE in maths_*; do mv -i "$FILE" "${FILE#maths_}"; done
$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 matrices
change a prefix

$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 maths_algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 maths_calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 maths_equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 maths_geometry
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 maths_matrices

$ for FILE in maths_*; do mv -i "$FILE" "MATHS_${FILE#maths_}"; done
$ ls -l
total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 MATHS_algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 MATHS_calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 MATHS_equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 MATHS_geometry
-rw-r--r-- 1 rob rob 50 2006-10-20 13:23 MATHS_matrices

Thanks to Devon for that!

shell脚本对文件进行批量改名例子:
把当前文件夹下文件名含有eee的都改成大写的EEE,比如21-eee.doc就要改成21-EEE.doc。

for filename in *eee*
do
newname=`echo $filename|sed -n ’s/eee/EEE/p’`
mv $filename $newname
done

如果要将每个文件的执行权限去掉,这样写:
for filename in *; do chmod ugo-x $filename;done

没有评论: