I need to write a script that will find files that contain a string in their name. If a file exists that is the same name but without that string, I want to remove the original file.
For example, if I am in a directory with the following files:
algomb
gomba
alb
algomba
alba
If the substring is gom
then I would consider algomb
, gomba
, and algomba
. Removing gom
from each of those names I would check for the existance of alb
, ba
, and alba
. Of those, alb
and alba
do exist, so I would remove algomb
and algomba
leaving just
alb
alba
gomba
in the directory when I'm done.
Here is what I tried:
#!/bin/bash
sz="gom"
talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`
for c in ossz; do
if [ ! -d ]; then
echo "This is a directory"
else
if [ -f ];
then
find .-type f -name "*$sz*" -exec basename {} \;
else
echo ${talal%%:*}${talal##*:}
fi
fi
done
So this is works. This echo ${talal%%:*}${talal##*:}
is give back the filename without "gom". But I can't compare these values with find . -type f -exec basename {} \;
results.
Given that I can find the substrings, how can I test for the files and remove the appropriate ones?