Skip to main content
Made my question more clear.
Source Link
Jack
  • 111
  • 1

If the folder structure is:

parentFolder
├── file1.rtf
├── file2.rtf
... #there are many more files.
├── moreFiles.rtf
├── subFolder1
└──├── subFolder2
├── subFolder3
... #there are many more subfolders.
└── moreSubFolders

How would I delete all subfolders except subFolder1, but not subFolder2 orwithout deleting any of of the files without specifying the file names? Specifying folder names is ok.

I know you could specify each folder and filedo not want to have to name each folder which should be deleted:.

rm -r !("file1.rtf"|"file2.rtf"|"subFolder1")

But I would preferalso do not want to have name each file which should not be deleted.

I want a command that only deletes folders, not files, and allows the user to exclude some folders from being deleted.

If the folder structure is:

parentFolder
├── file1.rtf
├── file2.rtf
├── moreFiles.rtf
├── subFolder1
└── subFolder2

How would I delete subFolder1, but not subFolder2 or any of the files without specifying the file names? Specifying folder names is ok.

I know you could specify each folder and file not to be deleted:

rm -r !("file1.rtf"|"file2.rtf"|"subFolder1")

But I would prefer not to have name each file.

If the folder structure is:

parentFolder
├── file1.rtf
├── file2.rtf
... #there are many more files.
├── moreFiles.rtf
├── subFolder1
├── subFolder2
├── subFolder3
... #there are many more subfolders.
└── moreSubFolders

How would I delete all subfolders except subFolder1 without deleting any of the files?

I do not want to have to name each folder which should be deleted.

I also do not want to have name each file which should not be deleted.

I want a command that only deletes folders, not files, and allows the user to exclude some folders from being deleted.

Source Link
Jack
  • 111
  • 1

If a folder contains sub-folders and files, is there a Terminal command to remove some sub-folders while not removing any files?

If the folder structure is:

parentFolder
├── file1.rtf
├── file2.rtf
├── moreFiles.rtf
├── subFolder1
└── subFolder2

How would I delete subFolder1, but not subFolder2 or any of the files without specifying the file names? Specifying folder names is ok.

I know you could specify each folder and file not to be deleted:

rm -r !("file1.rtf"|"file2.rtf"|"subFolder1")

But I would prefer not to have name each file.