-3

we have linux rhel machine 7.2 with the following folders under /kafka/topics

drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-1
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-10
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-100
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-103
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-106
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-109
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-112
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-115
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-118
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-121
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-124
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-127
drwxr-xr-x 2 root  kafka    141 May 11 17:19 bgol.ase.fgt.dad3-13
.
.
.

we want to delete all this topics with names - bgol.ase.fgt.dad3 by using variable as ,

topic=bgol.ase.fgt.dad3
find /kafka/topics  -name "$topic*" -exec rm -rf "{}" \;

but I feel the above approach isn't so elegant , since we want to set this approach in bash script

I will appreciate to get other suggestion to delete the folders , when using variable as $topic

1

if you use find , it's return full path :

$ find /kafka/topics 
/kafka/topics/bgol.ase.fgt.dad3-1
/kafka/topics/bgol.ase.fgt.dad3-100
...

so , you should be add * after var and befor this var like that *$topic* , finallment

find /kafka/topics  -name "*$topic*" -exec rm -rf "{}" \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.