The “touch” Command in Linux [8 Practical Examples]

The touch command is very useful for updating the access or modification time of single and multiple files on the Ubuntu terminal. You can also create files with it. In this article, I will describe all the features and uses of the touch command in Linux with multiple examples.

A. Description

The touch command allows us to update a file’s access or modification time. However, if the file doesn’t exist we can create that file. This ability to create files makes the touch command one of the most useful commands.

B. Syntax

The syntax for the touch command is pretty simple which is the command itself followed by some specific options.

touch [OPTION]... FILE...

Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. Moreover, 3 dots after FILE suggests that you can specify multiple files to create and change the access & modification time.

C. Options

Many useful options are available for the touch command. I have listed some of the most used ones here. However, if you want to know more about options for the touch command you can always check the man page for the touch command.

man touch

Useful Options

  • -a (changes only the access time)
  • -c (do not create any files)
  • -d (parse STRING and use it instead of current time)
  • -m (changes only the modification time)
NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “touch” Command in Linux

The touch command in Linux is an easy-to-use command that is used to update the access and modification time of files. Some of the most useful applications of the touch command have been illustrated below.

Example 1: Create a File Using the “touch” Command in Linux

You can create a new file using the touch command. To make a new file you should follow the below procedure. Here, I have created a file named newfile.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls

➌ Press the ENTER button.

You will get the list of files and folders in your directory printed on display.

➍ To create a new file type the following command in the command prompt:

touch newfile.txt

➎ Now, press the ENTER button again.

➏ Again, type the following command in the command prompt:

ls

➐ Finally, press the ENTER button.

Output >

You can see in the below picture that the newfile.txt file is created on my desktop.Create a File Using the “touch” Command in Linux


Similar Readings


Example 2: Create Multiple Files Using the “touch” Command

You can create multiple new files using touch. To make multiple new files you should follow the below procedure. Here, I have created 3 files named file1.txt, file2.txt, and file3.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls

➌ Press the ENTER button.

You will get the list of files and folders in your directory printed on display.

➍ To create a new file type the following command in the command prompt:

touch file1.txt file2.txt file3.txt

➎ Now, press the ENTER button again.

➏ Again, type the following command in the command prompt:

ls

➐ Finally, press the ENTER button.

Output >

You can see in the picture that files named file1.txt, file2.txt, and file3.txt. are created on my desktop.Create Multiple Files Using the “touch” Command

Example 3: Change Access Time Using the “touch” Command

You can change the access time of a file using touch. To make the change of a file you should follow the below procedure. Here, I have changed the access time of a file named file2.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls -lu file2.txt

➌ Press the ENTER button.

You will get the access time of the file2.txt file in your directory printed on display.

➍ To change the access time of a file, type the following command in the command prompt:

touch -a file2.txt

➎ Now, press the ENTER button again.

➏ Again, type the following command in the command prompt:

ls -lu file2.txt

➐ Finally, press the ENTER button.

Output >

You can see in the picture that the access time of the file2.txt file has changed from 10:28 to 10:51.Change Access Time Using the “touch” Command

Example 4: Change Modification Time Using the “touch” Command

You can change the modification time of a file using the touch command. To make the change of a file you should follow the below procedure. Here, I have changed the modification time of a file named file1.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls -l file1.txt

➌ Press the ENTER button.

You will get the modification time of the file1.txt file in your directory printed on display.

➍ To change the modification time of a file, type the following command in the command prompt:

touch -m file1.txt

➎ Now, press the ENTER button again.

➏ Again, type the following command in the command prompt:

ls -l file1.txt

➐ Finally, press the ENTER button.

Output >

You can see in the picture that the modification time of the file1.txt file has changed from 10:28 to 10:47.Changing the modification time

Example 5: Change Access & Modification Time

You can change both the access & modification time of a file using the touch command. To make the change of a file you should follow the below procedure. Here, I have changed both the access & modification time of a file named file1.txt.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls -l file1.txt

➌ Press the ENTER button.

You will get the modification time of the file1.txt file in your directory printed on display.

➍ Type the following command in the command prompt and press the ENTER button.

ls -lu file1.txt

You will get the access time of the file1.txt file in your directory printed on display.

➎ To change both the access & modification time of a file, type the following command in the command prompt:

touch -am file1.txt

➏ Now, press the ENTER button again.

➐ Again, type the following command in the command prompt:

ls -l file1.txt

➑ Press the ENTER button.

➒ Type the following command in the command prompt:

ls -lu file1.txt

Finally, press the ENTER button.

Output >

You can see in the picture that both the access & modification time of the file1.txt file has changed from 10:56 & 10:28 to 11:00 & 11:00 respectively.Change Access & Modification Time

Example 6: Avoid Creating New File Using the “touch” Command

You can avoid creating a new file using the “touch” command if the file doesn’t exist. To avoid making a new file you should follow the below procedure.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls

➌ Press the ENTER button.

You will get the list of files and folders in your directory printed on display.

➍ Type the following command in the command prompt:

touch -c file4.txt

➎ Now, press the ENTER button again.

➏ Again, type the following command in the command prompt:

ls

➐ Finally, press the ENTER button.

Output >

You can see in the picture that there is no new file created on my desktop named file4.txt.Avoid Creating New File Using the “touch” Command


Similar Readings


Example 7: Set Specific Timestamp Using the “touch” Command

You can set the specific timestamp for a  file using touch. To do this you should follow the below procedure. Here, I have set a specific timestamp for the newfile.txt file.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls -l --full-time newfile.txt

➌ Press the ENTER button.

You will get the full timestamp of the newfile.txt file in your directory printed on display.

➍ Type the following command in the command prompt:

touch -t 199801020304.05 newfile.txt

You must follow this order.

  • 1998 denotes year
  • 01 denotes month
  • 02 denotes date
  • 03 denotes hour
  • 04 denotes minutes
  • 05 denotes seconds

➎ Now, press the ENTER button again.

➏ Again type the following command in the command prompt:

ls -l --full-time newfile.txt

➐ Finally, press the ENTER button.

Output >

You can see in the picture that the given specific timestamp is added to the newfile.txt file on my desktop.

Set Specific Timestamp Using the “touch” Command

Example 8: Set the Timestamp by Using Another File as the Reference

You can set the timestamp of a  file based on another file’s timestamp on your desktop using the touch command. To do this you should follow the below procedure. Here, I have set the timestamp of the file2.txt file based on the timestamp of the file1.txt file.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls -l file1.txt file2.txt

➌ Press the ENTER button.

You will get the timestamp of the file1.txt & file1.txt files in your directory printed on display.

➍ Type the following command in the command prompt to set the reference:

touch -r file1.txt file2.txt

➎ Now, press the ENTER button again.

➏ Again type the following command in the command prompt:

ls -l file1.txt file2.txt

➐ Finally, press the ENTER button.

Output >

You can see in the picture that the timestamp of file2.txt has changed according to the timestamp of the file1.txt file on my desktop.Set the Timestamp by Using Another File as the Reference

Conclusion

After completing this article, you will acquire all the knowledge about the touch command in Linux. With the given practical examples, you will be able to execute many commands to become a power user.


Similar Readings

5/5 - (1 vote)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Borhan Uddin

Hello, I am Borhan Uddin, a resident of Dhaka, Bangladesh. I have completed my undergraduate degree in Electrical and Electronic Engineering (EEE) from Chittagong University of Engineering and Technology (CUET). I love to spend my leisure by playing and watching various movies & dramas. Currently, I am working as a Linux Content Developer Executive here. I would like to learn more about Linux every day of the week and would be keen to share it with you rapidly. Read Full Bio

Leave a Comment