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.
- A system running Linux.
- Access to the command line/terminal.
- Basic terminal commands, such as
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:
|Changes the access time.|
|Avoids creating a new file.|
|Changes a timestamp using a date string.|
|No effect. In older BSD's the option forces changes.|
|Changes a symbolic link's timestamp.|
|Changes the modification time.|
|Changes a timestamp to the referenced file's timestamp.|
|Modifies a timestamp, where the stamp is the date/time format.|
|Opens the help menu.|
|Prints the program version.|
Linux touch Examples
When working with files in Linux, there are three timestamps to be aware of:
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.
The simplest way to use the
touch command is without any options:
If a file does not exist, touch creates the file. For example, to create a file called test, run:
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
touch command can create multiple files as well. To do so, list the filenames separated by spaces:
touch <filename> <filename>
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:
For example, to create ten files with appended numbering, run:
The command also works with letters. For example:
Important: The command cannot combine numbers and letters.
Set Specific Timestamp
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- the first two digits for a year
YY- the last two digits for a year
MM- the month
DD- the day
hh- the hour
mm- the minutes
ss- 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
--full-time option with
ls to see timestamp details.
Set File Timestamp Using Date String
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
- Days of the week, such as
- Relative time, such as
5 years ago,
next tuesday, etc.
For example, change the timestamp using the
-d option to
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
-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:
Next, change the access time for the file named test with:
touch -a test
Lastly, view the changed time by running:
The access time changes to the current timestamp.
Change Access Time Explicitly
Modify the access time to a specific timestamp by combining the
touch -at <timestamp> <filename>
Check the access time for files before changing it:
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:
After running the command, the access time changes to the value set with the
Change Modification Time to Current
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:
Next, change the modification time for the test file:
touch -m test
Lastly, check the mtime after the change:
-m option changes the modification time to the current timestamp by default.
Change Modification Time Explicitly
-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:
Change the modification time to midnight January 1st, 1999, by running:
touch -mt 9901010000 test
Lastly, recheck the modification time:
-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 -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:
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
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:
The new_test file inherits the timestamp from the test file.
Set Timestamp Using a Symbolic Link
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:
Change the timestamp for the symbolic link to the current time:
touch -h link
Lastly, recheck the timestamp to confirm the change:
-h option, the
touch command only changes the test file's timestamp.
The symbolic link's timestamp stays unchanged in this case.
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