Sunday, March 18, 2012

Some interesting Unix commands - PART I


  1.  newname=$(ls *.log 2> /dev/null | sed 's/log/bad/' | sort -n )   --> this will list the files ending with .log and send errors to /dev/null. Then substitute the filename from log to bad and sort the files and store the filenames in the CRON_LOG variable. the actual file names will be untouched. 
    • sed options include: i-> edit files in place. Also, n-> silent output if not using edit file in place.
  2. Another way of doing the same as 1 is : perl -p -i -e 's///g'  *. You can also create a backup using  perl -p -i.bak -e 's///g'  *. This replaces within the existing file.
  3. echo ls *|tr [:lower] [:upper]  --> translates lower filenames to upper
  4. ls *.log|tr -d log ---> Deletes log from the filename
  5. ls file{2,3}*log ---> Only lists files: file2.log and file3.log
  6. ls *.log|cut -d "." -f1 --> prints out all the file names without the ".log"
  7. find /home/ -type f -name "*.log"
  8. newname=$(ls *.bad|cut -d '.' -f1,2,3) and then for z in $newname; do echo $z".log";done ---> the first command truncates .bad from filename abc.2323.gz.bad. The second one traverses through all the newnames and appends .log to them. So, the new filename is now abc.2323.gz.log

How to create keys on ssh

- On your local machine: ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
be:fd:e4:c6:bb:b5:20:4d:ea:9c:76:39:db:7a:70:f3 user@centosvm03


- Server A is the server that you would like to ssh into from your local machine:
cat .ssh/id_rsa.pub | ssh user @A 'cat >> .ssh/authorized_keys'

Thats it!!! Now, you can log into Server A from your local host without a password or with the passphrase if you didn't leave it blank.

How to install new modules from CPAN

There are various ways you can do this. Keep in mind that you need to be logged in as 'root' to be able to install a package from CPAN:
 - Use the cpan shell and install all the modules you would like:
- If you don't want to log into the shell. You can use Perl to download a module from CPAN directly from the command line: