8 Practical Examples of Linux “Touch” Command

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 a new file, and access or modify an existing file, the timestamps of that file are automatically updated.

Linux Touch Command
Linux Touch Command Examples

In this article, we will cover some useful practical examples of Linux touch commands. The touch 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

  • -a, change the access time only
  • -c, if the file does not exist, do not create it
  • -d, update the access and modification times
  • -m, change the modification time only
  • -r, use the access and modification times of the file
  • -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 the 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. How to Change File Access and Modification Time

To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create a new empty file with the name.

# touch -a leena

The most popular Linux commands such as the find command and ls command use timestamps for listing and finding files.

4. How to Avoid Creating New File

Using the -c option with the 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 -m option with the touch command. Please note it will only update the last modification times (not the access times) of the file.

# touch -m leena

6. Explicitly Set the Access and Modification times

You can explicitly set the time using the -c and -t option with the touch command. The format would be as follows.

# touch -c -t YYDDHHMM leena

For example, the following command sets the access and modification date and time to a file leena as 17:30 (17:30 p.m.) December 10 of the current year (2020).

# touch -c -t 12101730 leena

Next verify the access and modification time of file leena, with the ls -l command.

# ls -l

total 2
-rw-r--r--.  1 root    root   0 Dec 10 17:30 leena

7. How to Use the time stamp of another File

The following touch command with the -r option, will update the time-stamp of file meena with the time-stamp of leena 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 a 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 give the tecmint file a time stamp of 18:30:55 p.m. on December 10, 2020.

# touch -t 202012101830.55 tecmint

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 them in this list, please update us via the comment box.

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

25 thoughts on “8 Practical Examples of Linux “Touch” Command”

  1. In example 6, you forgot to include the MM portion of the timestamp.

    CCYYMMDDHHMM
    

    So, 201912311200 would be 12:00 on 12/31/2019, for example.

    Reply
    • @Shilpa,

      You can create 100 text files using touch command as shown.

      # touch shilpa{1..100}.txt
      

      The above command will create files in following order.

      shilpa1.txt
      shilpa2.txt
      shilpa3.txt
      ...
      
      Reply
  2. @Ravi

    Nice coverage of touch as per the man pages, but I think the article would be more useful if you cover the scenarios to use touch. In which scenario one would like to use the following options.

    If you modify the file then modify time changes. How come ls -l gives the access time? Did you check it? Doesn’t it only list the modify time.

    If you use touch -a then the output of ls -l doesn’t change, however try it with touch -m to notice the output.

    Reply
  3. This may be behavior specific to the Apple/Mac implementation of TOUCH, but I figured one additional thing out. The touch command does not have an option to update the CREATION date of a file. However, if you use touch to update the modification date, and that date is before/earlier than the creation date, the creation date will be updated as well.

    Reply
  4. At 6. Explicitly Set the Access and Modification times you write for the format:
    [# touch -c -t YYDDHHMM leena]
    Should it not be: YYMMDDHH ?
    Two digits for the year (the last two) is fine, but followed by DD which should
    mean the DAY doesn’t make sense.
    The info (MAN) page for “touch” says “-t [[CC]YY]MMDDHHMM[.SS]”.
    I’m here because I’m getting a “touch: invalid date format ‘15062500’”, after doing
    “touch -c -t 15062500 my-file.txt”. That’s June 25, 2015, 00h(00).
    Trying to figure it out.

    Reply
    • @Marc,
      Do this way..

      tecmint@tecmint ~/renamefiles $ touch -c -t 1506250000 my-file.txt
      tecmint@tecmint ~/renamefiles $ ls -l
      total 0
      -rw-r--r-- 1 tecmint tecmint 0 Jun 25 00:00 my-file.txt
      
      Reply
      • Yes, thank you, I got it. I was omitting the dot before the seconds and
        doing as if the minutes were optional. They are not. It’s the seconds
        that are optional.

        But really you should edit your original post and replace YYDDHHMM
        with MMDDHHMM. That was a misprint. You cannot have day following
        year.

        Thanks again.

        Reply
  5. Hej

    I use Ubuntu 14.04, when I use touch like this:

    touch -t 195312100101 sekt-0001.jpg

    Then the file icon i filer have a clock on it why?

    I can still use the picture

    Reply
    • I think the idea is that you are just “touching” the file so it records that it was accessed/modified, but you are not actually changing the file in any other way. Note that there are no options to modify the content of the file itself.

      Reply
      • @Matt,
        Yes, you correct and I totally agree with your point, we are only touching the file, no modifications, but it records as accessed/modified time.

        Reply
  6. How does one update the modification timestamp of a file to become its current value plus one second?

    I would like to write a shell script which does this easily to any group of files as needed.

    Reply
  7. Ravi,
    Nice post & it’s very useful.

    One small correction here. I think the following command will update the time-stamp of file meena with the time-stamp of leena file.

    $ touch -r leena meena

    Will you check it.

    Thanks,

    Reply
    • Dear Vijay,

      Thanks for the findings, no one noticed that post was 1.2 year old and the only you who noticed. Thanks again, corrected in the write up..:)

      Reply

Leave a Reply to MykeC Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.