I need a bash script to do this: I have folder that contain html files and subfolders.
now files name as [login to view URL], [login to view URL] etc.
i want to replace the original file name with the title tag of the file ( instead of the space between the words i would like to add "_". then replace all links in all folder that contain the original name with the new title name.
example of title - <title>admin/environment/php some text</title> change to <title>php_some_text</title>
then to replace all links in all files that contain the old links to the new file name that will be php_some_text.html.
all my needs have a solutions in my script but don't know how to replace the link all over to the new file name. my script:
#!/bin/bash
cd /var/www/clients/client1/web2/web/docs/31/en
find . -name "*.html" > [login to view URL]
while read -r filepath ;
do
# Considering the subfolder can have spaces,
# We will start by separating the filename
# And dirname
folderpath=`dirname "$filepath"`
filename=`basename "$filepath"`
# With GNU GREP
new_name=$(grep -oP '<title>\K.+?</title>' "$filepath" | sed 's#</title>##' | tr ' ' '_' )
#echo new_name=$(grep -oP '<title>\K.+?</title>' "$filepath" | sed 's#</title>##' | tr ' ' '_' )
# With BSD GREP (for who need the info)
# new_name=$(grep -o '<title>.*</title>' $filepath | sed -e 's/<title>\(.*\)<\/title>/\1/g' | sed -e 's/$filename/$new_name/g' | tr ' ' '_')
#if [[ $new_name = */* ]]; then echo "/${new_name##*}"; else echo "$new_name"; fi
mv -v "$filepath" "$folderpath/`basename $new_name`.html"
### command that i tried to solve the link url issue
#find /var/www/clients/client1/web2/web/docs -type f -exec sed -i 's/$filename/$new_name/g' {} \;
#grep -rl '$filename' /var/www/clients/client1/web2/web/docs/31/en | xargs sed -i 's/$filename/$new_name/g' *.html
#sed -i 's/$filename/$new_name/gi' *.html
done < [login to view URL]
Thank you
Hello!
I have experience with C programming and bash scripting.
I would try to solve the link replacement problem with bash scripting but I also consider using C if I fail to do it with bash scripts, if you also agree.
Please contact me so we can discuss more details.
I would be glad to help you with this script :).
Got 5 years of experience in linux field, working as Server Engineer and wrote tons of bash nagios alerts scripts to detect critical issues before it catches the client attention.