简体   繁体   English

如何删除外壳中除“ 1.jpg”以外的所有jpg文件

[英]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.

相关问题 如何使用 Linux 命令将“1.jpg”等文件夹的所有文件重命名为“1 hello.jpg”? - How can I rename all file of a folder like '1.jpg' to '1 hello.jpg' with Linux command? 是否可以将目录中的所有文件重命名为0.jpg,1.jpg,2.jpg等? - Is it possible rename all files in a directory to 0.jpg, 1.jpg, 2.jpg, etc? 如何使用shell脚本递归抓取所有jpg文件 - how to grab all jpg files recursively using shell scripting 如何从Linux中的shell中删除以._开头的所有文件? - How can I delete all files starting with ._ from the shell in Linux? Linux:在csv中递归“ ls”所有jpg文件 - Linux: 'ls' all jpg files recursively in csv 递归重命名所有子目录中的 .jpg 文件 - Recursively rename .jpg files in all subdirectories 我想通过 shell 删除所有文件,除了少数文件,但会导致语法错误:“(”意外 - i want to delete all files via shell except few as but results in Syntax error: "(" unexpected 如何在Linux控制台中删除.jpg文件 - How to delete .jpg file in linux console 如何在linux中重命名包含破折号的所有子目录中的所有.jpg文件? - How to rename all .jpg files in all subdirs which contains dash in linux? 如何使用linux命令将jpg文件转换为png文件? + 难度 = 子文件夹 - How to convert jpg files into png files with linux command? + Difficulty = Subfolders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM