×
touch command - full tutorial how to modify access time, modified time and created time on a file in linux

The touch command is a Linux command-line tool that can be used to create an empty file and modify the timestamps of files.

Timestamps stores the information of each file and folder including, access time, modify time and change time.

Whenever you create or change an existing file, the timestamps value will be updated automatically.

There are three types of timestamps :

  • Access time
    The last time the file was accessed by some application.
  • Modification time
    The last time the file contents were modified.
  • Changed time
    The last time the file’s metadata, attribute or content was changed.

In this tutorial, we will learn how to use Touch command with practical examples.

Basic Syntax for Linux Touch Command

The basic syntax of Touch command is shown below:

touch [OPTION] FILENAME

A brief explanation of each option is specified below:

  • -a
    Used to change only the access time.
  • -m
    Used to change only the modification time.
  • -d
    Used to update the access and modification times.
  • -c
    Do not create a new file, if the does not exist.
  • -r
    Used to use the access and modification times of another file.
  • -t
    Used to create a file with a specified time.

Create a Single File with Touch

You can create a single empty file by running the following command:

touch file1

You can now verify your file with the following command:

ls -l file1

You should see the following output:

-rw-rw-r-- 1 vyom vyom 0 Mar 29 10:44 file1

you can avoid creating new files if a file doesn’t exists, use the touch command with -c option as shown below:
touch -c file1

Once you are finished, you can proceed to the next example.

Create a Multiple Files with Touch

To create multiple files named file2, file3, file4 and file5, run the following command:

touch file2 file3 file4 file5

You can now verify all the files with the following command:

ls -l file*

You should see the following output:

-rw-rw-r-- 1 vyom vyom 0 Mar 29 10:44 file1
-rw-rw-r-- 1 vyom vyom 0 Mar 29 10:49 file2
-rw-rw-r-- 1 vyom vyom 0 Mar 29 10:49 file3
-rw-rw-r-- 1 vyom vyom 0 Mar 29 10:49 file4
-rw-rw-r-- 1 vyom vyom 0 Mar 29 10:49 file5

Example:

Change Access Time of File with Touch

If you want to change/update the access time of an existing file, you can use -a option file touch command.

For example, to change the access time of a file named file1 to the current time, run the following command:

touch -a file1

Next, verify whether the access time of a file has been changed or not using the following command:

stat file1

You should see the following output:


File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467544 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:50:14.503381528 +0530
Modify: 2020-03-29 10:44:37.155374736 +0530
Change: 2020-03-29 10:50:14.503381528 +0530
Birth: -

Example:

Change Modification Time of File with Touch

You can change the modification time of a file using -m option.

For example, to change the modification time of a file named file2, run the following command:

touch -m file2

Next, verify the modification time of a file with the following command:

stat file2

You should see the following output:

File: ‘file2’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467546 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:49:18.007380390 +0530
Modify: 2020-03-29 10:54:23.735386545 +0530
Change: 2020-03-29 10:54:23.735386545 +0530
Birth: -

Example:

Change Access and Modification Time of File with Touch

You can change both access and modification time of a file using the option -a and -m together.

For example, change the access and modification time of a file named file3 to the current time using the following command:

touch -am file3

You can now verify the access and modification time of a file with the following command:

stat file3

You should see the following output:

File: ‘file3’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467615 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:58:23.347391370 +0530
Modify: 2020-03-29 10:58:23.347391370 +0530
Change: 2020-03-29 10:58:23.347391370 +0530
Birth: -

Example:

Set Specific Access and Modification Time of a File with Touch

You can also set specific access and modification time of a file using the following syntax:

touch -c -t YYYYMMDDhhmm FILENAME

Where:

  • YYYY : Specify the four digits of the year.
  • MM : Specify the month of the year.
  • DD : Specify the day of the month.
  • hh : specify the hour of the day.
  • mm : Specify the minutes of the hour.

For example, set the access and modification time of a file named file4 to 11:25 AM Jan 10 of the year 2020, run the following command:

touch -c -t 202001101125 file4

You can verify the access and modification time of a file with the following command:

stat file4

You should see the following output:

File: ‘file4’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467620 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-01-10 11:25:00.000000000 +0530
Modify: 2020-01-10 11:25:00.000000000 +0530
Change: 2020-03-29 11:06:33.535401239 +0530
Birth: -

Example:

You can also set specific date and time in human readable format by using the -d option.

For example, set the date of a file named file4 to 15 Feb run the following command:

touch -d '15 Feb' file4

This command will set the date of a file4 to 15 Feb and the time is automatically set to 00:00.

You can verify it with the following command:

stat file4

You should see the following output:

File: ‘file4’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467620 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-02-15 00:00:00.000000000 +0530
Modify: 2020-02-15 00:00:00.000000000 +0530
Change: 2020-03-29 11:27:00.983425951 +0530
Birth: -

Example:

Set the Timestamp of Another File

You can also take a timestamp from another file and use it to set the time for other files using the -r option.

For example, set the timestamp of a file named file5 using the timestamp value of file1 using the following command:

touch file5 -r file1

You can now verify the timestamp value of file5 with the following command:

stat file5

You should see the following output:

File: ‘file5’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467630 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:50:14.503381528 +0530
Modify: 2020-03-29 10:44:37.155374736 +0530
Change: 2020-03-29 11:17:05.391413960 +0530
Birth: -

Example:

Conclusion

In the above guide, we’ve learned How to Use the Touch Command with several examples, including how to change Access, Modified and Created times.

We hope you can now use the touch command to change the access and modification time of file and create files as well.

Feel free to ask any questions below and post your comments!