简体   繁体   中英

How can I delete all jpg files except “1.jpg” in shell

In my Raspberry Pi, there are a lot of jpg files. I want to delete all jpg files except 1.jpg . How can I do that in shell script?

You can use find , eg

find . -name \*.jpg \! -name 1.jpg -exec rm {} \;

Be very careful though, you can easily delete a lot of files unintentionally if you get this wrong. Do a "dry run" first to check which files will be deleted, eg

find . -name \*.jpg \! -name 1.jpg -exec echo "rm {}" \;

You can use an extended glob pattern for negation (requires Bash):

$ shopt -s extglob
$ ls
1.jpg  2.jpg  name.jpg
$ ls !(1).jpg
2.jpg  name.jpg
$ rm !(1).jpg
$ ls
1.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM