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, sheenameena 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 Makefiles

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 102012.

# touch -t 201212101830.55 tecmint

9. 批量创建文件

#!/bin/bash
#create file in batch
for name in {..}.txt
do
touch $name
done

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 脚本攻略

8 Pratical Examples of Linux “Touch” Command--reference的更多相关文章

  1. 15 Practical Grep Command Examples In Linux / UNIX

    You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ...

  2. 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法

    Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...

  3. 12 Linux Which Command, Whatis Command, Whereis Command Examples

    This Linux tutorial will explain the three "W" commands. The three "W"s are what ...

  4. 13 Basic Cat Command Examples in Linux

    FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...

  5. 15 Basic ‘ls’ Command Examples in Linux

    FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...

  6. 18 Tar Command Examples in Linux

    FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...

  7. Linux touch命令详解

    Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...

  8. Linux Touch命令的8种使用技巧

    Linux touch命令不仅可以用于在Linux上创建空文件. 您可以使用它来更改现有文件的时间戳,包括其访问权限和修改时间. 本文介绍了8种可以通过Linux终端使用touch命令的方案. 我们在 ...

  9. Linux Touch命令的8种常见使用方法

    Linux touch命令不仅可以用于在Linux上创建空文件. 您可以使用它来更改现有文件的时间戳,包括其访问权限和修改时间. 本文介绍了8种可以通过Linux终端使用touch命令的方案. 我们在 ...

随机推荐

  1. nutch 很多url unfetched的原因

    bin/hadoop jar apache-nutch-1.7.job org.apache.nutch.crawl.CrawlDbReader crawl/crawldb -stats -sort ...

  2. Java线程监听,意外退出线程后自动重启

    Java线程监听,意外退出线程后自动重启 某日,天朗气清,回公司,未到9点,刷微博,顿觉问题泛滥,惊恐万分! 前一天写了一个微博爬行程序,主要工作原理就是每隔2分钟爬行一次微博,获取某N个关注朋友微博 ...

  3. NAT(NAPT)地址转换过程

    整理自NAT地址转换过程 注:本文实质讲的是NAPT(Network Address Port Translation),即网络端口地址转换.NAPT与动态地址NAT不同,它将内部连接映射到外部网络中 ...

  4. python加密解密

    from Crypto.Cipher import Blowfish  #easy_install pycrypto可以获得

  5. IE连EXCHANGE的HTTPS时,出现错误

    "There is a problem with this website's security certificate" 这种问题,一般是SSL或CA认证证书的问题. 一般从服务 ...

  6. Android 系统功能设置菜单 LinearLayout与relativeLayout的实现

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. 【网络流24题】 No.6 最长不减子序列问题 (最大流)[模型:最多不相交路径]

    [题意] 给定正整数序列x1 ,x2 , x3... ( 1)计算其最长不减子序列的长度 s.( 2)计算从给定的序列中最多可取出多少个长度为 s 的不减子序列.( 3) 如果允许在取出的序列中多次使 ...

  8. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

  9. Android Training精要(一)ActionBar上级菜单导航图标

    Navigation Up(ActionBar中的上级菜单导航图标) 在android 4.0中,我们需要自己维护activity之间的父子关系. 导航图标ID为android.R.id.home @ ...

  10. WCF Host中的BaseAddress 和 Endpoint中的Address的区别

    http://stackoverflow.com/questions/18720810/wcf-service-base-address-vs-endpoint-address baseAddress ...