Reputation: 19
I have folder structure like below
├── tmp
└── Dir1
|__Dir2
|__Dir3
|__Dir4
My current working directory is different which is /home/users/aaa from here I want to delete everything inside Dir1 except directory Dir2
I have one working solution which is
abc=Dir2
shopt -s extglob
`rm -r /tmp/Dir1/!($abc)/`
But somehow this solution is not acceptable by team
do you have any other solution for deleting directories except one .
below is my actual code
current_date=`date +"%Y_%m_%d"
#remove all containt of folder /tmp/metdata if they are older
shopt -s extglob
`rm -r /tmp/metdata/!($current_date)/`
above solution is working but not acceptable by team:(
removing everything inside "/tmp/metdata/" except folder have name having value current_date
actually the issue is if there in no folder inside /tmp/metdata/ it shows below message each time ,I don't want to display this message
rm: cannot remove ‘/tmp/metdata/!(2020_05_15)/’: No such file or directory
can we check any condition before this
any help appreciated
please help
Upvotes: 0
Views: 508
Reputation: 4178
find /tmp/Dir1 -maxdepth 1 -mindepth 1 ! -name Dir2 -exec rm -rf {} +
current_date=$(date +"%Y_%m_%d")
find /tmp/Dir1 -maxdepth 1 -mindepth 1 ! -name "${current_date}" -exec rm -rf {} +
Upvotes: 1