Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command

In Linux, every file is associated with timestamps, which store information such as the last access time, last modification time, and last change time. Therefore, whenever we create a new file, access, or modify an existing file, the timestamps of that file are automatically updated.

This article will introduce some practical examples of the Linux touch command. The touch command is a standard utility in Unix/Linux operating systems used to create, change, and modify file timestamps.

Before learning the examples of the touch command, please review the following options:

Syntax of the touch Command

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command

  • <span>-a</span>, change only the access time
  • <span>-c</span>, do not create the file if it does not exist
  • <span>-d</span>, update access and modification times
  • <span>-m</span>, change only the modification time
  • <span>-r</span>, use the access and modification times of another file
  • <span>-t</span>, create a file with a specified time

1. Create an Empty File

The following touch command will create a new empty (zero-byte) file named sheena.

touch testfile1.txt

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command2. Create Multiple Files

The touch command can also create multiple files. For example, the following command will create three files named testfile2.txt, testfile3.txt, and testfile4.txt.

touch testfile2.txt testfile3.txt testfile4.txt

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command3. Change File Access and Modification Times

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

touch -a touchfile

The most commonly used Linux commands (such as the find command and ls command) utilize timestamps to list and locate files.

4. Avoid Creating New Files

Using the -c option in the touch command can prevent the creation of new files. For example, if a file named touchfile1 does not exist, the following command will not create that file.

touch -c touchfile1

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command5. Change the Modification Time of a File

If you want to change the modification time of a file named touchfile, use the -m option in the touch command. Note that it will only update the last modification time of the file (not the access time).

touch -m touchfile

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command6. Set Access and Modification Times

You can explicitly set the time using the -c and -t options in the touch command. The format is as follows:

touch -c -t YYDDHHMM touchfile

For example, the following command sets the access and modification date and time of the file touchfile to (June 10, 2025) 17:30 (5:30 PM).

touch -c -t 2506101730 touchfile

Then use the ls -l command to verify the access and modification times of the file touchfile.

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command7. Use the Timestamp of Another File

The following touch command with the -r option will update the timestamp of the file touchfile using the timestamp of the file testtouch. Thus, both files will have the same timestamp.

ls -l *touch*
touch -r touchfile testtouch

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command8. Specify the Creation Time of a File

If you want to create a file with a specified time instead of the current time, the format should be:

touch -t YYMMDDHHMM.SS touchfiletime

For example, the following touch command with the -t option will timestamp the file touchfiletime to 17:30:25 on July 1, 2025.

touch -t 202507011730.25 touchfiletime

Chapter 2: Basic Linux Commands, Practical Examples of the [touch] Command

Conclusion

Through these 8 practical examples, you should have mastered the essence of the <span>touch</span> command—it is not only a quick tool for creating files but also a master of timestamp manipulation. Whether it is batch creating blank documents or precisely modifying file time attributes, the <span>touch</span> command can handle it with ease.

🔑 Key Techniques Review

  • Use <span>-t</span> to create files across time
  • Use <span>-c</span> to avoid accidentally creating new files
  • Use <span>-r</span> to synchronize file timestamps
  • Use <span>-a</span>/<span>-m</span> to precisely modify access/modification times

Time Management Proverb“In the world of Linux, mastering timestamps is mastering the memory of files”

Now open the terminal and try using <span>touch</span> to inject new time memories into your files! What interesting scenarios have you encountered while using it? Feel free to share your time manipulation stories in the comments section~

Recommended Reading:

👉 1. Chapter 2: Basic Linux Commands, Practical Examples of the [cp] Command

👉 2. Chapter 2: Basic Linux Commands, Practical Examples of the [mv] Command

Please open in the WeChat client

Leave a Comment