Most useful examples of the touch command – Linux

Linux touch command examples

The touch command is the easiest way to create empty new empty files in Linux. It is also used to change the timestamps of existing files or directories. In this article, you’ll learn about the practical uses and examples of the touch command in Linux.

We have the following options available for the touch command, and remember that the options are very important to understand.

Options for touch command in Linux

OPTIONUSE
-aTo change the access time only
-cDo not create a new file if the mentioned file does not exist
-dTo update the access and modification time of the files/directories
-mTo only change the modification time of the file/directory
-rTo use the access and modification time of the file
-tTo create a file using a specified time
Available options for the touch command

The touch command looks like a very ordinary command in Linux, but in fact it has got some really very uses. Here are some useful examples of the touch command in Linux.


Examples of the touch command in Linux:


1. Create empty files in Linux

The following terminal command creates a new empty file file1.txt in Linux.

touch file1.txt


2. Create multiple files in Linux

Using the touch command, you can also create multiple files in Linux. The following command creates two files, File1.txt and file2.txt in Linux.

touch File1.txt file2.txt


3. Change file access time in Linux

You can change the access time of a file or folder in Linux using the touch command. Use the -a option to set the access time of the file to the current time. Also, if the file does not exist, it will create a new file with that name.

$ ls -l file1.txt
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Nov 26 18:52 file1.txt

Change the access time of the file called file1.txt using the following command:

touch -a file1.txt


And if you don’t want to create a new file in case the file does not exist, then use -c option to avoid creating new file.

touch -c -a test.txt


4. Change modification time of a file in Linux

You can change the modification time of a file without actually modifying anything in the file using the touch command. Use the -m option to set the modification time of the file to the current time. Also, if the file does not exist, it will create a new file with that name. And if you don’t want to create a new file, use the -c option to avoid that as mentioned in the above example. You can check the file modification time using ls -l command in linux:

$ ls -l file1.txt
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Nov 26 18:52 file1.txt

Now change the modification time to the current time using the following command:

touch -m file1.txt

And you can verify the changes in the modification time using the ls -l command:

$ ls -l file1.txt
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Nov 26 19:49 file1.txt

You can clearly see that the modification time got updated to the current time.


5. Set custom modification time of a file in Linux

Using the touch command, you can set custom modification time of a file. Use -c and -t options with the touch command with the following format:

touch -c -t CCYYMMDDHHmm.ss file_name

For example, the following command sets the modification time of file1.txt as July 11 at 19:15:31 of the 20th year of the 20th century.

touch -c -t 202007111915.31 file1.txt

And you can see the changes using the ls -l command:

$ ls -l file1.txt
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Jul 11 19:15 file1.txt

Or just by looking at the properties of that file:

6. Set timestamp of another file in Linux

You can set the timestamp of a file to another file in Linux using the touch command with -r option. For example, the following command updates the timestamp file2.txt with that of file1.txt using -r option with the touch command.

touch -r file1.txt file2.txt

You can check the modification details using ls -l command:

ls -l
total 0
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Jul 11 19:15 file1.txt
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Nov 26 19:07 File1.txt
-rw-rw-r-- 1 pyubuntu pyubuntu 0 Jul 11 19:15 file2.txt

If you use touch command for any other task, please let everyone know in the comments.

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

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