Category Archives: Unix/Server Stuff

All Unix and server related stuff.

Custom themes for PuTTY

This post has sat in my draft folder for more than 5 years. I was doing some cleaning and found that I didn’t publish this post and since it is still relevant I thought of pushing the publish button.

Being a web developer, most of the time I would be staring at PuTTY. I got sick of the default bright colours of PuTTY and was searching for ways to customize the display of my PuTTY window.

After some research, I found pre-packaged custom themes for PuTTY created by Ilya Grigorik. These themes are available as .reg files. You can directly download and use them or if you want, you can further customize them for your needs.

Below is the screenshot of the different themes that are available.


Wow! now my eyes have something pleasant to stare at, most of the times 😉

Posted in Unix/Server Stuff, Web Programming | Tagged , , | Leave a comment

Automatically send unique errors (with count) from Apache error log as email

Sometime back I wrote about a simple awk script that I wrote which allowed me to find unique errors from Apache error log files.

After I wrote that script I found myself executing that script every day in the morning to figure out if there were any errors in my sites. After a couple of days I wrote another script to automatically parse the error log file and email me if there were any errors. As usual I thought of writing about it here so that it would be useful for someone else as well 🙂

Continue reading »

Posted in Python, Unix/Server Stuff | Tagged , , | 5 Comments

How to print unique errors(with count) from Apache error logs

After I moved to WordPress Multisite recently, I had to closely watch the Apache error log file for a couple of days to make sure that there weren’t any configuration mismatch.

Initially I used run the tail command to view the errors, but then I quickly realized that if there is a frequent error, then it becomes extremely difficult to find out other non frequent errors.


After some fiddling with the awk and sort commands, I came up with the following one liner that prints the unique errors with their count.


awk script is executed for each line in the error log file.

Field Separator

The -F directive is used to specify field separator. If you look at a single line in the apache error log file, you will notice that each field is enclosed by [ and ].

awk allows us to specify multiple field separators by enclosing them between []. That’s what we are specifying in -F[\[\]]

Filtering out lines that contain error

After splitting the fields, we need to filter out the lines that contain the term error in the fourth field. That’s what happens in the next part of the command. Basically the part $4 == "error"{print $7} prints the seventh field, which has the real error message if the fourth field is equal to the string “error”.

Sorting the error messages

The next step is to sort all the error messages. This is done by piping the output to the sort command.

Finding unique error messages and counting them

The next step is to find the unique error messages and then count them. This is done by piping the output to the uniq command. The -c flag does the counting.

Sorting the messages by frequency

The last step is to sort the messages again by frequency and print them. This is done by the last sort command. The -n flag sorts by numbers and the flag -r does the sorting by descending order.

Posted in Unix/Server Stuff | Tagged , , , , , | Leave a comment

How To Unfreeze Frozen SSH Tabs In Mac Terminal App

I use the Mac Terminal App a lot to ssh into remote servers. It all works great except for a single annoyance. If I leave the ssh tab open without any activity for sometime or put my Mac to sleep, then the tab gets frozen and becomes unresponsive. It takes a couple of minutes for it to log out of the server and become responsive again, so that I can reuse the tab again.

Whenever this happens, I have to close the unresponsive tab and open a new one, or wait for 5-10 minutes. I did some research about it and found that Mac Terminal App provides an escape command that can be used to forcefully log out of the remote server. All you need to do is to type the following command in the unresponsive tab.


(Tilda followed by a dot)

I tried it and it works like a charm. It immediately logs you out of the frozen ssh tab and you can reuse your tab again without waiting for it to become responsive again.

Hope this small trick is useful for others as well 🙂

Posted in Unix/Server Stuff | Tagged , , , | 4 Comments

Rollback a commit in git, while maintaining history

I use git for pretty much all my projects these days. When ever I do a commit, I make sure that it is atomic and has a proper commit message. One of the main reason why I keep the commits atomic is that, I can rollback the commit if needed.

Recently, I faced a situation, where in one of my projects, the feature I added a couple of commits back wasn’t working properly and I decided to completely get rid of it. Since I follow semantic versioning, I wanted to make sure that I maintain the different release of my project properly.

After a bit of digging up, I found the exact command in git to do that. Since the command was not that intuitive I thought of documenting it here, so that I know where to lookup when I face the situation again 😉


Let me explain problem properly. Let’s assume that I have the following list of commits in my history.

A -> B -> C -> D -> E

The head is at E now. The feature that I was talking about was introduced in commit C. Now I want to rollback the changes that I did in commit C, after E, but still maintain the history. In short I want the commits to look like

A -> B -> C -> D -> E -> C'

where C' is the opposite of C


I could have manually removed that changes I did in commit C. But being a fan of fancy one-liners, I was looking for a solution and found that there is a command in git which can do this for you.

All you need to do is to execute the following command.

git revert 'SHA_of_C'

It’s really fascinating to see, how the developers of git have thought of these different cases 🙂

Posted in Unix/Server Stuff | Tagged , | Leave a comment

Sum up all values of a column in a text file

Recently, I had to sum up all integers of a column in a text file (similar to how you do in excel). After some digging up, I came up with a awk one liner to do it.

Following my tradition of documenting one liners, I am going to document this one as well 🙂

Input and output data

Here is the super simplified version of input data that I was using.

I wanted to find the sum of all values present in the 3rd column. So in the case, the output that I was expecting was 145


Here is the awk one liner, which does this.


awk script is executed for each line and the first part of the command creates a variable s that stores the sum of all values in the 3rd column.

When the end of file is reached, the second part of the command is executed, which just prints the value of the variable.

Field separator

If the columns are separated by a comma or by any other non whitespace character, then you have to just specify it by adding FS=',' to the above command.

The more I dig deeper into awk, the more I like it and it is really fascinating to see how much you can do with this tool.

I learned a lot about awk and hopefully this teaches something to you as well 😉

Posted in Unix/Server Stuff | Tagged , , | 7 Comments

Count the number of empty lines in a file using grep

Recently I had a need to count the number of empty lines in a text file. After some digging into the man pages of grep I was able to come up with a one liner which was able to do it.

Following my tradition of documenting one liners, I am going to document this one as well 🙂


By empty line, I mean any line which either has no characters or has only whitespace (space, tab) characters.


For the impatient in you, here is the actual command.


  • -P '\S' – This selects all lines that have a non whitespace character
  • -c – Print the count of matching lines
  • -v – Select only the non-matching lines

So, we are first matching all lines that have a non whitespace character and then use -v option to ignore them and then -c option to print the count instead of the actual line.

If we wanted the count of all non-empty lines, then we just have to remove the -v option from the above command.

Hope this is helpful. Happy Grep’ing 😉

Posted in Unix/Server Stuff | Tagged , , | 7 Comments

Remove duplicate lines based on a field

Recently while working on formatting some data files for further processing, I had to remove duplicate lines from the file based on a particular field. After trying out cut and grep commands, I was finally able to solve it with a very concise awk command/script.

The command was so concise but still was packed with so much information and it helped me to learn more about the awk scripting language. I thought of writing about it here so that it is useful for others and also I know where to search for it, when I needed it 🙂

Feel free to use it in whatever way you want, if it solves your problem as well.

Input and output data

Let me first explain the input data I had and the output that I was expecting.

Consider a file which has the following lines. Each line has four fields.

Now assume that we want to remove duplicate lines by comparing only the second field. We want the output to look like this.


Get ready for the surprise. The actual command is just this.


awk script execution and printing

awk script is executed for each line and if the result is true then the line is printed. If the result is false then the line is not printed.

Associative arrays

The awk language supports associate arrays, similar to the ones found in PHP. The script x[$2]++ fills up an associate array. The key used here is $2 which refers to the second field and x is the variable name. You can use any name for it.

The array is populated for every line. This is how the array would look like after each line.

Conditional evaluation

The ! operator results in a boolean evaluation which determines whether a particular line should be passed on to the output (printed) or not.

When the field is not present in the array, then it results in a zero value which is false. The ! (not) operator evaluates it to non-zero, which results in a true value and the line is passed on to the output (printed). When a duplicate is found, the array returns a non-zero count, which is true, but the ! converts it to false and that line is not passed on to the output.

The expanded version of the above command would be

But what is the fun in using the expanded version 😉

Field separator

In the input file that I had, the fields were separated by whitespace, so I didn’t have to specify the field separators. But if you are using a non-whitespace field separators, then you can specify it by adding FS="," to the above command.

This one-liner actually thought me that awk supports a full programming language that can be used to create scripts and also increased my understanding of the way awk command works. Hopefully this teaches something for you as well 🙂

I know that this is already a concise version, but if you think that this can be improved, then do let me know.

Posted in Unix/Server Stuff | Tagged , , | 5 Comments

Parse Apache error log and list down all missing images

Recently I had to parse Apache error log and find out all images that are missing.

After referring to a couple of man pages, I came up with this one liner. I am sure I will need it again, so thought of noting it here so that I know where to look when I need it again 😉

Feel free to use it in whatever way you want, if it solves your problem as well.


This assumes that each line in your apache error log looks like this.


Filter all 404 lines from the log file

The first step is to filter all lines that that contain the “File does not exist” text. This is done by using sed.

  • By default, sed prints out all lines. This is prevented by the -n.
  • The second option is the regular expression followed by the p flag. This option prints out all lines which match the text.
  • The third option is the name of the error log file.

Extract the last column of matching lines

The next step is to retrieve the file name from the matching lines. This is done by using awk.

  • By default awk uses space as the delimiter and splits the lines into different columns. If you look at each line, we want the last column.
  • NF is a special variable which points to the last column.
  • print $NF prints the last column

Filter only images

The next step is to filter out only the images. This is done again by using sed.

  • I use -n again to prevent sed from printing all lines.
  • The -r is added, so that we can use extended regular expression
  • The regular expression (jpg|jpeg|png|gif)$ filters out all images and p at the end prints out only lines that match

Sort and find uniques

The sort and uniq commands sort the list and find the unique lines.

Write to a file

The final output is written to a file by using the redirection > operator. If you want to append to a file then we may have to use >> operator.

More to come

It is really amazing like how you can combine these tools to do amazing things. I am planning to document other one liners which I end up creating to solve my problems. So stay tuned 🙂

Also if you think this can be improved, then do let me know as well.

Posted in Unix/Server Stuff | Tagged , , , | 5 Comments

Contributing to project hosted in Github

As most of you know, I host most of my projects which I have released as open source in Github. I am open for collaboration and generally accept most of the pull requests that people send me.

Recently I noted that not all people who want to contribute are proficient with git or Github. So here is a small guide to help people who are interested in contributing to projects hosted at Github.

Before we proceed just keep in mind, that this is not the only way to do it. But if you are just starting out using git or Github, then this is a good starting point.

Fork the project in Github

The first step is to fork the project at Github. Go to the project that you want to contribute to and then click the fork button near the top right corner.

Clone the forked project to your machine

When you fork the project, Github creates the forked project in your account. Once you forked the project, you need to clone it to your machine.

You can use the following command to do it.

git clone

Replace it with your actual username and project name

Create a new branch

The next step is to create a new branch. You can use the following command to do that.

git checkout -b branch_name

Commit your changes

After you have made the changes, you have to commit them to the new branch

git add file_name


git add -p file_name

Then do

git commit -m "Your commit message"

Remember to have a meaning full commit message. If possible follow this guide by Tim Pope on how to construct a good commit message.

Push the change to Github

Next you have to push the changes to Github. You can do it by using the following command.

git push -u origin branch_name

Send a pull request

Now go to Github and send a pull request, by clicking the pull request button in Github. The owner of the repo will be notified and he may choose to accept or reject the request.

Keeping your repo upto date

Once the pull request is accepted, you can merge the changes back to your repo by using the following commands.

git remote add upstream git://

git checkout master

git pull --rebase upstream master

git push origin master

Posted in Unix/Server Stuff | Tagged , , | 5 Comments