웹사이트 검색

이름 바꾸기 – Linux에서 여러 파일의 이름을 바꾸는 명령줄 도구


우리는 Linux에서 단일 파일의 이름을 바꾸기 위해 종종 "mv" 명령을 사용합니다. 그러나 여러 파일 또는 그룹의 이름을 빠르게 바꾸면 터미널에서 작업이 매우 어려워집니다.

Linux에는 이름 바꾸기라는 매우 강력한 도구가 내장되어 있습니다. 이름 바꾸기 명령은 여러 파일 또는 파일 그룹의 이름을 바꾸고, 파일 이름을 소문자로 바꾸고, 파일 이름을 대문자로 바꾸고, Perl 표현식을 사용하여 파일을 덮어쓰는 데 사용됩니다.

"이름 바꾸기" 명령은 Perl 스크립트의 일부이며 많은 Linux 배포판에서 "/usr/bin/" 아래에 있습니다. "which" 명령을 실행하여 이름 바꾸기 명령의 위치를 찾을 수 있습니다.

which rename
/usr/bin/rename
이름 바꾸기 명령의 기본 구문
rename 's/old-name/new-name/' files

이름 바꾸기 명령에는 이름 바꾸기 명령이 실제 작업을 수행하도록 안내하는 필수 perl 표현식과 함께 몇 가지 선택적 인수가 함께 제공됩니다.

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
  1. -v: 성공적으로 이름이 변경된 파일의 이름을 인쇄합니다.
  2. -n: 이름이 변경된 파일을 표시합니다.
  3. -f: 기존 파일을 강제로 덮어씁니다.
  4. perlexpr: Perl 표현식.

이 유틸리티를 더 잘 이해하기 위해 기사에서 이 명령의 몇 가지 실제 예를 논의했습니다.

1. 기본 이름 바꾸기 명령 예

".html" 확장자를 가진 여러 개의 파일이 있고 모든 ".html" 파일의 이름을 ".php"로 바꾸고 싶다고 가정해 보겠습니다. 한 번에. 예를 들어, 먼저 "ls -l"을 실행하여 ".html" 확장자를 가진 파일 목록을 확인합니다.

ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

이제 이 모든 파일의 확장자를 ".html"에서 ".php"로 변경하려고 합니다. 아래와 같이 perl 표현식과 함께 다음 "이름 바꾸기" 명령을 사용할 수 있습니다.

ravisaive@tecmint:~$ rename 's/\.html$/\.php/' *.html

참고: 위 명령에서는 두 개의 인수를 사용했습니다.

  1. 첫 번째 인수는 .html.php로 대체하는 perl 표현식입니다.
  2. 두 번째 인수는 이름 바꾸기 명령이 모든 파일을 *.php로 대체하도록 지시합니다.

프롬프트에서 ls -l을 실행하여 모든 파일의 이름이 ".php" 확장자로 바뀌었는지 확인해 보겠습니다.

ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.php
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.php
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.php
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.php
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.php
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.php
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.php

이제 위에서 모든 html 파일의 이름이 php로 변경된 것을 볼 수 있습니다.

2. 이름 바꾸기 명령을 실행하기 전에 변경 사항 확인

중요하거나 주요한 이름 변경 작업을 수행하는 동안 "-n" 인수와 함께 이름 바꾸기 명령을 실행하여 항상 변경 사항을 확인할 수 있습니다. "-n" 매개변수는 어떤 변경 사항이 발생하는지 정확하게 알려주지만 변경 사항이 실제로 수행되지는 않습니다. 다음은 아래 명령의 예입니다.

ravisaive@tecmint:~$ rename -n 's/\.php$/\.html/' *.php

cricket.php renamed as cricket.html
entertainment.php renamed as entertainment.html
health.php renamed as health.html
lifestyle.php renamed as lifestyle.html
news.php renamed as news.html
photos.php renamed as photos.html
sports.php renamed as sports.html

참고: 위 명령 출력은 변경 사항만 표시하지만 "-n" 스위치 없이 명령을 실행하지 않는 한 실제로 변경 사항은 적용되지 않습니다.

3. 이름 바꾸기 출력 인쇄

이름 바꾸기 명령이 변경 사항에 대한 정보를 표시하지 않는 것을 확인했습니다. 따라서 이름 바꾸기 명령의 세부 정보를 얻으려면(“-n” 옵션을 사용한 것처럼) 여기서는 “-v” 옵션을 사용하여 전체 내용을 인쇄합니다. rename 명령으로 성공적으로 수행된 모든 변경 사항에 대한 세부 정보입니다.

ravisaive@tecmint:~$ rename -v 's/\.php$/\.html/' *.php

cricket.php renamed as cricket.html
entertainment.php renamed as entertainment.html
health.php renamed as health.html
lifestyle.php renamed as lifestyle.html
news.php renamed as news.html
photos.php renamed as photos.html
sports.php renamed as sports.html

4. 모든 소문자를 대문자로 변환하고 Vise-Versa

소문자 이름이 있는 모든 파일의 이름을 대문자로 일괄 변경하려면. 예를 들어 다음 파일을 모두 소문자에서 로 변환하고 싶습니다. >대문자.

소문자에서 대문자로
ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

그냥 Perl 표현식과 함께 다음 명령을 사용하십시오.

ravisaive@tecmint:~$ rename 'y/a-z/A-Z/' *.html

위 명령을 실행한 후 "ls -l"을 실행하여 변경 사항을 확인할 수 있습니다.

ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 CRICKET.HTML
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 ENTERTAINMENT.HTML
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 HEALTH.HTML
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 LIFESTYLE.HTML
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 NEWS.HTML
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 PHOTOS.HTML
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 SPORTS.HTML

위 명령이 실제로 모든 소문자 파일 이름(.HTML 확장명 포함)을 대문자로 변경한 것을 볼 수 있습니다.

대문자에서 소문자로

마찬가지로 다음 명령을 사용하여 모든 대문자 문자를 소문자로 변환할 수도 있습니다.

ravisaive@tecmint:~$ rename 'y/A-Z/a-z/' *.HTML
ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

5. 파일 이름의 첫 글자를 대문자로 사용하세요

각 파일 이름의 첫 글자만 대문자로 사용하려면 다음 명령을 사용하십시오.


rename 's/\b(\w)/\U$1/g' *.ext

6. 기존 파일 덮어쓰기

기존 파일을 강제로 덮어쓰려면 아래와 같이 “-f” 옵션을 사용하세요.

ravisaive@tecmint:~$ rename -f 's/a/b/' *.html

이름 바꾸기 명령에 대해 자세히 알고 싶으면 터미널에 "man rename"을 입력하세요.

이름 바꾸기 명령은 명령줄에서 파일 이름을 여러 개 바꾸거나 일괄적으로 바꾸는 경우 매우 유용합니다. 파일 이름 바꾸기 측면에서 얼마나 유용한지 한 번 시도해 보시고 알려주세요.