Imagine you have a folder that has about 20 files that you need and like 100 files that you don’t. In this case, there is a very simple bash script that allows you to do that in a jiffy!
Here it is:
ls | grep -v "hobba.txt\|tito.sh\|mambo.sh\|.xlsx" | xargs rm
This command does the following (in order):
- lists all files in the folder with “ls”
- grep -v means grep everything except the following
- The list of files – or globs – that it’s going to ignore, you have to separate each by pipe preceded with a backslash. It ignores 3 files and 1 glob:
- hobba.txt
- tito.sh
- mambo.sh
- .xlsx (means anything with extension .xlsx)
- Finally it deletes them one by one through the xargs rm
Enjoy!
Sources: Many which I unfortunately forgot to take note of, but they are mostly from Stackoverflow