program tip

Linux의 Bash에서 한 번에 여러 파일을 삭제하는 방법은 무엇입니까?

radiobox 2020. 8. 26. 07:45
반응형

Linux의 Bash에서 한 번에 여러 파일을 삭제하는 방법은 무엇입니까?


Linux 서버 내에 다음 파일 목록이 있습니다.

abc.log.2012-03-14
abc.log.2012-03-27
abc.log.2012-03-28
abc.log.2012-03-29
abc.log.2012-03-30
abc.log.2012-04-02
abc.log.2012-04-04
abc.log.2012-04-05
abc.log.2012-04-09
abc.log.2012-04-10

rm -rf아래 명령을 사용하여 선택한 로그 파일을 하나씩 삭제했습니다 .

rm -rf abc.log.2012-03-14 
rm -rf abc.log.2012-03-27
rm -rf abc.log.2012-03-28

선택한 파일을 한 번에 삭제할 수있는 다른 방법이 있습니까 ??


Bash는 모든 종류의 와일드 카드 및 확장을 지원합니다.

정확한 케이스는 다음 과 같이 중괄호 확장 으로 처리됩니다 .

$ rm -rf abc.log.2012-03-{14,27,28}

위의 내용은 세 개의 인수가 모두 포함 된 단일 명령으로 확장되며 다음을 입력하는 것과 같습니다.

$ rm -rf abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28

이 확장은 rm로드 되기 전에 쉘에 의해 수행된다는 점에 유의하는 것이 중요합니다 .


*여러 파일을 일치 시키려면 와일드 카드 ( )를 사용하십시오 .

예를 들어, 아래 명령은 이름이 abc.log.2012-03-.

rm -f abc.log.2012-03-*

명령을 ls abc.log.2012-03-*실행하기 전에 삭제할 내용을 볼 수 있도록 파일을 나열하기 위해 실행 하는 것이 좋습니다 rm.

자세한 내용은 파일 이름 확장 에 대한 Bash 매뉴얼 페이지를 참조하십시오 .


이름이 특정 형식과 일치하는 모든 파일을 삭제하려는 경우 와일드 카드 (glob 패턴)가 가장 간단한 솔루션입니다. 몇 가지 예 :

$ rm -f abc.log.*             # Remove them all
$ rm -f abc.log.2012*         # Remove all logs from 2012
$ rm -f abc.log.2012-0[123]*  # Remove all files from the first quarter of 2012

정규식은 와일드 카드보다 강력합니다. 의 출력 greprm -f. 파일 이름의 일부와 함께 시작하는 경우 예를 들어, "abc.log"그리고 일부는 "ABC.log", grep할 수 있습니다 당신은 대소 문자를 구분 일치를 수행합니다

$ rm -f $(ls | grep -i '^abc\.log\.')

이 작업을 수행 할 때 ls | grep ...먼저 명령을 실행하고 원하는 출력을 생성하는지 확인합니다. 특히 다음을 사용하는 경우 rm -f:

$ ls | grep -i '^abc\.log\.'
(check that the list is correct)
$ rm -f $(!!)

여기서는 !!이전 명령으로 확장됩니다. 또는 위쪽 화살표 또는 Ctrl-P를 입력하고 이전 줄을 편집하여 rm -f명령 을 추가 할 수 있습니다 .

이것은 bash 셸을 사용하고 있다고 가정합니다. 일부 다른 쉘, 특히 csh 및 tcsh 및 일부 이전 sh 파생 쉘은 $(...)구문을 지원하지 않을 수 있습니다 . 동등한 백틱 구문을 사용할 수 있습니다.

$ rm -f `ls | grep -i '^abc\.log\.'`

$(...)구문은 읽기 쉽게, 그리고 당신이 정말로 야심이 있다면 그것은 중첩 될 수 있습니다.

마지막으로 삭제하려는 파일의 하위 집합을 정규식으로 쉽게 표현할 수없는 경우 자주 사용하는 트릭은 파일을 임시 텍스트 파일에 나열한 다음 편집하는 것입니다.

$ ls > list
$ vi list   # Use your favorite text editor

그런 다음 list파일을 수동으로 편집하고 제거 할 파일 만 남겨두고 다음을 수행 할 수 있습니다 .

$ rm -f $(<list)

또는

$ rm -f `cat list`

(이것은 파일 이름에 재미있는 문자, 특히 공백이 포함되어 있지 않다고 가정합니다.)

Or, when editing the list file, I can add rm -f to the beginning of each line and then:

$ . ./list

or

$ source ./list

Just use multiline selection in sublime to combine all of the files into a single line and add a space between each file name and then add rm at the beginning of the list. This is mostly useful when there isn't a pattern in the filenames you want to delete.

[$]> rm abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28 abc.log.2012-03-29 abc.log.2012-03-30 abc.log.2012-04-02 abc.log.2012-04-04 abc.log.2012-04-05 abc.log.2012-04-09 abc.log.2012-04-10

A wild card would work nicely for this, although to be safe it would be best to make the use of the wild card as minimal as possible, so something along the lines of this:

rm -rf abc.log.2012-*

Although from the looks of it, are those just single files? The recursive option should not be necessary if none of those items are directories, so best to not use that, just for safety.


I am not a linux guru, but I believe you want to pipe your list of output files to xargs rm -rf. I have used something like this in the past with good results. Test on a sample directory first!

EDIT - I might have misunderstood, based on the other answers that are appearing. If you can use wildcards, great. I assumed that your original list that you displayed was generated by a program to give you your "selection", so I thought piping to xargs would be the way to go.


if you want to delete all files that belong to a directory at once. For example; yor Directory name is "log" and "log" directory include abc.log.2012-03-14, abc.log.2012-03-15,..... ect files. You have to be above the log directory and

rm -rf /log/*

참고URL : https://stackoverflow.com/questions/10516384/how-to-delete-multiple-files-at-once-in-bash-on-linux

반응형