Signup/Sign In

Use touch command to Create a File, Change a File, and Modify Timestamps

touch - In Linux/Unix operating system touch command is used to create, change and modify timestamps of a file.

The general syntax of touch command.

touch [OPTION]... FILE...

Brief description of options available with the touch command.

Options Description
-a change only the access time
-c, --no-create do not create any file
-d, --date=STRING parse STRING and use it instead of the current time
-f ignored
-h, --no-dereference affect each symbolic link instead of any referenced file
(useful only on systems that can change the timestamps of a symlink).
-m change only the modification time.
-r, --reference=FILE use the file's times instead of the current time.
-t STAMP

use [[CC]YY]MMDDhhmm[.ss] instead of current time.

--time=WORD change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m.
--help display this help and exit.
--version output version information and exit.

Example: Create a file using the touch command.

In this example, a new file is created without content using the touch command touch <file name>

touch command screenshot tutorial in Linux terminal shell

Example: Create multiple files at a time using the touch command.

In this example, using the touch command you can create multiple files at a time touch <first file> <second file> <third file> ...

touch command screenshot tutorial in Linux terminal shell

Example: change the last access time of a file.

In this example, Using -a (access time) option you can modify the last access time of a file. Here the date command is used to display the current date and time. you can see that after using -a option with the touch command the last access time is changed.

touch command screenshot tutorial in Linux terminal shell

Example: Use of -c option and check file is created or not. This option avoids creating a new file.

In this example, newfile.txt is not available in hope directory and after using -c option with the touch command you can see that newfile.txt is not created.

touch command screenshot tutorial in Linux terminal shell

Example: Use another file time instead of the current time.

In this example, you can see the time of the available file and details using the ll command. After using touch --reference=newfile.txt main.c time of main.c file charged to the newfile.txt file instead of the current time.

touch command screenshot tutorial in Linux terminal shell

Conclusion

In this tutorial, we covered how to create, change, and modify timestamps of a file in the Linux operating system using touch command with available options and suitable examples.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.