首先建立随机文件
#!/bin/bash for i in {1..10000} do touch $(head -1 /dev/urandom | md5sum | cut -d ' ' -f 1).qas done
1.rename
这里使用rename
rename 's/\.qas$/\.qwer' *.qas
效率最高,执行命令后,一万个文件几乎没有延迟,瞬间完成
使用time命令检测耗时:
rename 's/\.qas$/\.qwer/' *.qas 0.10s user 0.10s system 99% cpu 0.198 total
2.find命令
find . -type f -name "*.qas" | sed 's/.qas//g' | xargs -i mv {}.qas {}.qwer
耗时:
find . -type f -name "*.qas" 0.02s user 0.02s system 0% cpu 6.509 total sed 's/.qas//g' 0.02s user 0.00s system 0% cpu 8.484 total xargs -i mv {}.qas {}.qwer 7.69s user 2.40s system 99% cpu 10.157 total
3.shell脚本
#!/bin/bash for i in $(ls -1) do first_name=$(echo $i | cut -d '.' -f 1) last_name=$(echo $i | cut -d '.' -f 2) if [[ $last_name == "qas" ]] then mv ${first_name}.qas ${first_name}.qwer fi done
耗时:
./changename.sh 28.95s user 5.73s system 115% cpu 30.017 total