Delete all directories named “XXX” from sub-directories from root directory in Windows or Unix

StackOverflow https://stackoverflow.com/questions/10754380

  •  11-06-2021
  •  | 
  •  

Question

I have many directories with some sub-directories that need to be deleted. Is there a way to deltree/rmdir so that all directories titled "TAB", "Tab_old" and files in them are deleted.

Directory structure is like

root>townx>TAB
root>towny>
root>towny>TAB
root>towny>zone1>
root>towny>zone1>Tab

etc... so all "TAB" directories should be deleted.

===== edmastermind29 suggested process output ====

$ find / -name "TAB" -type d -exec rm -rf {} \;

atgisuser@ATGISLAPTOP02 /c/scratch/Test_Lidar
$ ls
Ath_test.csv  LAS               Success_LOG.txt  asc
Contours      Orthophotomosaic  XYZ              schema.ini


atgisuser@ATGISLAPTOP02 /c/scratch/Test_Lidar
$ cd contours

atgisuser@ATGISLAPTOP02 /c/scratch/Test_Lidar/contours
$ ls
Atherton  TAB

atgisuser@ATGISLAPTOP02 /c/scratch/Test_Lidar/contours
$ 

The "TAB" directory above should be deleted...

Was it helpful?

Solution

Here is a Windows CMD solution

for /f "delims=" %F in ('dir /b /s /ad x:\rootFolder ^| findstr /le "\TAB \Tab_old"') do 2>nul rd /s /q "%F"

If used in a batch script, then %F must change to %%F

OTHER TIPS

find / -name "XXX" -type d -exec rm -rf {} \;

/ searches the entire file system. If you want to search just the root folder, then you would use /root

Usage of -name is case sensitive. However, -iname ignores case sensitivity.

In plain english, the above command states: Search the entire file system for "XXX", a directory. Upon finding "XXX", recursively remove the contents within the "XXX" directory forcefully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top