
$ 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_/
)를 기반으로 앞에 0을 추가할 수 있습니다. 이를 모두 종합하면 다음과 같습니다.
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자리까지만 채우려는 한 이것이 작동할 것이라고 생각합니다 .
답변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/' *