
$ ls
pro2e_u01_txt_vocabulaire_11_descriptiveadjectives.mp3
pro2e_u01_txt_vocabulaire_12_nationality.mp3
pro2e_u01_txt_vocabulaire_1_campus.mp3
pro2e_u01_txt_vocabulaire_2_personnes.mp3
pro2e_u01_txt_vocabulaire_3_presentations.mp3
pro2e_u01_txt_vocabulaire_4_identifier.mp3
pro2e_u01_txt_vocabulaire_5_bonjouraurevoir.mp3
pro2e_u01_txt_vocabulaire_6_commentcava.mp3
pro2e_u01_txt_vocabulaire_7_expressionspolitesse.mp3
このようにソートされるように、中央の数字の前に「0」を追加したいと思います。
$ ls -v
pro2e_u01_txt_vocabulaire_1_campus.mp3
pro2e_u01_txt_vocabulaire_2_personnes.mp3
pro2e_u01_txt_vocabulaire_3_presentations.mp3
pro2e_u01_txt_vocabulaire_4_identifier.mp3
pro2e_u01_txt_vocabulaire_5_bonjouraurevoir.mp3
pro2e_u01_txt_vocabulaire_6_commentcava.mp3
pro2e_u01_txt_vocabulaire_7_expressionspolitesse.mp3
pro2e_u01_txt_vocabulaire_11_descriptiveadjectives.mp3
pro2e_u01_txt_vocabulaire_12_nationality.mp3
私が今まで持っているのは
$ for i in *.mp3; do echo ${i/_[0-9]_/_0¿_}; done
pro2e_u01_txt_vocabulaire_11_descriptiveadjectives.mp3
pro2e_u01_txt_vocabulaire_12_nationality.mp3
pro2e_u01_txt_vocabulaire_0¿_campus.mp3
pro2e_u01_txt_vocabulaire_0¿_personnes.mp3
pro2e_u01_txt_vocabulaire_0¿_presentations.mp3
pro2e_u01_txt_vocabulaire_0¿_identifier.mp3
pro2e_u01_txt_vocabulaire_0¿_bonjouraurevoir.mp3
pro2e_u01_txt_vocabulaire_0¿_commentcava.mp3
pro2e_u01_txt_vocabulaire_0¿_expressionspolitesse.mp3
[0-9]に一致する数字が現れるように、「¿」を置き換えるために何を使うべきかわかりません。
答え1
時には特定の解決策で十分な場合があります。現在のケースと同様に、考慮すべきファイルサブセットのパターン(たとえば/_[0-9]_/
)を識別し、一意に識別されるプレフィックス(たとえば/re_/
)に基づいて前にゼロを追加できます。これをすべてまとめると次のようになります。
for f in *_[0-9]_*.mp3 ; do mv -i "${f}" "${f/re_/re_0}" ; done
必要な事前確認の場合は、echo
前に追加できますmv
。
答え2
rename
fromを使用してこれを行うこともできますutil-linux
。
rename vocabulaire_ vocabulaire_0 *vocabulaire_[0-9]_*.mp3
結果:
pro2e_u01_txt_vocabulaire_01_campus.mp3
pro2e_u01_txt_vocabulaire_02_personnes.mp3
pro2e_u01_txt_vocabulaire_12_nationality.mp3
pro2e_u01_txt_vocabulaire_231_whatever.mp3
0 を追加するには、コマンドを繰り返して桁数を増やします。
rename vocabulaire_ vocabulaire_0 *vocabulaire_[0-9][0-9]_*.mp3
結果:
pro2e_u01_txt_vocabulaire_001_campus.mp3
pro2e_u01_txt_vocabulaire_002_personnes.mp3
pro2e_u01_txt_vocabulaire_012_nationality.mp3
pro2e_u01_txt_vocabulaire_231_whatever.mp3
答え3
for f in ./*.mp3
do set "${f%_*}" "_${f##*_}"; f=10${1##*_}
mv "$1$2" "${1%_*}_${f#*$((${#f}<4))}$2"
done
_
数値フィールドが最後の区切り文字フィールドと最後の2番目の区切り文字フィールドの間にあり、2桁までを埋めたい場合はこれがうまくいくと思います。
答え4
次のファイルの場合:
test0.txt
test1.txt
test2.txt
...
test1234.txt
これは私にとって効果的です。
rename 's/test([0-9]{1}).txt/test0$1.txt/' *
rename 's/test([0-9]{2}).txt/test0$1.txt/' *
rename 's/test([0-9]{3}).txt/test0$1.txt/' *
結果:
test0000.txt
test0001.txt
test0002.txt
...
test1234.txt
もちろんループで包むこともできます。
重要:ファイルがパディングしたい数字で始まるか終わる場合は、始めと終わりを一致させるために^
andを使用する必要があります。たとえば、次のようになります。$
rename 's/^([0-9]{1}).txt/0$1.txt/' *
または
rename 's/test([0-9]{1})$/test0$1/' *
rename
パラメータを使用すると、-n
実際に名前を変更せずに変更をプレビューできます。たとえば、次のようになります。
rename -n 's/test([0-9]{1}).txt/test0$1.txt/' *