The Linux user often uses the touch command as a way to create a text file in the current working directory, but this tool is not limited to that; it’s way bigger than creating a text file.
The official description for touch command is “a tool that can change a file’s access and modification times (atime, mtime),” and that is what it does and is built for.
In this article, you will learn how to use the touch command for changing file access time, file modification time, replicating another file timestamp, and much more.
Tutorial Details
Description | Touch |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | touch |
Internet Required | No |
Syntax of the Touch Command
The touch command takes two arguments: one is the option, and the other is the file name.
$ touch [OPTION] [FILE]
The following are known options for this command:
Options | Description |
---|---|
-a | Change only the access time. |
-c | Do not create a file if one does not exist. |
-d | Change a timestamp using the date string. |
-f | deprecated (it used to force changes in older versions of BSD). |
-h | Change the timestamp to a symbolic link instead of a referenced file. |
-m | Change only the modification time. |
-r | Replicate the existing file timestamp to the referenced file. |
-t | Specify a custom date and time for the referenced file. |
Key Note of Touch Command
While working with this command, you must be aware of the following things:
- Access time or atime changes when a command reads the file content, like grep and cat do. To view the access time changes, use the ls command with the “
-lu
” flag. - Change time or ctime changes when a modification in file property happens, like renaming the file, changing the permissions, or moving the file. Use the “
ls -lc
” command to view the ctime of the file. - Modification time or mtime is changed while modifying the content of the file; to view the mtime, use the “
ls -l
” command.
Keeping that in mind, let’s move to the examples.
Creating a New File
The touch command without any options will create an empty file with the specified name.
$ touch data.txt
Output:
If the specified file is already present, the touch command will only update the timestamp, keeping the permissions and file content unchanged.
Creating Multiple Files
Specify multiple filenames, using a space as a separator, to create them in your working directory.
$ touch data1.txt data2.txt data3.txt
Output:
Creating a Range of Files
Instead of specifying multiple filenames, you can specify the range of files using an integer as order inside the “{}
” curly bracket with “..
” two dots specifying the start and end of the range, as shown.
$ touch data{1..5}.txt
The above command will create five files in “data1.txt
“, “data2.txt
“, .., “data5.txt
” order.
There are a few more patterns that you can follow to create a sequence of files.
$ touch data{5..10}.txt #"data5.txt", "data6.txt", .., "data10.txt"
$ touch data{20..15}.txt #"data20.txt", "data19.txt", .., "data15.txt"
$ touch data{a..j}.txt #"dataa.txt", "datab.txt", .., "dataj.txt"
$ touch data{j..z}.txt #"dataj.txt", "datak.txt", .., "dataz.txt"
$ touch data_{1..10}.txt #"data_1.txt", "data_2.txt", .., "data_10.txt"
$ touch data_{a..j}.txt #"data_a.txt", "data_b.txt", .., "data_j.txt"
Note that you cannot combine the numbers and letters to create a sequence of files, and special characters like “$
“, “/
“, “\
“, etc., are not allowed to be used as a sequence of files.
How to Avoid Creating a New File (if it does not exist)
The “-c
” flag can be used with other flags (that modify the timestamp of a target file) to modify the changes only for an existing file; if it does not exist, do not create a new file.
$ touch -c data.txt
Output:
Changing the File Timestamp Using a Date String
The “-d
” flag will make it easier for you to change the timestamp of a referenced file using the date string.
$ touch -d "-1 hour" data.txt
The above command sets the time on a referenced file to one hour in the past.
The following are a few examples (accepted in human-readable format) that you can use as a date string.
- Set the timestamp to 1 hour in the past using “
-1 hour
“. - Calendar dates, such as
20 November 2022
. - Time of day, such as
5:20am
or10:30pm
. - Days of the week, such as
Sunday
,Monday
, etc. - Relative times, such as
6 years ago
,yesterday
,next monday
, etc.
Changing the File Access and Modification Time
The “-a
” flag will modify the last access and modification times of a referenced file with the current date and time; if the file does not exist, it will create a new one (use the “-c
” flag to avoid this).
$ touch -a data.txt
Output:
Explicitly Changing the Access and Modification Time
Unlike the previous command, use the “-ct
” flag to explicitly set your desired time instead of the current for the referenced file.
For example, the following command will set the access and modification times for the “data.txt
” file as “16:30 pm
” on November 20 of the current year (2022).
$ touch -at 11201630 data.txt
Output:
The following is a breakdown of the (11201630
) timestamp format used in the above command:
11
: Referring to the month in its numeric value (11 = November).20
: It refers to day 20.16
: It will be used as an hour in a 24-hour format.30
: It will set the minute.
Changing the File Modification Time
The “-m
” flag will only change the modification time (not access time) of the referenced file.
$ touch -m data.txt
Output:
Explicitly Changing the File Modification Time
Use the “-mt
” flag to explicitly set the custom modification time for the referenced file instead of the current date and time.
For example, the following command will set the modification time for the “data.txt
” file as “16:30 pm
” on November 20 of the current year (2022).
$ touch -mt 11201630 data.txt
Output:
The following is a breakdown of the (11201630
) timestamp format used in the above command:
11
: refers to the month in its numeric value (11 = November).20
: refers to day 20.16
: It will be used as an hour in a 24-hour format.30
: It will set the minute.
Replicating the Existing File Timestamp to the Referenced File
The already existing file timestamp can be copied to the referenced file using the “-r
” flag.
$ touch -r olddata.txt data.txt
The above command will set the “olddata.txt
” file timestamp to the “data.txt
” file.
Updating the Symbolic Link Timestamp
The “-h
” flag will change the timestamp of the symbolic link without affecting the original file.
$ touch -h symbolic_data.txt
Output:
Here comes the end of this article.
I hope this article will help you understand how useful this command is.
If you have any question or query regarding this topic, feel free to ask it in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.