programing

Bash에서 파일 확장자를 반복적으로 변경합니다.

javajsp 2023. 4. 24. 22:25

Bash에서 파일 확장자를 반복적으로 변경합니다.

예를 들어 디렉토리를 반복적으로 반복하여 특정 확장자의 모든 파일 확장자를 변경합니다..t1로..t2이를 위한 bash 명령어는 무엇입니까?

용도:

find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +

가지고 계신 경우rename다음 중 하나를 사용합니다.

find . -name '*.t1' -exec rename .t1 .t2 {} +
find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' +

debian 14를 새로 설치할 때 제안된 솔루션 중 어느 것도 나에게 효과가 없었습니다.모든 Posix/MacOS에서 작동합니다.

find ./ -depth -name "*.t1" -exec sh -c 'mv "$1" "${1%.t1}.t2"' _ {} \;

모든 크레딧은 https://askubuntu.com/questions/35922/how-do-i-change-extension-of-multiple-files-recursively-from-the-command-line로 보내드립니다.

사용 중인 버전이bash를 서포트하고 있습니다.globstar옵션(버전 4 이후):

shopt -s globstar
for f in **/*.t1; do
    mv "$f" "${f%.t1}.t2"
done 

bash에서는 이렇게 하겠습니다.

for i in $(ls *.t1); 
do
    mv "$i" "${i%.t1}.t2" 
done

EDIT : 제 실수 : 재귀적이지 않습니다.파일명을 재귀적으로 변경하는 방법은 다음과 같습니다.

for i in $(find `pwd` -name "*.t1"); 
do 
    mv "$i" "${i%.t1}.t2"
done

또는 명령어를 설치하고 다음 작업을 수행할 수 있습니다.

mmv '*.t1' '#1.t2'

여기서#1첫 번째 글로벌 파트입니다.**.t1.

또는 순수한 bash의 경우, 간단한 방법은 다음과 같습니다.

for f in *.t1; do
    mv "$f" "${f%.t1}.t2"
done

(예:for다음과 같은 외부 명령어를 사용하지 않고 파일을 나열할 수 있습니다.ls또는find)

HTH

이러한 솔루션 중 하나의 복사 붙여넣기가 느리지만, 이미 설치되어 있기 때문에 다음과 같이 했습니다.

fd --extension t1 --exec mv {} {.}.t2

부터fd의 맨 페이지, 명령어 실행 시(사용)--exec):

          The following placeholders are substituted by a
          path derived from the current search result:

          {}     path
          {/}    basename
          {//}   parent directory
          {.}    path without file extension
          {/.}   basename without file extension

언급URL : https://stackoverflow.com/questions/21985492/recursively-change-file-extensions-in-bash