zoukankan      html  css  js  c++  java
  • 8 Pratical Examples of Linux “Touch” Command--reference

    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 {1..10}.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 脚本攻略

  • 相关阅读:
    「Kafka」Kafka中offset偏移量提交
    「Flink」Flink中的时间类型
    Hash存储模型、B-Tree存储模型、LSM存储模型介绍
    「Flink」RocksDB介绍以及Flink对RocksDB的支持
    「Flink」理解流式处理重要概念
    「Flink」Flink 1.9 WebUI运行作业界面分析
    「Spark」Spark SQL Thrift Server运行方式
    「Flink」配置使用Flink调试WebUI
    Django基础篇之简介和项目创建
    在python文件中操作django orm提示环境变量设置问题
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3778208.html
Copyright © 2011-2022 走看看