In Linux terminal, how to delete all files in a directory except one or two

前端 未结 5 827
傲寒
傲寒 2021-02-05 14:39

In a Linux terminal, how to delete all files from a folder except one or two?

For example.

I have 100 image files in a directory and one

5条回答
  •  旧巷少年郎
    2021-02-05 15:12

    find supports a -delete option so you do not need to -exec. You can also pass multiple sets of -not -name somefile -not -name otherfile

    user@host$ ls
    1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt josh.pdf keepme
    
    user@host$ find . -maxdepth 1 -type f -not -name keepme -not -name 8.txt -delete
    
    user@host$ ls
    8.txt  keepme
    

提交回复
热议问题