What are the ways to delete all files except some files in Shell
This article introduces the relevant knowledge of "what are the ways to delete all files except some files in Shell". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
If you want to delete all files except * .iso and * .zip in the ~ / Downloads directory, you can do the following in bash:
Method 1: pattern matching method
The code is as follows:
Shopt-s extglob # confirm that the extglob option is enabled
Cd ~ / Downloads
Rm-v! (* .iso | * .zip)
! (pattern list) is used to match files other than pattern list
Method 2: set variable method
In bash, GLOBIGNORE can be used to set pattern matching files to ignore, and multiple patterns are separated by:.
The code is as follows:
Cd ~ / Downloads
Export GLOBIGNORE=*.zip:*.iso
Rm-v *
Unset GLOBIGNORE
Methods 3:find search method
Friends who are familiar with find must know that find is extremely powerful, so you can also use it to solve this problem.
The code is as follows:
Cd ~ / Downloads
Find. -type f-not\ (- name'* .zip'- or-name'* .iso'\)-delete
However, I would like to remind you that the operation of rm is very dangerous, do not show off your skills, if you are not assured, then you can honestly delete one by one or in batches, which is much more convenient than finding it afterwards.
This is the end of the content of "what are the ways to delete all files except some files in Shell". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!