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

前端 未结 5 872
傲寒
傲寒 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:09

    Use the not modifier to remove file(s) or pattern(s) you don't want to delete, you can modify the 1 passed to -maxdepth to specify how many sub directories deep you want to delete files from

    find . -maxdepth 1 -not -name "*.txt" -exec rm -f {} \;
    

    You can also do:

    find  -maxdepth 1 \! -name "*.txt" -exec rm -f {} \;
    

提交回复
热议问题