Introduction
The touch
command's primary function is to modify a timestamp. Commonly, the utility is used for file creation, although this is not its primary function. The terminal program can change the modification and access time for any given file. The touch
command creates a file only if the file doesn't already exist.
This tutorial explains how to use the touch
command with basic and advanced options.
Prerequisites
- A system running Linux.
- Access to the command line/terminal.
- Basic terminal commands, such as
ls
.
touch Command Syntax
The fundamental syntax for the touch
command is:
touch <options> <file or directory name>
The touch utility works without any options or with multiple options for advanced queries. Some options have a long and short format. If an option requires additional information, then the data is mandatory for both long and short forms.
touch Command Options
Below is a reference table for all available touch
command options:
Option | Description |
---|---|
-a | Changes the access time. |
-c --no-create | Avoids creating a new file. |
-d=<string> --date=<string> | Changes a timestamp using a date string. |
-f | No effect. In older BSD's the option forces changes. |
-h --no-dereference | Changes a symbolic link's timestamp. |
-m | Changes the modification time. |
-r=<file> --reference=<file> | Changes a timestamp to the referenced file's timestamp. |
-t <stamp> | Modifies a timestamp, where the stamp is the date/time format. |
--help | Opens the help menu. |
-v --version | Prints the program version. |
Linux touch Examples
When working with files in Linux, there are three timestamps to be aware of:
1. Access time or atime changes when a command reads the file's contents, such as grep or cat. The ls -lu
command displays the atime for files.
2. Change time or ctime changes when a file's property changes, such as renaming files, modifying file permission, or moving the file. The ls -lc
command shows the ctime for files.
3. Modification time or mtime changes when a file's contents change. The ls -l
command shows the mtime for files.
The examples below are all run from the terminal and demonstrate how to use the Linux touch
command with various options and what output to expect.
Create File
The simplest way to use the touch
command is without any options:
touch <filename>
If a file does not exist, touch creates the file. For example, to create a file called test, run:
touch test
List directory contents to see the file using the ls command.
If the file already exists, touch changes the timestamp to the current time.
The file contents and permissions stay unchanged.
Create Multiple Files
The touch
command can create multiple files as well. To do so, list the filenames separated by spaces:
touch <filename> <filename>
For example:
touch test1 test2
A useful way to apply touch
is to create large batches of files. To do so, add curly braces and indicate the first and last element in addition to the filename:
touch <filename{<start>..<finish>}>
For example, to create ten files with appended numbering, run:
touch test{1..10}
The command also works with letters. For example:
touch test_{a..j}
Important: The command cannot combine numbers and letters.
Set Specific Timestamp
Use the touch
command to set a specific timestamp for an existing file, for example:
touch -t <timestamp> <filename>
The timestamp format follows a specific pattern:
[[CC]YY]MMDDhhmm[.ss]
CC
- the first two digits for a yearYY
- the last two digits for a yearMM
- the monthDD
- the dayhh
- the hourmm
- the minutesss
- the seconds
The digits in the square brackets are optional. When using the two-digit year format, setting YY
to any number between 0-68 automatically assumes CC
is 20, whereas 69-99 assumes CC
is 19.
For example, to change the timestamp for a file called test to midnight January 1st, 1999, run:
touch -t 199901010000 test
Use the --full-time
option with ls
to see timestamp details.
Set File Timestamp Using Date String
The touch
command uses the -d
option to set a timestamp using a date string. The syntax is:
touch -d <string> <filename>
The date string is a flexible time format and accepts many different human-readable textual forms. Some examples include:
- Calendar dates, such as
19 August 2020
. - Time of day, such as
9:27pm
or8:02am
. - Days of the week, such as
Sunday
,Monday
, etc. - Relative time, such as
5 years ago
,yesterday
,next tuesday
, etc.
For example, change the timestamp using the -d
option to tomorrow
:
touch -d tomorrow test
To see a complete list of the possible string input options, visit the Date input formats GNU documentation.
Change Access Time to Current
Use the -a
tag to change a file's access time. The general syntax is:
touch -a <filename>
For example, to show a file's access time, run:
ls -lu
Next, change the access time for the file named test with:
touch -a test
Lastly, view the changed time by running:
ls -lu
The access time changes to the current timestamp.
Change Access Time Explicitly
Modify the access time to a specific timestamp by combining the -a
and -t
options:
touch -at <timestamp> <filename>
Check the access time for files before changing it:
ls -lu
Change the access time for the file test to midnight January 1st, 1999, by adding the timestamp:
touch -at 9901010000 test
Lastly, check the access time after the change:
ls -lu
After running the command, the access time changes to the value set with the -t
tag.
Change Modification Time to Current
The touch
command offers an option to change the modification time. The basic syntax is:
touch -m <filename>
As an example, check the file's mtime before changing the timestamp:
ls -l
Next, change the modification time for the test file:
touch -m test
Lastly, check the mtime after the change:
ls -l
The -m
option changes the modification time to the current timestamp by default.
Change Modification Time Explicitly
Combine the -m
option with -t
to explicitly state the modification timestamp. The general syntax is:
touch -mt <timestamp> <filename>
Check the file's mtime before changing it:
ls -l
Change the modification time to midnight January 1st, 1999, by running:
touch -mt 9901010000 test
Lastly, recheck the modification time:
ls -l
Adding the -t
option updates the modification time to a specific value.
Change Both Modification and Access Time
The touch utility allows changing the modification and access time with a single command. To do so, run:
touch -am <filename>
Before changing the atime and mtime, check it with:
ls -lu
ls -l
Next, change both times for the test file to the current timestamp:
touch -am test
Check the atime and mtime after the change:
ls -lu
ls -l
The combined options change both times in one go to the current time. Combine further with the -t
tag to state an explicit timestamp.
Avoid Creating a New File
By default, touch generates a new file if it doesn't exist. However, certain situations require overriding this functionality. Add the -c
option to avoid creating a new file when invoking the touch
command:
touch -c <filename>
For example, try to run touch
with the -c
option with a non-existent file:
touch -c new_test
List directory contents to confirm the file is not there:
ls -l
On the other hand, if the file does exist, the touch
command performs supplied operations on the existing file as usual.
Set Timestamp Using a Reference File
The touch
command offers a useful option to change a file's timestamp based on another file's timestamp.
To perform such a change, run:
touch -r <reference file> <file>
For example, create a new file and reference the timestamp of an existing test file:
touch -r test new_test
Check the timestamp for both files with:
ls -l
The new_test file inherits the timestamp from the test file.
Set Timestamp Using a Symbolic Link
The touch
command allows changing the timestamp for symbolic links without changing the referenced file's timestamp. Use the -h
option to modify the time for a symbolic link:
touch -h <filename>
For example, check the time for an existing symbolic link before any changes:
ls -l
Change the timestamp for the symbolic link to the current time:
touch -h link
Lastly, recheck the timestamp to confirm the change:
ls -l
Without the -h
option, the touch
command only changes the test file's timestamp.
The symbolic link's timestamp stays unchanged in this case.
Conclusion
The touch utility is one of the primary terminal programs when working with files in Linux. The tutorial outlined some typical use-cases for the touch
command. Next, check out our Linux commands cheat sheet, which features the touch
command.