*NIX Tricks

[unix] Display the number of processors in the command line

Posted in linux, osx, unix by kousik on January 16, 2010

The information about the CPU  is stored in /proc/cpuinfo. The following command will show you the number of processors by reading that file:

$ grep processor /proc/cpuinfo | wc -l

A simple
$ grep -c processor /proc/cpuinfo
should also be enough!

Tagged with: , , , , , , , , ,

[unix] Compare the contents of two directories in the command line

Posted in cli, unix by kousik on January 11, 2010

The first thing that comes to mind when you try to do any comparison, e.g. between two files or directories, is diff. Here is how it is used to compare two directories, dir-1 and dir-2:
$ diff /path_to_dir-1/ /path_to_dir-2/

But if the files are very big in size, diff may take quite a while.

Another useful utility is comm which is relatively fast for comparison of two files. It prints out the unique lines of file-1 in column 1, that of file-2 in column 2 and the common lines in column 3. The column 1, 2 and 3 may be suppressed by using the flags -1, -2 and -3, respectively. So, for example, if we want to see which files are common in dir-1 and dir-2, we may use comm with the “-1” and “-2” flags on the output of the ls command on the directories:
$ comm -1 -2 <(ls /path_to_dir-1/) <(ls /path_to_dir-2/)

Note: Of course, you can do a diff (or better vimdiff) of the outputs of ls as above, but comm still wins over diff/vimdiff because of the option to selectively output common or unique lines. Also notice that these forms are different from the diff mentioned at the top which does the comparison byte-by-byte.

Tagged with: , , , , , , ,

[osx] Eject a volume ‘unwilling’ to be ejected!

Posted in linux, osx, unix by kousik on October 31, 2009

I sometime have problem finding which persistent process is not letting me eject a volume (CD/DVD/USB drive). The error message is not very helpful at all:

Try quitting applications

OK, but which one(s)? I often have hard time figuring that out using htop/ top or a combination of ps and grep.

But actually this is what I want:

$ lsof | grep -i Volume_Name

where Volume_Name is the name of the volume (or an ‘identifying’ part of the name) I want to eject. This gives me the name(s) and process ID(s) of the run-away process(es) as well as the path to the file(s) in use on the volume, Volume_Name.

Now that I know the names I should first try to save the documents and quit the applications involved in the normal way. However, if a particular application does not oblige, I can always kill it (with the risk of potential data loss!) using its process ID (say, 123):

$ kill -9 123

After that, the ejection of the volume should not be difficult at all.

Needless to say, being a Unix utility, lsof (=“list open files”) may be used on other *nix-based systems as well.

[cli] Find out the public IP in the command line

Posted in cli by kousik on October 30, 2009

You may find out your IP using commands like ifconfig, but if your computer is behind a firewall, that is not the IP that the world sees. Here’s a quick how-to to find out what your external IP is.

First, let’s create an alias: (link)

alias getip="wget -q -O - http://whatismyip.com/automation/n09230945.asp"

Next time when you need the public IP of your computer, just type getip in the command line and hit enter.

The above command gets the IP from http://whatismyip.com/automation/n09230945.asp (just copy and paste this URL to your location bar and hit enter, and see what it does!). The “-q” is to suppress verbose information (quiet mode) and “-O -” causes the output to be written to STDOUT.

You may use dyndns.org too to find out the IP, but in this case you may need some trimming: substitute the text within the code above by the following:


wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1

A few words about various flags above:
The output of the first command (everything before the first pipe) is

<code><html><head><title>Current IP Check</title></head><body>Current IP Address: xxx.xxx.xx.xx</body></html>

(the actual IP address is masked by x).
The “-o /dev/null“ part redirects the STDERR of wget to /dev/null. The “-d :” option in first “cut” tells it to use colon (:) to be used as the delimiter and “-f 2” causes it to print second of the delimited fields. Similarly, the flags of the second “cut” cause it to use “<” as the delimiter for the piped output from the first “cut” and choose the first of the delimited fields.

UPDATE: Another one using curl [link]. You may again change the quoted text within the alias above by the following:


curl --connect-timeout 3 http://www.whatismyip.org/

[latex] Crop, resize and rotate figures in LaTeX

Posted in latex by kousik on October 26, 2009

First of all, make sure to include the graphicx package in the preamble of your LaTeX source file. The following command exemplifies how to crop, resize and rotate figures in LaTeX.

\begin{figure}[h]
\centering
\includegraphics[trim=1cm 2cm 3cm 4cm, clip=true, totalheight=0.5\textheight, angle=90]{figure}
\caption{The caption goes here}
\end{figure}

Needless to say, the above goes in between begin{document} and end{document} statements.

Let’s explain the various parts in the argument of \includegraphics command:

  • Cropping:trim=1cm 2cm 3cm 4cm” trims (crops) from left, bottom, right and top by 1, 2, 3 and 4cm respectively. It must be accompanied by “clip=true”.
  • Resizing: totalheight=0.5\textheight” will force the figure to occupy 50% of the page length-wise keeping the aspect ratio constant.
  • Rotating:angle=90” rotates the figure by 90 degrees.

Reference: For more options, refer to this.

Follow

Get every new post delivered to your Inbox.