Category: linux

There may be a time, in your life, when you need to compare two text files on a linux server, and all you have is a CLI (Command Line Interface).  How can this be done? Well, it’s a pretty easy task (when you know how to do it!)

All you need is the diff command, and this is how you use it:

diff [first-file] [second-file]

Is it that easy?!? The answer is: YES!

So, for example, let’s say we have two style sheets on our webserver, style1.css and style2.css, and we want to compare them. We will use the following command:

diff style1.css style2.css

and this is what we’ll get in return:

< color: #999;
> color: #666;

What is diff trying to tell us? That line 140 of our first file (style1.css) contains the text color: #999; while line 138 of the second file (style2.css) is different and contains color: #666;.
Did you notice the < and > characters at the beginning of the lines? They indicate which file they refer to: < for the first file, > for the second.

If you have the chance to widen the window of your terminal (at least 126 columns), you may like the -y option, that will display a two column output, with the two files side by side:

diff -y [first-file] [second-file]

Output example of diff with option -y

Output example with option -y

A screenshot of Ubuntu 9.04 with GNOME 2.26.

Screenshot of GNOME on Ubuntu - Image via Wikipedia

At times it may happen that your graphical session on ubuntu freezes and you find yourself unable to do anything else. As a MS Windows user, the first thing that comes to your mind is a reboot of your machine, isn’t it? Well, in ubuntu you might have some other options. Let’s see some of them.

Option 1
If your Ubuntu is prior to 10.04, you can try the ctrl+alt+backspace combination. (note that this will instantly kill your X-windows, with all the programs running within it, like the window manager, and probably everything else, and it won’t ask you for any confirmation)
If your Ubuntu is newer than 10.04, bear in mind that the keystroke ctrl+alt+backspace has been disabled and replaced with alt+print screen+k.

Option 2
Using the command line (tou can try and switch to another tty using the Alt+Fn keys like Alt+F1, Alt+F2, etc., or you can use ssh from another machine), kill your graphical interface using the following commands:

/etc/init.d/gdm restart

note: depending on what you chose as a session manager, you might have to use “/etc/init.d/xdm restart” or “/etc/init.d/kdm restart” or, if you have the 11.10 version, try

sudo service lightdm restart

Option 3
Always from a command line, use the following procedure:

1 – retrieve the PID of your X server

ps -e | grep X

2- kill it

(example: kill 856)

3- restart it

startx &


Leave a comment if you think I’ve missed some better procedures.

Recently one of my PuTTY sessions froze, and I decided to try and kill it using the command line, instead of simply closing the window.
This is the complete procedure:

  1. I opened another PuTTY session
  2. using the who command I found the name of the frozen TTY
    myuser@ubuntu:~$ w
    14:21:07 up 119 days, 12 min, 2 users, load average: 0.06, 0.05, 0.05
    user1 pts/0 192.168.x.x 13:02  1:36  1.81s 0.10s sshd: user1 [priv]
    user1 pts/2 192.168.x.x 14:21  0.00s 0.46s 0.00s w
  3. using ps and grep i found its PID
    ps -u root | grep -i pts/0
  4. finally, I killed the blocked session:
    kill -9

To backup a single mysql database, use the following command:

:~$ mysqldump -u my_user_name -p db_name > backup_of_my_db.sql

If compression is needed, the command changes like this:

:~$ mysqldump -u my_user_name -p db_name  | gzip -9 > backup_of_my_db.sql.gz

To extract the compressed .gz file, use the following gunzip command:

:~$ gunzip backup_of_my_db.sql.gz