How To Use the touch Command in Linux

November 22, 2021

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.

How To Use The touch Command In Linux

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:

OptionDescription
-aChanges the access time.
-c
--no-create
Avoids creating a new file.
-d=<string>
--date=<string>
Changes a timestamp using a date string.
-fNo effect. In older BSD's the option forces changes.
-h
--no-dereference
Changes a symbolic link's timestamp.
-mChanges 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.
--helpOpens 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
touch test terminal output

List directory contents to see the file using the ls command.

If the file already exists, touch changes the timestamp to the current time.

touch change timestamp terminal output

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
touch multiple files terminal output

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}
touch ten files terminal output

The command also works with letters. For example:

touch test_{a..j}
touch letter files terminal output

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 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
touch -t terminal output

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 or 8: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
touch -d terminal output

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
touch -a terminal output

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
touch -at terminal output

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
touch -m terminal output

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
touch -mt terminal output

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
touch -am terminal output

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
touch -c terminal output

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
touch -r terminal output

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
touch -h terminal output

Without the -h option, the touch command only changes the test file's timestamp.

touch symbolic link unchanged

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.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is an aspiring technical writer at phoenixNAP and a data nerd. Her background in Electrical Engineering and Computing and her teaching experience give her a unique set of skills - being able to easily explain complex technical concepts through her content.
Next you should read
14 Dangerous Linux Terminal Commands
November 17, 2021

It is always dangerous to run a Linux terminal command when you aren't sure what it does. This article lists 14 Linux commands that can have adverse effects on your data or system.
Read more
How to Change File Permissions Recursively with chmod in Linux
August 17, 2020

Setting file and directory permission properly is important in multi-user systems such as Linux. You can set permission recursively using the chmod or find...
Read more
How to Use DD Show Progress Command in Linux?
September 24, 2020

The dd is a command-line utility is used to convert and copy files on Unix and Unix-like operating systems. Learn how to use the dd command to...
Read more
How to Move Directories in Linux
September 8, 2021

This tutorial shows you how to move directories in Linux using the GUI or the built-in mv command.
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.