在Mac OS X上重命名shell中的多个文件

时间:2016-02-20 18:59:42

标签: shell rename multiple-files

我有以下数百个文件:

22170000000000002 Module 1 Quiz Quiz Item Analysis Report.csv
22170000000000112 Module 2 Quiz Quiz Item Analysis Report.csv
22180000000000002 Module 3 Quiz Quiz Item Analysis Report.csv

如何使用iOS系统终端重命名它们

2217002_01_Item.csv
2217112_02_Item.csv
2218002_03_Item.csv

我知道有一些关于重命名文件的线程,但我对shell命令真的很新。我的具体案例需要帮助。

2 个答案:

答案 0 :(得分:1)

# create test environnement:
mkdir tmp
cd tmp
echo "22170000000000002 Module 1 Quiz Quiz Item Analysis Report.csv" > "22170000000000002 Module 1 Quiz Quiz Item Analysis Report.csv"
echo "22170000000000112 Module 2 Quiz Quiz Item Analysis Report.csv" > "22170000000000112 Module 2 Quiz Quiz Item Analysis Report.csv"
echo "22180000000000002 Module 3 Quiz Quiz Item Analysis Report.csv" > "22180000000000002 Module 3 Quiz Quiz Item Analysis Report.csv"

# test file renaming:
f="22170000000000002 Module 1 Quiz Quiz Item Analysis Report.csv"
echo $f | sed -e "s/^\(....\)[0-9]*\([0-9][0-9][0-9]\) .*Module \([0-9]\)* .*$/\1\2_0\3_Item.csv/g"
#return 2217002_01_Item.csv

# Verifing in loop 
ls -1 | while read f
do
  new_name=`echo $f | sed -e "s/^\(....\)[0-9]*\([0-9][0-9][0-9]\) .*Module \([0-9]\)* .*$/\1\2_0\3_Item.csv/g"`
  echo "$f     ---->   $new_name"
done

# To rename all files : 
ls -1 | while read f
do
  new_name=`echo $f | sed -e "s/^\(....\)[0-9]*\([0-9][0-9][0-9]\) .*Module \([0-9]\)* .*$/\1\2_0\3_Item.csv/g"`
  echo "$f     ---->   $new_name"
  mv "$f" "$new_name"     
done

ls -1

答案 1 :(得分:0)

您可能需要查看重命名命令:

http://man7.org/linux/man-pages/man1/rename.1.html