×


Linux Touch Command Examples - How it works ?

Touch command is basically used to create empty files and also changes the timestamps of existing files on Unix & Linux System. Modifying timestamps here means updating the access and modification time of files and directories.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux System Commands queries.

In this context, we shall look into a few practical examples of the Linux touch command.


What is the Basic syntax for Touch Command?

The touch command takes the following syntax:

$ touch [OPTION] [FILE]

Now, now let's check out a few touch command example usages.


How do i create an empty file in Linux ?

With the touch command, you can easily create an empty file with the below command:

$ touch [filename]

For example, if you create a file named file1.txt, then use the ls command we can see that the file has been created:

$ touch file1.txt


How do i create Multiple files in Linux ?

Also, you can create multiple files at a go by specifying the file names in one command as follows:

$ touch [filename1] [filename2] [filename3] [filename4]

For example, the common below creates four new files:

$ touch file1.txt file2.txt file3.txt file4.txt

This will create files named file1, file2, file3, and file4.


How to change file access time ?

If you would like to update the last access time of a file, use the touch -a command:

$ touch -a [filename]

After creating the file, you the use stat command to check the status of our directory with the following command:

$ stat [filename]

To avoid creating new files, when using the touch command with the c option can be used to check whether a certain file exists. If that file does not exist, touch will not create it. You will have avoided creating a new file. To implement this, use the command syntax below:

$ touch -c [filename]


How to change access and modification time in Linux ?

To update both the access and modification time use the touch command below:

$ touch -c -t YYMMDDHHMM fileName

For example:

$ touch -c -t 202106121830 file4.txt

Just to be sure, confirm the changes using the stat command as follows:

$ stat file4

You will notice that the access and modification time for file4.txt have both been changed to 2021-06-12 18.30:30.


How to Change the modification time of a file ?

If you want to change only the modification time of a file, use the touch -m command:

$ touch -m [filename]


How to use the timestamps of another file ?

The touch command with the -r option is used to apply the timestamp of one file to that of another file:

$ touch -r second_file_name first_file_name


How to Create a file using a specified time ?

The touch -t command is used to specify time when creating a file. The syntax for the command is:

$ touch -t YYMMDDHHMM fileName

For example,

$ touch -t 202106162228.30 file.txt

This will create File.txt with the specified time of 2021-06-16 22.28:30.


[Need assistance in fixing missing Software Packages Installation on Debian Linux System ? We can help you. ]


Conclusion

This article covers a few basic examples of the touch command. For more information use 'man touch' to view the manual page. In fact, the touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file.


Touch Command Options

  • -a to change the access time only.
  • -c if the file does not exist, do not create it.
  • -d to update the access and modification times.
  • -m to change the modification time only.
  • -r to use the access and modification times of file.
  • -t creates a file using a specified time.