1.15.2012

Block unwanted advertisements with /etc/hosts file on Linux

I surf the web an awful lot, probably slightly more than your average 13 year old geek. I notice that a lot of sites load rather slowly mostly because your waiting on content from outside the specific domain. For example if you go to a website like thechive.com (one of my favorites) you will notice it takes quite a long time loading the ads.

Although I mentioned thechive.com I spend most of my time on the net looking for information, not entertainment. These ads really hinder my search speed!

So here is a quick way you can block all the ads. Not only will your surfing be faster but you will also save some bandwidth.

First off I would like to thank the fine folks at http://winhelp2002.mvps.org/ for doing all the leg work and collecting all the data necessary for this to work.

Now this is simple.
First, let's make a copy of your current hosts file. Some people still use this!

NOTE: Everything done below should be done as root, or with sudo access.

Here we will make a copy of your /etc/hosts file and save it in your home directory in a hidden file called .etchosts , but you can save it anywhere you like. Just be sure to change the script to make the location of your file.
cp /etc/hosts ~/.etchosts Now we will make the shell script.
run:
vi /root/update_hosts.sh
Fill the file with the following:
#!/bin/bash
cd /tmp
wget http://winhelp2002.mvps.org/hosts.txt
rm /etc/hosts
mv hosts.txt /etc/hosts
cat ~/.etchosts >> /etc/hosts
Now we have to make sure the script is executable:
chmod +x update_hosts.sh So just a little explanation of what the script does, it's simple.
1) cd /tmp (Changes to a temporary working directory)
2) wget http://winhelp2002.mvps.org/hosts.txt (Gets the hosts.txt file from mvps.org and saves it.
3) rm /etc/hosts (Deletes the current /etc/hosts file)
4) mv hosts.txt /etc/hosts (Moves the new downloaded file to the /etc/hosts file)
5) cat ~/.etchosts >> /etc/hosts (Moves your old host entries back into the new file)
Now all you have to do is run the script and your advertisements will disappear from your browser.

You can use a cron job to update the hosts file automatically every night. The good folks at mvps.org update the file fairly regularly, not everyday, but a couple times a month.

Let's add it to roots cron.
run:
crontab -e
Then enter the following in the file.
59 23 * * * /root/update_hosts.sh
Remember, if you need to add something to your hosts file you now want to add it to ~/.etchosts and then run the script to update /etc/hosts.



REFERENCE: http://winhelp2002.mvps.org/hosts.htm

1.05.2012

Extract a single file from a TAR archive

It is possible to extract a single file from a tarball archive using the tar command. For this example we will use my Pink Floyd mp3 collection. I used tar to archive my songs so I can move them to my new machine. Right now I only want to extract the song "Learning to Fly" for the archive. First off we will need to know the exact name of the file. We can list the files in a tar archive using the following command. tar -tvf filename.tar Here is an example: $ tar -tvf Floyd.tar
-rw-rw-r-- savona/savona 432516 2012-01-05 14:49 Echoes.mp3
-rw-rw-r-- savona/savona 837292 2012-01-05 14:49 Goodbye_Blue_Sky.mp3
-rw-rw-r-- savona/savona 748479 2012-01-05 14:49 Green_Is_The_Colour.mp3
-rw-rw-r-- savona/savona 3455103 2012-01-05 14:49 Learning_To_Fly.mp3
-rw-rw-r-- savona/savona 234426 2012-01-05 14:49 Obscured_By_Clouds.mp3
Now we can see the filename for Learning to Fly is "Learning_To_Fly.mp3". Let's extract that one song with the following command: tar xvf filename.tar file_to_extract Example: $ tar xvf Floyd.tar Learning_To_Fly.mp3
Learning_To_Fly.mp3
To learn more useful tar commands please see the tar man page: man tar

12.21.2011

Grep All Email Addresses from a Text File Using Regular Expressions

Took me a while to construct this regular expression, thought it might be useful to someone else. grep -E -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" filename.txt NOTE: The above command should be on one line.
(You can also use egrep instead of grep with the -E switch)
egrep -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" filename.txt
From grep man pages:
-E = Interpret PATTERN as an extended regular expression.
-o = Show only the part of a matching line that matches PATTERN.

More info:
GREP MAN PAGE: http://ss64.com/bash/grep.html
GREP REGEX TUTORIAL: http://www.regular-expressions.info/grep.html