Examples to Use touch Command in Linux

Helpful 12 Examples to Use touch Command in Linux

In Unix/Linux operating systems, the touch command is used to create, change, and modify the timestamps of a file. In other words, the touch command creates a file without any content. In this article, you will learn How to Use touch Command in Linux. It creates a file only if the file does not already exist. So, the created file is always empty. Using this command’s many options, we can change specific timestamps for the files provided as arguments. For a better understanding, we will cover 12 helpful examples to explain the exact usage of the touch command in Linux.

Generally, the creation of a file in Linux is possible using two commands cat and touch commands. The main difference is that you can create the file with content using the cat command while the touch command creates a file without any content. touch command Linux is not complicated. As a Linux user, you can use the touch command only when you do not have data to store during the file creation. Also, you can use touch command in Linux to modify the time of files and folders or change accessing them. Updating and changing the timestamps or modifying them to a date in the past is what the touch command does in Linux.

Join us with Linux touch command with Examples to get skilled in using it. The examples show how to use the Linux touch command with various options. They are all executed via the terminal. Before that, you need to review some essential definitions and requirements.

Linux File Timestamps

As we mentioned, to change the time of files and directories or how you access them, use touch command in Linux. So, let’s see what are these three timestamps:

Access time: Last time when a file was accessed. When a command, like grep or cat, analyzes a file’s contents, the access time, or atime, changes. The ls -lu command shows the file’s atime.

Modify time: Last time when a file was modified. When a file’s contents change, the modification time, or mtime, changes. The mtime for files is displayed by the ls -l command.

Change time: Last time when file metadata was changed. When a file’s property, such as its name, permissions, or location, change, the change time, or ctime, also changes. The ctime for files is displayed by the ls -lc command.

Prerequisites to Use touch Command in Linux

To Use touch Command in Linux, your machine needs to meet all the below specifications:

  • A Server running Linux VPS.
  • Access to the command line/terminal.
  • Basic terminal commands, such as ls.

Syntax of Linux touch Command

The fundamental touch Command syntax is shown below:

touch <options> <file or directory name>

syntax for touch command

touch Command Options

The touch utility functions both with and without options for advanced queries. There are options with lengthy and short formats. This tutorial all important commands that you need to use touch command in Linux that each one comes with an option to do the required task. Let’s see all available touch command options in the below table:

OptionsDescription
-aChanges the access time.
c
--no-create
Avoids creating a new file.
-d=[string]
--date=[string]
Changes a timestamp using a date string.
-fNo effect. In older BSD's the option forces changes.
-h
--no-dereference
Changes a symbolic link's timestamp.
-mChanges the modification time.
--helpOpens the help menu.
-r=[file]
--reference=[file]
Changes a timestamp to the referenced file's timestamp.
-t[stamp]Modifies a timestamp, where the stamp is the date/time format.
-v
--version
Prints the program version.

touch Command Examples in Linux

So far, you learned about touch command meaning. Let’s continue this guide with 12 practical Linux touch Examples which explain how to use touch command in Linux.

Remember that the touch command is used in all Linux distributions such as Ubuntu, Debian, CentOS, etc.

1. Create an Empty File

touch command in Linux is used for creating a new file (while it does not exist). To do this, the simplest syntax of the touch command is used:

touch <filename>

For example, if you have not already created a file called opera, you can use the above command to create your considered file:

touch opera
Output
~/touch$ touch opera
~/touch$ ls -l
total 0
-rw-rw-r--1 kb 0 Oct 25 2:24 opera
~/touch$

Using ls command, you can list the content of file and directory.

In the case of the existing file, touch will change all three timestamps to the current time without changing the contents and permission of the file as you can see below:

Output
~/touch$ ls -l
total 4
-rw-rw-r-- 1 kb kb Oct 25 2:24 opera
~/touch$ touch opera
~/touch$ ls -l
total 4
-rw-rw-r-- 1 kb kb Oct 25 2:35 opera
~/touch$

In this way, the timestamp of the file will be updated to the current time and the access and modification time is successfully updated. The access and modification times have been updated successfully as a result.

2. Create Multiple Empty Files with touch command

You can use touch command in Linux to create more than one empty file. Considering the names of the files you need to create, run the command below to create multiple empty files:

touch opera1 opera2
Output
~/touch$ touch opera 1 opera 2
~/touch$ ls -l
total 0
-rw-rw-r--1 kb kb 0 Oct 25 2:45
-rw-rw-r--1 kb kb 0 Oct 25 2:45
~/touch$

Applying little changes in the above command, you can create large batches of files. To do this, Add curly braces and indicate the first and last element in addition to the filenames:

In the below example, we create 5 files with appended numbering:

touch opera{1..5}
Output
~/touch$ touch opera{1..5}
~/touch$ ls -l
total 0
-rw-rw-r--1 kb 0 Oct 25 2:57 opera1
-rw-rw-r--1 kb 0 Oct 25 2:57 opera5
-rw-rw-r--1 kb 0 Oct 25 2:57 opera2
-rw-rw-r--1 kb 0 Oct 25 2:57 opera3
-rw-rw-r--1 kb 0 Oct 25 2:57 opera4
-rw-rw-r--1 kb 0 Oct 25 2:57 opera5
~/touch$

Also, you can do the same using letters. But it is not possible to combine letters and numbers.

touch opera_{a..e}
Output
~/touch$ touch opera{a..e}
~/touch$ ls -l
total 0
-rw-rw-r--1 kb 0 Oct 25 3:05 opera-a
-rw-rw-r--1 kb 0 Oct 25 2:57 opera-b
-rw-rw-r--1 kb 0 Oct 25 2:57 opera-c
-rw-rw-r--1 kb 0 Oct 25 2:57 opera-d
-rw-rw-r--1 kb 0 Oct 25 2:57 opera-e
~/touch$

3. Avoid Creating A New File

By now, you certainly know that the touch command creates a new file if it does not exist. But what if you need to get a different result?

For example, if you do not need a new file creation when running the touch command or only need to modify the read or access time of your considered file, the below command is all you should run:

touch -c <filename>

According to our example:

touch -c new_opera

Using the -c flag, touch command will not create a new file and just modify the timestamps of the existing file. However, you can run the command below to check the list directory contents and make sure that the file is not there:

ls -l
Output
~/touch$ touch -c new-opera
~/touch$ ls -l
total 4
-rw-rw-r--1 kb kb 6 Oct 25 3:12 opera
~/touch$

4. Set Specific Timestamp (Access & Modification)

In this part, you will learn How to use touch command in Linux to set a specific timestamp for an existing file. While touch command mostly changes the timestamps to the current timestamp. So, there should be a way to make an exception. Using the -t option lets you set access and modification time to a past or future date:

touch -t <timestamp> <filename>

Here is the specific pattern of timestamp format:

[[CC]YY]MMDDhhmm[.ss]
  • CC – First two digits of a year
  • YY – Second two digits of a year
  • MM – Month of the year (01-12)
  • DD – Day of the month (01-31)
  • hh – Hour of the day (00-23)
  • mm – Minute of the hour (00-59)
  • ss – Seconds (00-59)

In the instance above, CC is not required. In actuality, CCYY is also optional; in that instance, the current year will be used. Likewise, seconds are optional and have a default value of 00.

For example, to change the timestamps to the 11th month, 26th day, and 6th hour of the current year, you need to run:

touch -t 11260600 opera

To view the details of timestamps, use the --full-time option.

:~/touch$ ls -l --full-time

5. Set File Timestamp in linux

One of the situations you can use touch command in Ubuntu Linux is when you need to use another date as timestamp. Using a date string as -d flag enables you to set a timestamp. The date string is a versatile time format that supports a wide range of legible textual representations.

touch -d <string> <filename>

In this way, the actual date for the file timestamps can be manipulated. For example, you can run the command below to change the timestamp:

touch -d +1 week opera
Output 
~touch$ ls -l
-rw-rw-r--1 kb kb 6 Oct 25 3:15 opera
~touch$ touch -d +1 week opera
~touch$ ls -l
total 4
-rw-rw-r--1 kb kb 6 Nov 01 2023 opera 
~touch$

6. Update Access Time of File to Current

Let’s see how to use touch command in Linux to change the only access time of file. In this case, touch command is helpful when you do not prefer to change all the timestamps of a file. To update the access time of your considered file, the -a option can be used with touch.

Here is the basic syntax:

touch -a <filename>

To change the access time for the file named opera, run:

touch -a opera

Finally, you can use the command below to view the changed time:

ls -lu
Output
~touch$ s -lu
total 4
-rw-rw-r--1 kb kb 6 Nov 26 1989 opera
~touch$ touch -a opera
~touch$ ls -lu
total 4
-rw-rw-r--1 kb kb 6 Oct 25 3:23 opera
~touch$

7. Change Access Time Clearly

Also, you can use touch command in ubuntu, centos and etc. to modify the access time to a specific timestamp. To do this, run the command below by combining the -a and -t options:

touch -at <timestamp> <filename>

If you prefer to check the access time before doing the changes, type:

ls -lu

For example, to change the access for the file opera to 10:10 1989 Nov 26th:

touch -at 8911261010 opera

Again, check the modification time by running the following command:

ls -lu
Output
~touch$ s -lu
total 4
-rw-rw-r--1 kb kb 6 Oct 25 3:27 opera
~touch$ touch -at 8911261010 opera
~touch$ ls -lu
total 4
-rw-rw-r--1 kb kb 6 Nov 26 1989 opera
~touch$

8. Update the Modification Time to Current

To use touch command in Linux to update the modification time only, you can use the general below syntax:

touch -m <filename>

To change the modification time to the current timestamp, the -m is used.

Before changing the timestamps, you can check the mtime of the file:

ls -l

Then, run the following command to change the modification time for your considered file:

touch -m opera

Lastly, recheck the mtime after the change by running the command below:

ls -l
Output
~touch$ s -l
total 4
-rw-rw-r--1 kb kb 6 Nov 26 1989 opera
~touch$ touch -m opera
~touch$ ls -l
total 4
-rw-rw-r--1 kb kb 6 Oct 25 1989 3:35 opera
~touch$

As you see, changing the modification time to the current timestamp is done by the -m option by default.

9. Update the Modification Time Clearly

In this part, you will learn How to use touch command in Linux to change modification time for a specific file. To do this, you need to combine the -m option with -t to clearly state the modification timestamp. Here is the basic syntax:

touch -mt <timestamp> <filename>

If you need, check the file’s mtime before changing it:

ls -l

Then, as an example, change the access for the file opera to 10:10 1989 Nov 26th:

touch -mt 8911261010 opera

Finally, you can use the following command again to check the modification time:

ls -l
Output
~touch$ ls -l
total 4
-rw-rw-r--1 kb kb 6 Oct 3:45 opera
~touch$ touch -mt 8911261010 opera
~touch$ ls -l
total 4
-rw-rw-r--1 kb kb 6 Nov 26 1989 opera
~touch$

As you see above, updating the modification time to a specific value can be done by adding the -t option.

10. Change Modification and Access Time in the Same time

To change both modification and access time, you need to use touch command in Linux as you see below:

touch -am <filename>

Run the below commands to check the file’s atime and file’s mtime before changing:

ls -lu
ls -l

Then, to change both times for the opera file to the current timestamp, type:

touch -am opera

Finally, to recheck the atime and mtime after the change, run:

ls -lu
ls -l
Output
~touch$ ls -l
total 4
-rw-rw-r--1 kb kb 6 Nov 26 1989 opera
~touch$ ls -lu
total 4
rw-rw-r--1 kb kb Nov 26 1989 opera
~touch$ touch -am opera
~touch$ ls -l
total 4
rw-rw-r--1 kb kb Oct 25 3:58 opera
~touch$ ls -lu
total 4
rw-rw-r--1 kb kb Oct 25 3:58 opera
~touch$

11. Use another File Timestamp as Reference

To set timestamps using a reference file, you can use touch command in Linux. The -r option is used with touch command to use the timestamps of another file.

To set the timestamp using a reference file, run:

touch -r <reference file> <target file>

In the below example, we create a new file and reference the timestamp of an existing opera file:

touch -r opera new_opera

Using the following command, you can check the timestamp for both of the above files:

ls -l
Output
~/touch$
touch -r opera new-opera
~/touch$ ls -l
total 4
-rw-rw-r-- 1 kb kb 0 Nov 26 1989 new-opera
-rw-rw-r-- 1 kb kb 6 Nov 26 1989 opera
~/touch$

12. Change Timestamp of a Symbolic Link

In the previous example, you learned the way you can use touch command in Linux to change the timestamps by changing the reference file’s timestamp. In this part, you will learn how to do the same without doing that. Using the -h option, allows you to modify the time for a symbolic link.

touch -h <filename>

Before changing, you can use the command below to check the time for an existing symbolic link:

ls -l

Now, to set the timestamp for the symbolic link to the current time, run:

touch -h link

When you are finished, you can check the timestamp to confirm the change. To do this, type:

ls -l
Output
~/touch$ ls -l
total 4
-rw-rw-r-- 1 kb kb 4 Nov 26 1989 link -> opera
-rw-rw-r-- 1 kb kb 6 Nov 26 1989 opera
~/touch$ touch -h link
~/touch$ ls -l
total 4
-rw-rw-r-- 1 kb kb 4 Oct 25 4:14 link -> opera
-rw-rw-r-- 1 kb kb 6 Nov 26 1989 opera
~/touch$

Obviously, if you do not use the -h option, the symbolic link’s timestamp will stay unchanged and only the opera file’s timestamp will change.

That’s it! Using the above examples, now you know how to use touch command in Linux to modify the timestamps of the file.

Conclusion

In this article, you learned about touch command definition and How to Use touch command in Linux. The examples in this guide may help you know when and how to use the touch command and its appropriate options. Now you know all you need about this helpful command to get better at working with files in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked.