Thursday 4 February 2016

"Touch" command in linux

The touch is a very useful command in linux , which is used to create, change and modify time stamps of a file.  
Uses of Touch command :-
Several options which are very useful in touch command.
      -c :- if the file does not exist, do not create it
      -r :- Change time stamp of file1 similar to file2
      -a :- change the access time only
      -m :- change the modification time only
      -d :- update the access and modification times
      -t :- creates a file using a specified time
      -B :-  Going back the specified number of seconds
      -F:-  going forward the specified number of seconds.

use 1: Creating new file/files
You can create a single or multiple files in just once . Like touch a or touch a b c etc.
If we simply run touch command without any parameter, then it will create a new file with named that file.
For example. touch file1 , file 2, file 3, then files will be created as below
[root@localhost virender]$ touch file1 file2 file3
[root@localhost  virender]$ ls -ltrh|tail -n 3
-rw-rw-r--  1 root    root    0 Feb  4 12:12 file3
-rw-rw-r--  1 root    root    0 Feb  4 12:12 file2
-rw-rw-r--  1 root   root    0 Feb  4 12:12 file1
If you simply use touch command with file name , then there are two cases as below.
 a. If file is not available in that directory, then a new empty file will be created.
 b. If file is available, then only current date will be changed of already existing file. There will be no effect on size of that file
e.g  
[root@localhost virender]$ ls -ltrh|tail -n 3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file2
-rw-rw-r--  1 root root   18 Feb  4 12:16 file1
If file is not available in directory & you also don't want to create a new file, then use below
[root@localhost virender]$ touch -c file4    ( no file will be created)
Changing last modified time to current time [root@localhost virender]$ touch file1
[root@localhost virender]$ ls -ltrh|tail -n 3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file2
-rw-rw-r--  1 root root   18 Feb  4 12:20 file1      ( last modification time changed)
Use 2:  Changing time of file1 as same to file 2
 Use touch -r file1 file2. This command will change time of file2 same of file1. E.g
[root@localhost virender]$ ls -ltrh file*
-rw-rw-r-- 1 root root  0 Jan 29 10:52 file2
-rw-rw-r-- 1 root root  0 Feb  4 12:20 file3
-rw-rw-r-- 1 root root 18 Feb  4 12:20 file1
[root@localhost virender]$
[root@localhost virender]$ touch -r file1 file2
[root@localhost virender]$ ls -ltrh file*
-rw-rw-r-- 1 root root  0 Feb  4 12:20 file3
-rw-rw-r-- 1 root root  0 Feb  4 12:20 file2
-rw-rw-r-- 1 root root 18 Feb  4 12:20 file1
3. Changing access & modification time of file/files to current time.
You can check access & modification time of a file using stat command. E.g
[root@localhost virender]$ stat file1
  File: `file1'
  Size: 18              Blocks: 16         IO Block: 4096   regular file
Device: 806h/2054d      Inode: 3407892     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/ root)   Gid: (  500/ root)
Access: 2016-02-04 12:37:19.000000000 +0530
Modify: 2016-02-04 12:20:00.000000000 +0530
Change: 2016-02-04 12:37:19.000000000 +0530
[root@localhost virender]$
Now you can change access & modification time using options of touch command.
   a.  touch -a file1                      ( will change access time of file1)
   b.  touch -m file1                    ( will change modification time of file1)
   c.  touch -am file1                    ( will change access & modification time of file1)
4. Changing or creating a new file to other time.
Changing time of existing file . use touch -t YYMMDDHHMM.SS  file1
output will be as
[root@localhost virender]$ touch -t 1510051205.12 file1
[root@localhost virender]$ ls -ltrh file1
-rw-rw-r-- 1 root root 18 Oct  5 12:05 file1
[root@localhost virender]$ stat file1
  File: `file1'
  Size: 18              Blocks: 16         IO Block: 4096   regular file
Device: 806h/2054d      Inode: 3407892     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/ root)   Gid: (  500/ root)
Access: 2015-10-05 12:05:12.000000000 +0530
Modify: 2015-10-05 12:05:12.000000000 +0530
Change: 2016-02-04 13:09:12.000000000 +0530
Creating a new file with specific time stamp
[root@localhost virender]$ touch -c -t 1510051205.12 file5
[root@localhost virender]$ ls -ltrh file*
[root@localhost virender]$ touch -t 1510051205.12 file5
[root@localhost virender]$ ls -ltrh file*
-rw-rw-r-- 1 root root  0 Oct  5 12:05 file5