touch Command in Linux with Examples (The Complete Guide)

Today you will learn how to create a new empty file and change file timestamps in Linux using the touch command.

There are many commands in Linux operating system by which we can create files but cat command, touch command and, dd command are the most frequently used commands.

cat command creates a file with the content. If you don’t want to write content and just want to create an empty file, then you can’t use the cat command.

In that case, you have to use the touch command.

Before starting the topic, I want to explain to you some important useful terms.

As I told you, the touch command creates a new empty file and also changes the timestamp of a file.

The question that arises in the minds of beginners is what is this timestamp?

So let’s try to understand.

When a file is created in Linux, three timestamps are associated with it.

  • Access Time (atime): This is the date and time the file was last opened.
  • Modify Time (mtime): This is the date and time at which the contents of the file were last modified.
  • Change Time (ctime): This is the date and time at which the file’s attributes were last modified.

You can use the stat command to check the timestamps of a file. Here is an example:

~$ stat mydata.txt 
  File: mydata.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056265     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-28 03:20:13.458980696 -0800
Modify: 2021-01-28 03:20:13.458980696 -0800
Change: 2021-01-28 03:20:13.458980696 -0800
 Birth: -
Statistics of a file

First of all, let’s focus on some of the most important options that we can use with the touch.

OptionsExplanation
-aChange the access time
-c --no-createDon't create any files
-d --dateSet specified Timestamp
-h --no-dereferenceChange the timestamp of Symlink
-mChange the Modification time
-tUse [[CC]YY]MMDDhhmm[.ss] instead of current time
-r --referenceUse the timestamp of any other file
--helpDisplay help page of touch Command.
--versionCheck the version of touch command.

Key features of touch command:

  • Create Single or Multiple empty files
  • Change the access time
  • Change the modification time
  • You can avoid creating new files
  • Can use the Timestamp of any other file
  • Can set specified Timestamp
  • Change the Timestamp of a Symlink

Syntax:

You must follow the syntax given below to use the touch command.

touch [OPTION]... FILE...

1. Create an empty file

Type the following command to create an empty file. in this example, I am creating a file named file.txt.

Note: If there is no file with this name in the current directory, then only the touch command will create a new file otherwise it will set the timestamp (atime and mtime) of that existing file according to the current date and time.

$ touch file.txt

Output:

$ ls
file.txt

You can use the following command to create multiple empty files at once.

$ touch file1.txt file2.txt file3.txt

Output:

$ ls
file1.txt  file2.txt  file3.txt

OR use the following command to create multiple empty files quickly.

$ touch file{1..5}.txt

Output:

$ ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

Suggested Read: How to list contents (files/directories) in Linux

2. Change the modification time

Issue the following command to change the modification time of a file.

In this example, I am changing the modification time of the file named file.txt.

Note: If there is no file with this name in the current directory, then the touch command will create a new file.

$ touch -m file.txt

You can check the modification Date and Time of the file using the stat command

$ stat file.txt 
  File: file.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056300     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-27 23:00:02.725551367 -0800
Modify: 2021-01-27 23:00:14.668003694 -0800
Change: 2021-01-27 23:00:14.668003694 -0800
 Birth: -
Change the modification time

3. Change the access time

Type the following command to change the access time of a file.

Note: If there is no file with this name in the current directory, then the touch command will create a new file.

$ touch -a file.txt

Check the updated access Date and Time with the help of stat command.

$ stat file.txt 
  File: file.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056300     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-27 23:51:18.314972161 -0800
Modify: 2021-01-27 23:00:14.668003694 -0800
Change: 2021-01-27 23:51:18.314972161 -0800
 Birth: -
Change the access time

4. Don’t create any new files

As you saw in the above commands, if the file does not already exist, then the touch command creates a new file.

Let’s take a scenario on this feature of touch command, which will make it easier for you to understand.

If the file already exists in the current directory, the command will change the access and modification time of that file, and if no file exists, the command will not take any action.

In that case, you can pass the -c option to the touch command.

Let’s take an example:

Currently, a file named file2.txt exists in the current directory with the following access and modification details.

$ ls
file2.txt
$ stat file2.txt 
  File: file2.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056237     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-28 05:29:34.767712299 -0800
Modify: 2021-01-28 05:29:34.767712299 -0800
Change: 2021-01-28 05:29:34.767712299 -0800
 Birth: -

Now I run the command to create a file with the same name. Let’s see what happens.

$ touch -c file2.txt

Output:

As you can see no new file is created but access and modification statistics changed.

$ ls
file2.txt
$ stat file2.txt 
  File: file2.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056237     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-28 06:07:25.876126934 -0800
Modify: 2021-01-28 06:07:25.876126934 -0800
Change: 2021-01-28 06:07:25.876126934 -0800
 Birth: -

Let’s create a file with the new name using the same command.

$ touch -c file3.txt

As you can see no new file is created named file3.txt.

$ ls
file2.txt

5. How to use the timestamp of any other file

With the touch command, we can use the timestamp of a file in another file. for that, we have to pass the -r option to the touch command.

In this example, I am applying the timestamp of file2.txt to file3.txt.

$ touch -r file2.txt file3.txt

6. How to set a specified timestamp

This is my most favorite feature in the touch command.

You can set the timestamp of a file to your desired timestamp instead of the current timestamp.

To do this, the touch command gives us two options i.e. -t and -d.

To set our desired timestamp using the -t option, we have to use this format: [[CC]YY]MMDDhhmm[.ss]

Example #1

Following command will change the timestamp(Access and Modified Date and Time) of the file named test.txt to 28 Jan 09:20 AM.

$ touch -t 01280920 test.txt
$ stat test.txt 
  File: test.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056237     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-28 09:20:00.000000000 -0800
Modify: 2021-01-28 09:20:00.000000000 -0800
Change: 2021-01-28 07:54:22.760534506 -0800
 Birth: -

Example #2

Following command will change the timestamp(Access and Modified Date and Time) of the file named test.txt to 15 Mar 2021 23:20:14 PM

$ touch -t 202103152320.14 test.txt
$ stat test.txt 
  File: test.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056237     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-03-15 23:20:14.000000000 -0700
Modify: 2021-03-15 23:20:14.000000000 -0700
Change: 2021-01-28 08:01:12.443441880 -0800
 Birth: -

Suppose I want to set the Access and Modification date and time of file2.txt on 28 January 2021 08:55. To do this I have to run the following command.

$ touch -d '28 Jan 2021 08:55' file2.txt
$ stat file2.txt 
  File: file2.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 1056237     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/helpdesk)   Gid: ( 1000/helpdesk)
Access: 2021-01-28 08:55:00.000000000 -0800
Modify: 2021-01-28 08:55:00.000000000 -0800
Change: 2021-01-28 07:26:17.306959869 -0800
 Birth: -

7. How to Change the timestamp of Symlink

To change the timestamp of a Symlink pass the -h option to touch command.

In this example, I am changing the timestamp of Symlink named datafile_sym.txt

$ touch -h datafile_sym.txt

8. Check the version of touch command

Check the touch command version using the following command.

$ touch --version
touch (GNU coreutils) 8.28

9. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of touch command.

$ touch --help
$ man touch

You can visit at following websites to get more information on touch command.

Conclusion

I hope you have learned something from this article and you may have found that touch is a very important command in Linux.

I have tried my best to include all the features of touch command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.