Delete all folders inside a folder except one with specific name
56
votes
7
answers
93960
views
I need to delete all folders inside a folder using a daily script. The folder for that day needs to be left.
Folder 'myfolder' has 3 sub folder: 'test1', 'test2' and 'test3'
I need to delete all except 'test2'.
I am trying to match exact name here:
find /home/myfolder -type d ! -name 'test2' | xargs rm -rf
OR
find /home/myfolder -type d ! -name 'test2' -delete
This command always tries to delete the main folder 'myfolder' also !
Is there a way to avoid this ?
Asked by Riju Mahna
(693 rep)
Feb 6, 2018, 08:59 PM
Last activity: Jul 25, 2024, 08:20 AM
Last activity: Jul 25, 2024, 08:20 AM