The Linux operating system has multiple objects, such as application files, directories, system files, and binary files. These objects contain metadata information along with the files, which include create, access, and modification time. Sometimes, you may need to update these timestamps and the Linux touch command is a way to do it.
In this tutorial, we will learn about the touch command line utility in Linux.
- A Linux computer with terminal access.
- Understanding of Linux command-line interface.
- Excitement to learn new Linux commands.
Linux Touch Command
The Linux touch command is primarily used to modify a file timestamp. It can also be used to create an empty file. When you work on your Linux computer, you may need to create an empty file so that applications can write to it. Also, certain administrative services may require files to have a certain timestamp. The touch command in Linux can do both of these jobs. This command does not change file contents.
Touch can update the access and modification time of the specified file. However, if the specified file does not exist, then touch creates a new file with the specified name. It creates an empty file and you can add data to it later using any text editor. To update access and modification time, the file must exist on your system.
Before we dive deep into this command, let’s understand what three timestamps in Linux are:
- Access time (atime) – The last time when some command or application accessed the file.
- Modify time (mtime) – The last time when the content of the file was modified.
- Change time (ctime) – The last time when any attribute or content of the file was changed. Change time can be updated without updating the content.
Use the stat command in Linux followed by the file name to display the file timestamps.
Touch Command Syntax
The basic syntax of the touch command is:
touch [options] file_name
Linux Touch Command Options
The touch command supports various options to perform different operations. The following table describes the most commonly used options.
|Updates only the file’s access time of an existing file.|
|Avoids creating a new file even if the file does not exist.|
|Updates a timestamp using the specified time and date string.|
|Updates timestamp of a symbolic link.|
|Updates the file modification date and time of an existing file.|
|Updates a timestamp to the timestamp of the referenced file.|
|Updates a timestamp if the stamp is in the data and time format.|
How to Use the Touch Command
The touch command can be used with or without any options. When used without any option, the command creates an empty file if the file does not exist. If the file exists, then this command only changes the last access and modify timestamps to the current system time. For example, if the file file1 does not exist, the command will create it:
However, if the file file1 already exists, it does not overwrite the file, but will only update the timestamps.
When used with options, the touch command modifies the file timestamps based on the provided options. The touch command cannot open, close, or save a file. You can open the file created by the touch command using any text editor.
Create an Empty File
To create a new empty file using the touch command, specify the file name without any options:
Similarly, you can also use the following touch command to create multiple empty files:
touch bob tom maria
Create a Series of Empty Files
To create multiple files based on the specified number series, type:
The command also enables you to create a series of empty files based on the specified character series:
Avoid Creating a New File
By default, the touch command creates a new empty file if it does not exist. However, you can use the -c option to avoid creating a file:
touch -c mytestfile
Create a File Using a Specified Time
To create a new empty file using the specified timestamp different than the current system time, then use the following format:
touch -t YYYYMMDDHHMM.ss fileName
- YY denotes the first two digits of the year.
- YY denotes the last two digits of the year.
- MM denotes the month.
- DD denotes the day of the month.
- HH denotes the hour.
- MM denotes the minutes
- ss denotes the seconds
To create a file named mat with both the access time and modify time of 15 Aug 2021 at 12:30:45, use the following command:
touch -t 202108151230.45 mat
Change File Access and Modification Time to Current
To change the file access time of the existing file to the current system time, use the following command:
touch -a tom
To change the only modification time of the existing file to the current system time, use the following command:
touch -m bob
Change Only the Access Time Explicitly
To change the file access time explicitly of the file tom to 15 Aug 2021 14:25:21, use the following command:
touch -at 202108151425.21 tom
Similarly, you can also change only the modification time explicitly using the -am option.
Use the Timestamp of another Reference File
To change the access and modification time of a file by using the access and modification timestamp information from another file on your system, use the following command:
touch -r mynewfile tom
In this tutorial, we learned how to use the touch command in Linux. Mostly I used it to create empty files and change timestamp on a file.
Please leave your comments on how often you use touch command and what you use it for.