하나 이상의 파일 형식을 제외한 모든 유형의 파일 형식 제거 시도한 것

폴더의 모든 파일을 제거하는 명령을 찾으려고 노력했지만 파일 유형이 아닙니다. 그러나 나는 운이없는 것 같습니다. 내가 지금까지 시도한 것 :

set extended_glob
rm !(*.dmg)
# this returns zsh:number expected

rm ./^*.dmg
# this returns no matches found 

내가 사용하는 zsh 버전은입니다 zsh 5.0.2 (x86_64-apple-darwin13.0.1).



답변

extended_glob옵션은 zsh 자체 확장 glob 구문을 제공합니다 .

setopt extended_glob
rm -- ^*.dmg
rm -- ^*.(dmg|txt)

ksh globsksh_glob 를 얻 도록 옵션을 설정할 수 있습니다 . 음수 패턴이 단어의 마지막 항목 인 일반적인 경우 zsh는 괄호를 glob 한정자로 구문 분석 할 수 있습니다 (ksh 에뮬레이션 모드에서는이 작업을 수행하지 않음).

setopt ksh_glob
rm -- !(*.dmg|*.txt)
setopt no_bare_glob_qual
rm -- !(*.dmg)

답변

find쉘 대신 사용할 수 있습니다 .

find . -mindepth 1 -maxdepth 1 ! -name "*.dmg" -delete

보낸 사람 man find:

   ! expr True  if  expr  is false.  This character will also usually need
          protection from interpretation by the shell.
   -name pattern
          Base of file name (the path with the leading directories removed)
          matches shell pattern pattern.
   -delete
          Delete  files; true if removal succeeded.  If the removal failed,
          an error message is issued.  If -delete fails, find's exit status
          will be nonzero (when  it eventually exits).  Use of -delete
          automatically turns on the -depth option.

find어떤 이유로 든 사용할 수없는 경우 여기 zsh(또는 다른 껍질)로 사용하는 방법이 있습니다 . zshzsh이이 일을 간단한 방법은 아마도하지만 난 해요 때문에, bash사람이 내가 생각 해낸 것입니다 :

for file in *; do if [[ ! "$file" == *.dmg ]]; then rm $file; fi; done

답변

파일을 제거하는 또 다른 방법은 함께 find, xargs그리고 rm:

find . -mindepth 1 -maxdepth 1 ! -name '*.dmg' -print0 | xargs -0 rm