Linux touch Command Tutorial

Linux provides the touch command in order to create files, change, and modify timestamps. The most popular use case for the touch is creating an empty file where the data or content will be put later.

Create A File with touch Command

The touch command can be used to create a file. This file will be empty and the access time stamp will be the current time. In this example we will create a file named “myfile”.

touch myfile

The ls command can be used to list current file list information.

Create A File with touch Command

From the output we can see that the newly created file is empty which size is 0 . Also the access time is 16:45 which is also the creation time.

Create Multiple Files with touch Command

The touch command can be also sued to create multiple files. All of these created files will be empty too. We will just provide the file names separating them with spaces. In the following example we will create empty files named file1 , file2 , file3 .

touch file1 file2 file3
Create Multiple Files with touch Command

Update/Change File Access Time

Another useful feature of the touch command is the ability to change an existing file access time. This option do not creates a new file only updates existing file access time. The -a option and the file name we want to update access time is used with this command.

touch -a myfile

Update/Change File Modification Time

The file modification is the operation of changing the file data or content. When the file data or content is changed the modification time is updated automatically. But but using the -m option without a data or content change the modification time can be set as now.

touch -m file1

Update File Access and Modification Time

Both the access and modification times can be updated or changes into the current time by using both -a and -m options like below.

touch -a -m file1

Copy/Use Another File timestamp

Another useful feature of the touch command is the ability to use specified file access and modification times into another file. The -r option is used to specify the source file which access and modification time will be used and the last parameter the file we want to set the access and modification time. In the following example, we will use the source_file access and modification times and set it to the destination_file.

touch -r source_file destiation_file

Create A File with Specified Time Stamp

We can use the touch command to set specified access time stamp explicitly while creation it. The -t option is used and the access time stamp is provided as YYMMDDHHMM format where YY is year MM is month, DD is day, HH is hour and MM is minute.

touch -t 2012231825 newfile
READ  How To Generate SSH Public/Private Keys?

Leave a Comment