How the touch Command works on Linux
📣 Sponsor
The touch command lets us create files, or update the access or modified date of a file. It was first created in 1979 by AT&T Bell Laboratories. Today, on Linux and Unix based systems, touch can be accessed via the terminal.
In this guide, let's look at how touch
works. The syntax for touch
can be seen below, where x
is the name of the file we want to interact with or create, and [OPTIONS]
are optional settings we can include:
touch [OPTIONS] x
Creating a file on Linux or Mac from Terminal
If you want to make a file on Linux or MacOS from the terminal, we just have to use the touch
command by itself. For example, touch my-file.txt
will create a file called my-file.txt in whatever directory you are currently in.
If you need help changing directories, read our article on the cd command.
touch my-file.txt
If you want to make multiple files on Linux or MacOS, then use the touch
command, and separate each by spaces.
touch my-file.txt my-file-2.txt
If you want to create multiple numbered files in a certain file format on Linux or MacOS, use the touch
and use curly braces for the numbering. For example touch my-file-{1..5}.txt
will create my-file-1.txt through to my-file-5.txt.
touch my-file-{1..5}.txt
Changing the access/modification time of a File on Linux or Mac
If you need to change the access or modification time of an existing file to the current time, you can run the same command on an existing file. This will update the access and modification times of the file:
touch my-file.txt
If we want to update the access time and modification time separately, we use the -a
option for access time, and the -m
option for modified time.
for example, the below code will update the modification time only:
touch -m my-file.txt
Updating the timestamp of a file only if it exists on Linux or Mac
If we only want to update the timestamps of a file if the file exists on Linux or Mac, and not create it if it doesn't, we need to use the -c
option.
For example, the below code will update a file's timestamp should it exist to the current time, but it will do nothing if it doesn't:
touch -c my-file.txt
This can be combined with the -a
and -m
options to only update access or modification time respectively:
touch -ca my-file.txt
Setting a specific timestamp for a file on Linux or Mac
If we want to set a specific time and date on a file, we can do that using the -d
command (or --date=
).
touch time format on MacOS
On the latest versions of MacOS, the time format has to be in the form YYYY-MM-DDThh:mm:SS
. For example, 1993-03-2609:44:00
sets the access and modified date to 26 March, 1993 at 9:44:00:
touch -d '1993-03-2609:44:00' my-file.txt
touch time format on Linux
On Linux based systems, the touch time format is [YY]YYMMDDhhmm[.ss]
. That means we can set the modified and access dates of a file by using touch -d '[YY]YYMMDDhhmm[.ss]' file.txt
. The first two digits of the year, and the seconds are optional.
Typically it is easier to set the year as the full, 4 digit year code, however the first two digits of the year are optional. If only two year digits are given, then most systems assume 70 to 99 means 1970 - 1999, and 00 to 37 mean 2000 - 2037. This will presumably change as time goes on, so it is best to stick to 4 digits years.
As an example, the below code sets the access and modified dates for my-file
to 26 March, 1993 at 9:44:00.
touch -d '199303260944.00' my-file.txt
Combining specific timestamps with other options in the touch command
As you might expect, you can combine this with other commands. For example, touch -ad
sets the access date, and touch -md
sets the modified date of a file.
Updating the access/modification time of a symlink in Linux or Mac
By default, if you try to update the access or modification time of a symlink, it will also update the reference file too. To only update the symlink itself, use the -h
option. For example, the below code updates both the access and modification time for a symlink called mySymLink
:
touch -h mySymLink
This can be combined with other options, too. For example, this will update only the symlink access time to the current time:
touch -ha mySymLink
Setting the timestamp of a file to match another file on Linux or Mac
You can also set a timestamp of a file to match another file's timestamp using the -r
option. For example, if you want new-file.txt
to have the same timestamp as old-file.txt
, we would write the following:
touch -r old-file.txt new-file.txt
More Tips and Tricks for Linux
- How the cat Command works on Linux
- How the alias Command works on Linux
- How to Kill a Process Running on a Port
- How the chmod command works on Linux
- Reference: Non Printable Characters List
- How the cd command works in Linux
- How the mv Command works on Linux
- How the mkdir command works on Linux
- How to count all files in a directory in Linux
- How to Rename Files in Linux and MacOS Terminal