Wednesday 11 September 2013

Time Stamp of a file in Linux


Every Linux file is associated with timestamps, which specifies the last access time, last modification time and last change time.

To get the timestamp of a file "ls -l" and "stat".

1. Use -c option to avoid creating new files, but will change the timestamp by present time.

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-10-18 23:42:12.000000000 -0700
Modify: 2012-10-18 23:42:12.000000000 -0700
Change: 2013-09-11 08:49:16.509859887 -0700

touch -c tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:49:25.685239864 -0700
Modify: 2013-09-11 08:49:25.685239864 -0700
Change: 2013-09-11 08:49:25.685239864 -0700

2. Change File’s Access Time using -a.

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:10:44.783796541 -0700
Modify: 2013-09-11 08:10:44.783796541 -0700
Change: 2013-09-11 08:10:44.783796541 -0700

touch -a tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:13:14.824798981 -0700
Modify: 2013-09-11 08:10:44.783796541 -0700
Change: 2013-09-11 08:13:14.824798981 -0700

3. Change File’s Modification Time using -m.

touch -m *.o

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:13:14.824798981 -0700
Modify: 2013-09-11 08:10:44.783796541 -0700
Change: 2013-09-11 08:13:14.824798981 -0700

touch -m tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-09-11 08:13:14.824798981 -0700
Modify: 2013-09-11 08:16:36.600236392 -0700
Change: 2013-09-11 08:16:36.600236392 -0700

4. Modification time using -t and -d.

touch -t [CCYYMMDDHHMM.SS] filename

touch -t 201212312359.59 tgs.txt 
touch -a -m -t 201212312359.59 tgs.txt 

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-12-31 23:59:59.000000000 -0800
Modify: 2012-12-31 23:59:59.000000000 -0800
Change: 2013-09-11 08:30:24.706252619 -0700

touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt

stat tgs.txt

  File: `tgs.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 525402      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-10-18 23:42:12.000000000 -0700
Modify: 2012-10-18 23:42:12.000000000 -0700
Change: 2013-09-11 08:37:51.182799786 -0700

5. Copy the Time-stamp from Another File using -r.

ls -l
-rw-r--r--. 1 root root 0 Sep 11 08:43 test.txt
-rw-r--r--. 1 root root 0 Oct 19  2012 tgs.txt

touch test.txt -r tgs.txt

ls -l

-rw-r--r--. 1 root root 0 Oct 19  2012 test.txt
-rw-r--r--. 1 root root 0 Oct 19  2012 tgs.txt

ref: http://www.thegeekstuff.com/2012/11/linux-touch-command/

Executing Background Jobs in Linux

Manage Background Jobs in Linux

when you execute a find command that might take a lot time to execute, you can put it in the background as shown below.

Appending an ampersand ( & ) to the command runs the job in the background.

     find / -type f -mtime -1000 >> /root/filelist.log &

Sending the current foreground job to the background using Ctrl + Z.

     find / -type f -mtime -1000 >> /root/filelist.log
     Ctrl + Z
     bg

Taking a job from the background to the foreground.

     fg
     find / -type f -mtime -1000 >> /root/filelist.log
     Ctrl + Z

View all the background jobs.

     jobs

     [1]+  Stopped    find / -type f -mtime -1000 >> /root/filelist.log

Kill a specific background job.

     fg %1
     kill %1

ref: http://www.thegeekstuff.com/2010/05/unix-background-job/


Ganglia Monitoring System

Ganglia

Ganglia is a scalable distributed monitoring system for high-performance computing systems such as clusters and Grids.

Ganglia Installation and Configuration

yum -y install ganglia*

Edit gmetad.conf

vi /etc/ganglia/gmetad.conf

data_source "my cluster" localhost 192.168.1.100:8089

Edit ganglia.conf

vi /etc/httpd/conf.d/ganglia.conf

 Alias /ganglia /usr/share/ganglia
  <Location /ganglia>
     AllowOverride All
     DirectoryIndex index.php
  </Location>

Restart the services

/etc/init.d/httpd restart
/etc/init.d/gmetad restart
/etc/init.d/gmond restart

URL

 http://192.168.1.100/ganglia

ref: http://ganglia.sourceforge.net/


Saturday 25 May 2013

Text Processing Tools in Linux

The following command are often used when writing scripts to automate tasks.

The diff command :- to compare the content of two files for differences.

diff file1.conf file2.conf > output.txt
cat output.txt
Options are -c, -u and -r.

The patch command :- ued to apply simple patch file to a single file.

patch -b file1.conf < output.txt

The grep command :- displays the lines in a files that match pattern.

grep root /etc/passwd
ps -aux | grep sshd
Options are -i, -n, -r, -c, -v and -l.

The cut command :- ued to cut fields or colomns of text from a file and display standard output.

cut -f3 -d: /etc/passwd
/sbin/ip addr | grep 'inet' | cut -d ' ' -f6 | cut -d / -f1
Options are -d, -f and -c.

The head command :- displays first few lines of a file.

head /etc/passwd
head -n 3 /etc/passwd
Options is -n.

The tail command :- displays last few lines of a file.

tail -n 3 /etc/passwd
tail -f /etc/passwd
tail -f will continue to show updates until Ctrl+c is pressed.

The wc command :- counts the number of lines(l), words(w), bytes(c) and characters(m) in a file.

wc -l file1.conf
ls /tmp | wc -l
Options are -l, -w, -c and -m.

The sort command :- used to sort text data.

grep bash /etc/passwd | cut -d: -f1 | sort
options are -n, -k and -t.

The uniq command :- removes duplicate adjacent lines from a file.

cut -d: -f7 /etc/passwd | sort | uniq -c
Options are -u and -d

The echo command :-  output strings.

echo This is a test.
echo This is a test. > output.txt
cat output.txt

The cat command :- output or concatenate files.

cat file1.conf > output.txt
cat output.txt
cat file1.conf file2.conf | less
Options are -b, -n, -s, -v, -t, -e and -A.

The paste command :- join multiple files horizontally.

paste file1.conf file2.conf
Options are -d and -s.

The split command :- split files based on context lines.

split -l 500 myfile segment
split -b 40k myfile segment
Options are -l (line no) and -b(bytes).

The comm command :- to compare two files for common and distinct lines.

comm file1.conf file2.conf

The dirname command :-  it will delete any suffix beginning with the last slash ('/') character and return the result.

dirname /etc/httpd/conf/httpd.conf

The fold command :- used for for making a file with long lines more readable on a limited width terminal.
fold -w 30 file.txt

The sed command :- reads text input, line by line, and allows midfication.

sed 'word;wordtoreplace' < file1.conf > output.txt
sed '/word/ d' filename > output.txt
sed 's/word/ /g' filename > output.txt
sed 's/firstword//g; s/secondword//g' yourfile > output.txt

The awk command :- used as a data extraction and reporting tool.

awk "/word/" filename > output.txt

The less command :- used to view the contents of a text file one screen at a time.

less /etc/passwd


Friday 24 May 2013

How to scp, ssh and rsync without prompting for password


For example, login in to some host linux box as root 172.16.0.155 and pass the command as,

ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
35:50:28:70:2b:b0:9f:01:9a:8b:cb:0e:17:89:1d:a2 root@InVImFTSrv
The key's randomart image is:
+--[ RSA 2048]----+
|  o ... .o.      |
| o + .....       |
|+ o o ..  o      |
|o= + +   . .     |
|E + o   S        |
|.. .             |
|o..              |
|o.               |
| .               |
+-----------------+


now login to the host linux box as root 172.16.0.177 and copy,

scp -r root@172.16.0.155:/root/.ssh/id_rsa.pub /root/.ssh/

cd /root/.ssh/
cat id_rsa.pub >> authorized_keys
chmod 700 /root/.ssh/authorized_keys

now form 172.16.0.155 try ssh, scp or rsync as,

scp -r root@172.16.0.177:/usr/local/src/April /usr/local/src/

It won't prompt for any password.

Ref:-https://blogs.oracle.com/jkini/entry/how_to_scp_scp_and