Monday, April 16, 2012

Unix commands for Windows Users

I am not a great fan of Unix CUI and I prefer X-Windows to work with. I already blogged about enabling VNC Server for Linux.
However if you talk to Unix admins they are so passionate about Unix CUI and the power of Unix Scripting language,  I don't disagree either.
I would strongly suggest reading some of the Unix books to unlock the full potential of Unix. If you occasionally login to Unix (like me) here are few of the commands you can use.

  • Secure Copy (scp). This command is useful if you want to copy file from one server to other.
  • Update: Use rsync (given below)instead of scp
$scp foo.txt  user@<destination server>:<destination folder/>foo.txt
if you want to copy entire directory
$scp -r foo user@<destination server>:foo

if you also wants to preserve file timestamp, so that it doesn't look like all the files are recently created / modified in the destination server use -p
$scp -r -p foo user@<destination server>:foo
  • Soft Links. This is similar to windows shortcuts, however from the file system perspective it will look like a read directory. Even though you are creating a shortcut, you still need to have read access to the folder.
ln -s <destination folder name> <linkname>
  • which - Helps you find the location of the file you will execute when you type the name from the command prompt. This will become handy especially when you have multiple version of same program (Like java) and you have no idea which one is being executed.
$which java
  • Taking a peek at running processes
$ps -ef

  • Unix has multiple utilities to automate tasks. I think most famous ones are Cron & Autosys. It's  similar to Windows Task Scheduler, however there is no GUI. If you want to use crontab to schedule jobs here is the format.
*     *     *   *    *      
|     |     |   |    |    +--- command to be executed
|     |     |   |    +------ day of week (0 - 6) (Sunday=0)
|     |     |   +--------- month (1 - 12)
|     |     +------------ day of month (1 - 31)
|     +---------------- hour (0 - 23)
+-------------------- min (0 - 59)

30 15 * * 1-5 /usr/bin/ksh <shell Script>   --> This will run your script at 3.30 PM, Mon-Fri.

$crontab -l --> Will list scheduled tasks
$crontab -e --> Will let you edit the task in VI Editor.

Finally if you don't know how to use a particular command, Unix has powerful built in help called man (stands for manual). That's your "F1" key for Unix.

Update: 11/22/13
This is most powerful and my most favorite command for synchronizing directories across multiple servers or the same machine.
Format: Please go through man rsync to take a quick at all available options.
Most common format:
rsync -avzh <source> <destination>
source or destination can be in any of the following format
/home/balaji/mydata  => mydata directory present locally
balaji@unix_server2:/home/balaji/mydata =>Connect to unix_server2 as balaji and synchronize my_data directory

For example if you want to synchronize mydata directory present under your home in unix_server1 with unix_server2 the command will look like.
Assuming you logged into unix_server1
[balaji@unix_server1 ~]$ rsync -avzh /home/balaji/mydata balaji@unix_server2:/home/balaji
Please note: you don't need to specify "mydata" again on the destination side and it will automatically create it. Also the above options retains the file modification time as well.

Now lets look at the options
a->archive (Included -rlptgoD)
l->copy symlinks as symlinks
p->preserve permissions
t->preserve modification times
z->Compress data during transfer
h->human readable
n-> Dry Run .. Nothing gets copied . It pretends as if it is copying but does nothing. Just add "n" to options if you not sure about your sync command.
Here is the nice part of it, if you run this command multiple times, it sends just the updated files since the last run and is super quick.

Directory size
To find the space left at the volume level us
$df - h
If you want to find the size of a directory for example, mydata then use
$du -skh /home/balaji/mydata

Sunday, April 15, 2012

Enabling VNC Server in Linux

Linux comes in lot of flavors and steps you need to follow to enable VNC Server may be slightly different. Google is your best friend.
Try these steps for Redhat and best of luck.

  • Review ~home/.vnc/xstartup file and you may have to uncomment first couple of lines to enable VNC Session manager

  • Start VNC by executing /usr/bin/vncserver command . For the first time, you need to setup a password to protect your VNC Session.
Stopping VNC
"ps -la" will list all user processes and you can find VNC processes and kill it using "kill -9 <VNC Process ID>"

Cleaning up VNC Files
rm ~home/.vnc/<hostname>*
rm /tmp/.X*lock
rm /tmp/.X11-unix