これは、私がこれを書いた時点で存在していた 3 つの回答のベンチマークです。自由に拡張してください:
$ ./rename_bench
setting up
timing ext
real 0m35.453s
user 0m6.006s
sys 0m27.417s
setting up
timing sed
real 1m17.498s
user 0m15.223s
sys 0m56.376s
setting up
timing straight
real 0m38.352s
user 0m6.028s
sys 0m28.254s
$ ./rename_bench
setting up
timing ext
real 0m36.234s
user 0m6.030s
sys 0m28.270s
setting up
timing sed
real 1m16.467s
user 0m15.277s
sys 0m56.194s
setting up
timing straight
real 0m33.538s
user 0m5.911s
sys 0m26.672s
実際のベンチマーク スクリプト:
#!/bin/bash
setup() {
cd $(mktemp -dt "foo") &&
touch 'a - '{0..10000}'buzz.jpg'
}
ext_rename() {
shopt -s extglob
oIFS="$IFS" # save the original IFS
for file in *.jpg; do # Or whatever pattern you like
target=( ${file//+(-)/ } ) # Break the filename into an array on spaces, after turning - into space.
IFS='-' # Temporarily set the internal field separator into a dash so we can join on dashes.
mv "$file" "${target[*]}"
IFS="$oIFS"
done
}
sed_rename() {
for f in *.jpg; do
mv "$f" "$(sed -e 's/ /-/g' -e 's/---/-/g' <<< "$f")"
done
}
straight_rename() {
for file in *.jpg; do
file2="${file// - /-}"
file2="${file2// /-}"
mv "$file" "$file2"
done
}
echo "setting up"
setup
echo "timing ext"
time ext_rename
echo "setting up"
setup
echo "timing sed"
time sed_rename
echo "setting up"
setup
echo "timing straight"
time straight_rename