[英]How can I delete all jpg files except “1.jpg” in shell
In my Raspberry Pi, there are a lot of jpg
files. 在我的Raspberry Pi中,有很多jpg
文件。 I want to delete all jpg
files except 1.jpg
. 我想删除1.jpg
以外的所有jpg
文件。 How can I do that in shell script? 我该如何在shell脚本中做到这一点?
You can use find , eg 您可以使用find ,例如
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): 您可以使用扩展的glob模式进行求反(需要Bash):
$ shopt -s extglob
$ ls
1.jpg 2.jpg name.jpg
$ ls !(1).jpg
2.jpg name.jpg
$ rm !(1).jpg
$ ls
1.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.