Oct 17, 2023 4 min read

Linux Touch Command

Use the Linux touch command with our step-by-step tutorial. It is used to create new files or update the timestamp of existing files.

Linux Touch Command
Table of Contents

Introduction

Before we start talking about linux touch command, let's understand-What is Touch Command ?

The touch command in Linux is used to create new files or update the timestamp of existing files. It is a versatile command that allows users to modify file timestamps or create new empty files with specified names. The touch command is often used in shell scripting, automation, and managing file timestamps.

We can use the touch command to change the timestamps of existing files and directories, as well as create new, empty files.

In this tutorial, we'll show you how to use the touch command with real-world examples and extensive explanations of the most popular command option. We will also address a few FAQs on Linux Touch Command.

Linux Files Timestamps

Before we get into how to utilize the touch command, let's take a look at how Linux handles file timestamps.

In Linux, a file contains three timestamps:

  • atime (access time) - When the file was last accessed/opened by command or application like cat, vim, or grep.
  • mtime (modify time) - When the file's content was last changed.
  • ctime (change time) - When the attribute or content of the file was last changed. File permissions, file ownership, and file location are all examples of this property.

Use the stat command to see the file status, including timestamps.

stat file_name

Writing rights on the parent directory are required to create a new file. If you don't, you'll get a permission denied error.

How to Use the touch Command

If the file name supplied as an argument does not exist, touch will create a new file in its most basic form when called without any arguments.

The touch will update the file's last access and modification times to the current time if it already exists.

If the file file1 does not exist, for example, the following command will create it; otherwise, the timestamps will be changed:

touch file1

Specify the file names as parameters to create or alter multiple files at once:

touch file1 file2 file3

Use the -c (--no-create) option if you don't want the touch command to create new files.

If the file file1 exists, for example, the following command will modify the file timestamps; otherwise, it will have no effect:

touch -c file1

Changing only access or modification times

Touch will update the file's latest access and modification times to the current time if no option is selected. You can only alter one of these timestamps with the -a and -m parameters.

Change only the access time

To adjust simply the file's access time, use the -a option:

touch -a file1

Change only the modify time

To change the file's modification time, use the -m option:

touch -m file1

When the modify time is changed, the change time is updated as well.

Setting specific timestamps

We may also use the touch command to update or create a file at a different time than the current time.

To specify a date string instead of the current time, use the -d (--date=) option.

To modify both the latest access and modification times of file1 to 1 June 2018 11:02, for example, run the command:

touch -d '1 June 2018 11:02' file1

Single quotes must be used to encapsulate the date string.

You can also use the touch command with a partial date-time string. By providing merely the date, the year is automatically changed to the current one:

touch -d '12 June' file1

To specify a timestamp instead of the current time, use the -t option. The timestamp argument must be formatted as follows:

use [[CC]YY]MMDDhhmm[.ss]

The following command, for example, will set file1's last access and modification timings to 1 June 11:02 of the current year.

touch -t 06011102 file1

Using the timestamp of another file

We can specify a reference file and utilize its timestamps instead of the current time using the -r(--reference=) option.

For instance, the following command instructs touch to utilize file1 times for file2:

touch -r file1 file2

When you run the touch command on a symbolic link, the timestamps of the file it refers to are changed by default.

To change the symlink's timestamp, use the -h (--no-dereference) option.

To update the timestamps of the symbolic link symlink1, for example, type:

touch -h symlink1

FAQs for Linux Touch Command

How do I use the touch command? 

To create a new file, simply type touch filename. To update the timestamp of an existing file, use touch -c filename.

How does the touch command modify file timestamps?

By default, the touch command updates the access time (atime) and modification time (mtime) of a file to the current time. It can also create a file if it doesn't exist

Can I update only the access time or modification time with touch?

Yes, you can update either the access time or modification time by using the -a (access time) or -m (modification time) option followed by the filename. For example, touch -m filename updates only the modification time.

What happens if I touch a non-existent file?

If you touch a non-existent file, the touch command creates an empty file with the specified filename.

Can I use the touch command recursively on directories and subdirectories?

To use the touch command recursively on directories and subdirectories, you can combine it with the find command. For example, find . -type f -exec touch {} + will update the timestamps of all files in the current directory and its subdirectories.

Can I specify a specific timestamp to update a file with touch?

Yes, you can use the  touch -d option followed by a specific timestamp to modify a file's timestamp to that particular time and date. For example, touch -d "2022-07-01 15:30" filename sets the file's timestamp to July 1, 2022, at 15:30.

Can I change the timestamp of a file to a specific value using touch?

Yes, you can change the timestamp of a file to a specific value by using the -t option followed by the desired timestamp. The format should be [[CC]YY]MMDDhhmm[.ss]. For example, touch -t 202201011200.00 filename sets the file timestamp to January 1, 2022, at 12:00:00.

Conclusion

You should now be able to utilize the Linux touch command with confidence.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.