Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Find + delete folders with extension when using variable

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
by

1 Answer

Kajalsi45d
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 "{}" \;

Login / Signup to Answer the Question.