Menu

Use touch Command to Create and Modify Files in Linux

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>

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> …

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.

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.

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.

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.