Below are instructions on how to remove a cononical link element from multiple files or any line of code from multiple files with one script. I have seen in more than one occasion where someone has a site with multiple files all containing the wrong code for the canonical link.
If you don’t know what a canonical lin tag is read about it here What is a canonical link tag?
Removing them from 100′s of files can be a tedious job. But not on a Mac. On mac you can run one script that will remove a line from multiple files.
The script will search for a line of code that has a common word in it. In this case we search for a line of code that has the word canonical in it. You have to be careful if you use this script for something else. Make sure the line you want to remove has something unique that isn’t anywhere else in your file.
It’s rare in the case for canonical to be anywhere else. Unless you are writing an article about canonical tags as I have here.
One you have the script you can run it and remove the line in just a few seconds from multiple HTML files.
First thing you want to do is create a file in your text editor called deletecanonical.csh with the following code.
#!/bin/bash
for file in *.html; do
mv $file $file.old
sed ‘d/canonical/d’ $file.old > $file
rm -f $file.old
done
Save the file and place in the HTML directory.
Open a terminal window on your Mac and run the command to change directories to where your HTML files are. Learn how to use change directory command here.
If HTML files are in a /documents/websites then run the command “cd /documents/websites”
The file you created for the script file should be in this directory along with all the HTML files in which you want to remove the canonical tag.
Next you need to run the following command to make your file executable.
chmod a+x deletecanonical.csh
Then just run the file in the terminal like this.
./deletecanonical.csh
Voila ….
All .html files that had a line of code with the word canonical have been removed. You can apply this to any line of code you want to delete. Just be very careful. You may want to back up your files just in case you make a mistake.
Pingback: What is a Canonical Like Element | CreateBetterWebsites.com