..MindWrite..

Archive for the ‘unix’ Category

Copy files from one ec2 instance to other ec2 instance

Posted by guptaradhesh on January 7, 2014

Doing an scp gave the following error message:

Permission denied (publickey)

The reason for the above is that it asks for ec2 instance password while we use .ppk file now days.

Here are the quick steps one should follow:

  1. server 1
    1. generate the id_rsa.pub
      ssh-keygen -t rsa
    2. /root/.ssh/id_rsa or /home/ec2-user/.ssh/id_rsa
    3. No need to enter any passphrase
  2. server 2
    1. Open /etc/ssh/sshd_config/sshd_config
    2. If logged as ec2-user; susu su
      Uncomment the lines below if they are not already
      RSAAuthentication yes
      PubkeyAuthentication yes
    3.  Open /root/.ssh/authorized_keys
  3. append contents of id_rsa.pub file of server 1 to authorized_keys of server 2
Advertisements

Posted in tech, unix | Tagged: , , | Leave a Comment »

Output redirection: > /dev/null 2>&1

Posted by guptaradhesh on July 27, 2012

One would frequently notice this training redirect construct in many Unix commands – all it means is, ALL output from this command should be pushed into /dev/null.

> /dev/null 2>&1

/dev/null is a place where we can dump anything we don’t want to use. Since, 1 is stdout and 2 is stderr: 2>&1 can be interpreted as redirect stderr to stdout.  Thus, Error messages which go to stderr are redirected to stdout which is being redirected to dev/null. Therefore, no output at all.

 

Posted in tech, unix | Tagged: , , , , , | Leave a Comment »

Number of occurrences of a string in a file using vi editor

Posted by guptaradhesh on June 20, 2012

:%s/string/&/gn

It counts the number of substitutions without actually performing the substitutions.

Posted in tech, unix | Tagged: , , , | Leave a Comment »

remove front spaces from a string

Posted by guptaradhesh on November 8, 2010

a simple sed command to do the same is as:

sed -e 's/^ *//' data

Posted in tech, unix | Tagged: , , | 1 Comment »

Unix mtime, ctime, atime

Posted by guptaradhesh on November 1, 2010

ctime: is the inode change time. When does the inode change, when you of course update a file, but also when you do things like changing the permissions of the file but not necessarily its contents. We can view the ctime with the ls -lc command.

atime: stands for access time which is when the file was last read. Displaying the contents of a file or executing a shell script will update a file’s atime. We can view the atime with the ls -lu command.

mtime: is the modification time, so if you change the contents of the file, this time is updated. Keep in mind when you change the contents of a file and and save it, the inode gets updated as well for that file, so ctime always changes too. We can view this with ls -l.

Therefore, when mtime changes, ctime will always change.

Posted in tech, unix | Tagged: , , , , | Leave a Comment »

some arbit workaround with Unix Shell

Posted by guptaradhesh on October 25, 2010

>> finding if a directory exists or not:

to check if a directory ‘tempDir’ exists in your home directory, the shell command would be,

$ [ ! -d ~/tempDir ] && echo 'Directory tempDir not found'
OR
$ [ -d ~/tempDir ] && echo 'Directory found' || echo 'Directory tempDir not found'

Similarly, to check if a file exists, we use -f parameter in the above command.

Parameters like -r, -w, -x are also available to check if the file exists and is readable, writable or executable resp.

>> Adding a directory to the path of user or all users:

PATH=$PATH:/path/of/my/scripts

"export PATH" must be added to ensure that any programs called 
by bash have the altered $PATH variable.

- To add a directory to the path of a single user, 
place the lines in that user's .bash_profile file.

- For all users globally, set a path in /etc/profile.

Posted in tech, unix | Tagged: | 1 Comment »