8 Pratical Examples of Linux “Touch” Command–reference

InLinuxevery single file is associated with timestamps, and every file stores the information of las

InLinuxevery single file is associated with timestamps,and every file stores the information of last access time,last modification time and last change time. So,whenever we create new file,access or modify an existing file,the timestamps of that file automatically updated.

In this article we will cover some useful practical examples of Linuxtouch command. Thetouch commandis a standard program forUnix/Linuxoperating systems,that is used to create,change and modify timestamps of a file. Before heading up for touch command examples,please check out the following options.

Touch Command Options

  1. -a,change the access time only
  2. -c,if the file does not exist,do not create it
  3. -d,update the access and modification times
  4. -m,change the modification time only
  5. -r,use the access and modification times of file
  6. -t,creates a file using a specified time

1. How to Create an Empty File

The following touch command creates an empty (zero byte) new file calledsheena.

# touch sheena

2. How to Create Multiple Files

By using touch command,you can also create more than one single file. For example the following command will create 3 files named,sheena,meenaandleena.

# touch sheena meena leena

3.Change File’s Access Time using -a

We can change the access time of a file using -a option. By default it will take the current system time and update the atime field.

Before touch command is executed:

$ stat tgs.txt

File: `tgs.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2012-10-18 23:58:21.663514407 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-18 23:58:21.663514407 +0530

$ touch -a tgs.txt

After the above touch command (Please note that the access time is changed):

$ stat tgs.txt

File: `tgs.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2012-10-19 00:08:23.559514525 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-19 00:08:23.559514525 +0530

4. How to Avoid Creating New File

Using-coption with touch command avoids creating new files. For example the following command will not create a file calledleenaif it does not exists.

# touch -c leena

5. How to Change File Modification Time

If you like to change the only modification time of a file calledleena,then use the-moption with touch command. Please note it will only updates the last modification times (not the access times) of the file.

# touch -m leena

6.Explicitly Setting Access and Modification time using -t and -d

Instead of taking the current time-stamp,you can explicitly specify the time using -t and -d options.

The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]

$ touch -t [[CC]YY]MMDDhhmm[.SS]

The following explains the above format:

  • CC – Specifies the first two digits of the year
  • YY – Specifies the last two digits of the year. If the value of the YY is between 70 and 99,the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37,the value of the CC digits is assumed to be 20. It is not possible to set the date beyond January 18,2038.
  • MM – Specifies the month
  • DD – Specifies the date
  • hh – Specifies the hour
  • mm – Specifies the minute
  • SS – Specifies the seconds

For example:

$ touch -a -m -t 203801181205.09 tgs.txt

Verify the above change using stat command:

$ stat tgs.txt
  File: `tgs.txt'
  Size: 3           Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2038-01-18 12:05:09.000000000 +0530
Modify: 2038-01-18 12:05:09.000000000 +0530
Change: 2012-10-19 00:40:58.763514502 +0530

You can also use a string to change the time

Another example:

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

For developers,touch command will be really helpful when you are working with

7. How to Use the time stamp of another File

The following touch command with-roption,will update the time-stamp of fileleenawith the time-stamp ofmeenafile. So,both the file holds the same time stamp.

# touch -r leena meena

8. Create a File using a specified time

If you would like to create a file with specified time other than the current time,then the format should be.

# touch -t YYMMDDHHMM.SS tecmint

For example the below command touch command with-toption will gives thetecmintfile a time stamp of18:30:55 p.m. onDecember 10,2012.

# touch -t 201212101830.55 tecmint

9. 批量创建文件

#!/bin/ name {..

We’ve almost covered all the options available in the touch command for more options use “man touch“. If we’ve still missed any options and you would like to include in this list,please update us via comment box.

reference :

8 Practical Examples of Linux “Touch” Command

5 Linux Touch Command Examples (How to Change File Timestamp)

linux shell 脚本攻略

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部