How the touch Command Works on Linux
Let's look at how the touch command works on Linux and MacOS.
Join the DZone community and get the full member experience.
Join For FreeThe 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, touch
can be accessed via the terminal on Linux and Unix-based systems.
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 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, 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 particular 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 an existing file's access or modification time 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 particular time and date on a file, we can use 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
The touch
time format on Linux-based systems is [YY]YYMMDDhhmm[.ss]
. We can set a file's modified and access dates 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 complete, four-digit year code. However, the first two digits of the year are optional. If only two-year numbers are given, then most systems assume 70 to 99 means 1970 - 1999, and 00 to 37 means 2000 - 2037. This will presumably change as time goes on, so it is best to stick to 4 digits years.
For example, the below code sets my file's access and modified dates 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. 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 file's timestamp 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
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments