find command:https://www.howtouselinux.com/post/find-file-by-name-in-linux # Use find command, find all *.txt files but ignore foo.txt find . -type f \( -iname "*.txt" ! -iname "foo.txt" \) # To delete file add -delete or -exec your-delete-command-here option. find . -type f \( -iname "*.txt" ! -iname "foo.txt" \) -delete # To select folder or dirs use -type d, in this example, find all folders and ignore foo and bar folder : find . -type d \( ! -iname "foo" ! -iname "bar" \) # To delete folders except foo and bar find . -type d \( ! -iname "foo" ! -iname "bar" \) -execdir rm -rfv {} + # Be careful while deleting files and dirs.