加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

8 Pratical Examples of Linux “Touch” Command--reference

发布时间:2020-12-13 14:05:54 所属栏目:Linux 来源:网络整理
导读:In? Linux ?every 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 timestam

In?Linux?every 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 Linux?touch command. Thetouch command?is a standard program for?Unix/Linux?operating 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 called?sheena.

# 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,?meena?and?leena.

# 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?-c?option with touch command avoids creating new files. For example the following command will not create a file called?leena?if 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 called?leena,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?-r?option,will update the time-stamp of file?leena?with the time-stamp of?meena?file. 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?-t?option will gives the?tecmint?file a time stamp of?18:30:55 p.m. on?December 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 :

http://www.tecmint.com/8-pratical-examples-of-linux-touch-command/

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

?linux shell 脚本攻略

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读