Saturday, 19 December 2015

How To Use TOUCH In Unix/Linux

In this article we will see some more advance uses of touch command. We will see how can we implement these concepts with examples. Please go through previous post 

Basics of TOUCH Command on touch basics.


Create 4 Files at a time from 1 to 4.
$ touch {1..4}

Output: Will generate 4 files with name as 1,2,3,4.
-rw-r--r-- 1 baba None 0 Dec 19 19:54 3
-rw-r--r-- 1 baba None 0 Dec 19 19:54 2
-rw-r--r-- 1 baba None 0 Dec 19 19:54 1
-rw-r--r-- 1 baba None 0 Dec 19 19:54 4


Create 4 Text Files from 1 to 4.
$ touch file{1..4}.txt

Output: Will generate 4 files with name as fil1.txt,file2.txt so on.
-rw-r--r-- 1 baba None 0 Dec 19 19:55 file2.txt
-rw-r--r-- 1 baba None 0 Dec 19 19:55 file1.txt
-rw-r--r-- 1 baba None 0 Dec 19 19:55 file4.txt
-rw-r--r-- 1 baba None 0 Dec 19 19:55 file3.txt


Lets create a file abc.txt and see his properties using stat command.
$ touch abc.txt

 stat abc.txt
  File: ‘abc.txt’
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file
Device: 221fd83h/35782019d      Inode: 4222124650721620  Links: 1
Access: (0644/-rw-r--r--)  Uid: (197608/    baba)   Gid: (197121/    None)
Access: 2015-12-19 19:56:03.015820400 +0530
Modify: 2015-12-19 19:56:03.015820400 +0530
Change: 2015-12-19 19:56:03.015820400 +0530
 Birth: 2015-12-19 19:55:51.193144200 +0530

How to change file Access time
$ touch -a abc.txt
$ touch -a *.txt

Output: It will update the access time of the file abc.ttx. We can see the same using stat.
$ stat abc.txt
  File: ‘abc.txt’
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file
Device: 221fd83h/35782019d      Inode: 4222124650721620  Links: 1
Access: (0644/-rw-r--r--)  Uid: (197608/    baba)   Gid: (197121/    None)
Access: 2015-12-19 19:57:18.728150900 +0530
Modify: 2015-12-19 19:56:03.015820400 +0530
Change: 2015-12-19 19:57:18.728150900 +0530
 Birth: 2015-12-19 19:55:51.193144200 +0530


How to change file Modification time
$ touch -m abc.txt
$ touch -m *.txt 

Output: It will update the modified time of the file abc.txt. We can see the same using stat.

$ stat abc.txt
  File: ‘abc.txt’
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file
Device: 221fd83h/35782019d      Inode: 4222124650721620  Links: 1
Access: (0644/-rw-r--r--)  Uid: (197608/    baba)   Gid: (197121/    None)
Access: 2015-12-19 19:57:18.728150900 +0530
Modify: 2015-12-19 19:58:58.135836700 +0530
Change: 2015-12-19 19:58:58.135836700 +0530
 Birth: 2015-12-19 19:55:51.193144200 +0530

How to add specific last access time.
$ touch -d "23 august" abc.txt

Output: It will update the acces time of the file abc.txt to 23 August. We can see the same using stat.

$ stat abc.txt
  File: ‘abc.txt’
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file
Device: 221fd83h/35782019d      Inode: 4222124650721620  Links: 1
Access: (0644/-rw-r--r--)  Uid: (197608/    baba)   Gid: (197121/    None)
Access: 2015-08-23 00:00:00.000000000 +0530
Modify: 2015-08-23 00:00:00.000000000 +0530
Change: 2015-12-19 19:59:38.719157900 +0530
 Birth: 2015-12-19 19:55:51.193144200 +0530


$ touch -d "12:30" abc.txt

Output: It will update the access time of the file abc.txt to 12:30, which was set to 00:00:00  in previous command.
We can see the same using stat.

$ stat abc.txt
  File: ‘abc.txt’
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file
Device: 221fd83h/35782019d      Inode: 4222124650721620  Links: 1
Access: (0644/-rw-r--r--)  Uid: (197608/    baba)   Gid: (197121/    None)
Access: 2015-12-19 12:30:00.000000000 +0530
Modify: 2015-12-19 12:30:00.000000000 +0530
Change: 2015-12-19 20:00:26.694902000 +0530
 Birth: 2015-12-19 19:55:51.193144200 +0530

How to change access and modification time together.
$ touch -am abc.txt

We can use multiple switches together to update the required attributes.

How to modify the time stamp of a file by going forward/Backward with specific number of seconds.
$ touch -r abc.txt F 50 pqr.txt

The above command will make file abc.txt 50 seconds older than pqr.txt.

The below command with option "B" will make file pqr.txt older 50 seconds than abc.txt.
$ touch -r abc.txt B 50 pqr.txt

The above command will make file pqr.txt 50 seconds older than abc.txt.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...