Tag Archives: Linux - Page 5

How to create and use the patch command

This very simple HOWTO will show how to use the *nix patch command

Create a patch from two files

diff -u myfile mynewfile > myfile.patch

This will give you a patch (myfile.patch) that, when applied to myfile, will make it look exactly like mynewfile. I use the -u option because I like the so called unified diff format

Apply a patch to one file

patch myfile myfile.patch

This applies the myfile.patch on the file myfile (which makes it look exactly like mynewfile from above)

Create a patch from two directories

diff -u -r dir1/ dir2/ > myfiles.patch

Creating a patch file for multiple files in different directories requires the -r option which stands för recursive i.e. diff will search through all subdirectories

Apply a patch to a directory

 patch -p0 -i myfiles.patch 

When patching multiple files in different directories we need to tell the patch command to not strip paths from patch file (which is the default behavior). This is done through the -p0 option. We also need to specify the input file with the -i option <patchfile>
NOTE! when applying a patch you should always stand one level above the topmost directory of the structure to be patched

Setup shared keys for passwordless ssh login

In this post I will show how I setup passwordless logins to my servers. This is especially useful when you want to do logins in a script e.g. a rsync script for backup to another computer through ssh

First we have to start with creating a private/public key pair on the computer that we will do the login from

ssh-keygen -t rsa -b 1024

I use default options here. NOTE: Do NOT set a passphrase. If you do then the key pair expects this passphrase every time you use it which means that you will not be able to make passwordless logins with this key pair. Just press Enter twice and the key pair will be created without a passphrase.

After a while the key pair is created and you should have a id_rsa and id_rsa.pub in your home .ssh directory

Now we need to get your public key (id_rsa.pub) to the server we want to login to

cat .ssh/id_rsa.pub | ssh <servername> "cat >> ~/.ssh/authorized_keys"

That is it! Now you should be able to login to the server without using a password. Try it with:

ssh <servername>

No password should now be asked for

Tested on Debian 5.0 (arm/pc) and OS X 10.6.8

Advanced cool Linux commands

I will here save strange combinations of commands that has helped me in my daily work

Get all unique dates in a file where the dates apear in the second column (but not on all rows)

awk -F " " '{print $2}' <filename> | egrep "^[0-9]{4}" | sort | uniq

With awk I select the second ($2) column in the file. Columns are separated with space (” “) .
Egrep selects all rows that starts with 4 digits like in “2011-07-07”
The sort command sorts all rows – needed for the uniq command
The uniq command removes all duplicated rows

Get all unique rows that match regexp “<xml-tag>.*</xml-tag>”

 egrep "<xml-tag>.*</xml-tag>" /path/to/file | sort | uniq 

With egrep I get all rows that matches the regular expression
The sort command sorts all rows returned from egreg which is needed for the uniq command
The uniq command removes all duplicated rows

Get a list of occurrences of unique rows in a gziped textfile based on date (in second column) of rows containing a search string

zgrep search_string filename.gz | awk -F " " '{print $2}' | sort | uniq -c   

This will give you a list of dates together with the sum of occurrences of search_string like this:

    909 2011-07-01
   1608 2011-07-02
   1604 2011-07-03
   2775 2011-07-04
   2765 2011-07-05
   1757 2011-07-06
   3716 2011-07-07
   2785 2011-07-08
   1711 2011-07-09
   1655 2011-07-10

With zgrep we grep in a gziped file without unzipping it first
With awk we select the second column (in this case a YYYY-MM-DD formated date) on the row
The sort is only needed if the dates do not come in order
The uniq -c gives us the list of occurrences of the uniq dates (grouped together to one row per unique date)

Sum up integrer values in a specific column in a file

awk -F " " '{tot+=$1} END {print tot}' /path/to/the/numbers

Here the values to sum up are in the first column ($1) in the file. The -F ” “ option tells awk to consider a singel space ” “ to be the column separator

Get min/max integer from a file with integers (one per row)

awk -F " " 'value=="" || $5 < value {value=$5} END {print value}' /path/to/file

This will give you the min value of the numbers in the first column in the file. The -F " " option tells awk to consider a singel space " " to be the column separator. To look for the max value just change the <

Create pretty print copies of XML-onliners using xmllint

for f in * ; do xmllint.exe --format "$f" --output "prettyprint/${f%}.df" ; done

This will run all files in current directory through xmllint with --format option and place them as new files in a folder called prettyprint