10

How can I remove all in folder and exclude specific folders and files?

(As example with tar: tar --exclude="folder").

Edit: I can delete files and folders.

grogsy
  • 306
  • 2
  • 5
  • 10
  • What do you mean by `remove`? Delete? What is that you want to accomplish? – devnull Jun 19 '13 at 07:11
  • Yes - delete files. I use command rm. Sorry bad formulate question. – grogsy Jun 19 '13 at 07:13
  • Thanks, [exclude directory from find . command](http://stackoverflow.com/questions/4210042/exclude-directory-from-find-command) is answer. – grogsy Jun 19 '13 at 07:24
  • One of the best and short command I mostly use is this below. Exclude file or folder with !(FileName, DirectoryName or Wildcard with prefix or suffix) then add -rf to recursively delete directory and files. Example: rm !(*.txt) -rf – Mahesh K. Jun 27 '20 at 13:47

1 Answers1

17

NOTE: Be careful running the following commands.

find . -type 'f' | grep -v "NameToExclude" | xargs rm 
find . -type 'd' | grep -v "NameToExclude" | xargs rmdir
rein
  • 32,967
  • 23
  • 82
  • 106