How to Use Touch Command in Linux

Black And Sliver Laptop With Mobile Feature Image Touch Command

Want to create a file quickly, but don’t want to open a text editor or navigate through a graphical interface? Look no further than the touch command. This command is useful for creating empty files and updating file timestamps, which is key for managing backups and executing scripts effectively.

This guide will explore how to use the touch command in Linux to create single or multiple empty files, or adjust timestamps for scripting needs.

How The Touch Command Works

In UNIX/Linux systems, the touch command is often used to quickly create empty files. Additionally, you can use it to modify the timestamps – specifically the access and modification times of files that already exist.

If the file doesn’t exist, touch will generate it, though without any content. Moreover, its ability to modify timestamps makes it invaluable in scripting and managing file systems.

The syntax of the touch command is as follows:

touch [options] [file_name]

Create an Empty File

Creating an empty file with the touch command is very simple. Just use the touch command without any flags/options:

touch newfile.txt

To verify the creation of the file, you can check its file size using the stat command:

stat newfile.txt
Checking the file size of the empty file using the stat command.

In the output, look for the Size field. If it shows 0, the file is empty.

Furthermore, if the file already exists, the touch command updates its access (atime) and modification (mtime) times to the current time without impacting the file’s contents or permissions.

Create Multiple Empty Files

What if you need more than one empty file? With touch, you can make multiple files at once. For example, let’s create multiple files that are separated by spaces, like this:

touch file1.txt file2.txt file3.txt

You can verify the creation of these multiple empty files by running this:

ls -l *.txt
Creating and checking the size of the multiple empty files.

You can also create a series of files with numbered or lettered names using curly braces.

Let’s create multiple files with numbered names:

touch file{1..3}.txt

Confirm this by viewing your files in the specified directory.

Viewing multiple empty files in the specified directory.

Similarly, you can also create files with lettered names:

touch file_{a..e}.txt

Note: The touch command can’t mix numbers and letters in the same brace expansion.

Touch Command Options

As mentioned earlier, that touch command isn’t just about creating files – it’s also a tool for managing file timestamps. To manipulate these timestamps, you need to know how touch command options work. Let’s take a look at them:

OptionDescription
-aUpdates the file access time only.
-cPrevents the creation of a new file if it doesn’t already exist.
-d=, –date=Modifies a file’s timestamp using a specified date string.
-mUpdates only the file modification time.
-rUses the atime and mtime from a reference file.
-tModifies a file’s timestamp using a specified date and time.

Changing Access Time of File

Access time, or atime, is the timestamp that updates whenever a file’s contents are accessed using command-line tools like grep or cat, or text editors such as vim or nano.

By default, touch updates both access and modification times. However, if you only want to change the access time, you can use the -a option with the touch command. This is particularly useful for simulating file usage or adjusting logs for certain operations.

Before modifying the access time, let’s first display the current access time of the file we’re going to change:

Access time of the specified file before modifying.

Now, you can change the access time of a specified file using touch -a command:

touch -a happy.txt

Moreover, the above command will update the access time of the specified file to the current time without affecting the mtime.

Let’s verify the change by running:

ls -lu
Viewing access time of the specified file in terminal window after modification.

Furthermore, you can also set a specific access time by combining the -a and -t options. For example, to change the specified file access time to midnight on June 1st, 2010:

touch -at 201006010000 happy.txt

Here, the -a option changes the access time and the -t option specifies the time in the format [[CC]YY]MMDDhhmm[.ss].

Again, to verify the changes, you can use this command:

ls -lu
Assigning specific access time to a file using touch command options.

Changing Modification Time

The modification time, or time, is the timestamp that changes whenever the contents of a file are modified. However, it doesn’t track changes related to file permissions or ownership.

To update only the last modification time of a file without altering the access time, you can use the -m option of touch command:

touch -m happy.txt

To verify the changes, use the stat command:

stat happy.txt
Displaying the modification time after changing it using the -m option.

This changes the mtime to the present moment while leaving the access time, or atime untouched.

If you need to set a specific timestamp for the modification time, you need to combine the -m options with the -t option. For example, to set the modification time to 08:45 AM on June 19, 2014, run:

touch -mt 201406190845.00 happy.txt

Next, verify the updated modification time by running:

Stat happy.txt
Displaying the file modification time using the stat command.

Modify Modification and Access Time

There are instances where you might need to update both the atime and mtime simultaneously. To do that, use the touch command regardless of any option.

touch happy.txt

Running this command changes both the atime and mtime to the current time. Use this when you want to refresh a file’s timestamps to reflect recent interactions or alterations without changing the file’s content.

Furthermore, you can use the -a and -m option together with the touch command to modify both the access and modification times:

touch -am happy.txt

You can verify the updated times by running this:

stat happy.txt

Creating File Using Specified Time

Sometimes, you might want to set a file’s timestamp to a specific time and date rather than the current moment. The -t option followed by the desired timestamp in the format [[CC]YY]MMDDhhmm[.ss], allows you to do just that.

For example, if you want to set the access and modification times of a particular file to January 1, 2024, at 12:30 PM, you need to enter:

touch -t 202401011230.00 happy.txt

To confirm the modification, run:

stat happy.txt
Displaying the atime and mtime value after modifying it using the -t option of touch command.

You can also use the -r option to update a file’s timestamp to match that of another file. For example, let’s change the happy.txt file timestamp based on the new_file.txt timestamp.

But before modification, let’s view the modification time of the specified files:

ls -l
listing all the files with their modification time.

After that, change a file’s timestamp to match another file’s timestamp:

touch -r happy.txt new_file.txt

Verify the change with:

ls -l
Listing all the files along with their modification times after changing the timestamp with -r option.

Alternative to Touch Command

While touch is my go-to tool for quick file creation and timestamp manipulation, there are other methods you can use. Let’s explore a few of these alternatives.

Cat Command

You often use the cat command to display file contents, but it can also create files. By using cat with a redirection operator, you can generate an empty file:

cat > filename.txt

This command creates filename.txt and enters you in input mode. If you press Ctrl + D, it ends the input and leaves the file empty.

Redirection Operator

Redirection operators (>, >>) are another method for creating files. By using the > operator, you can create an empty file or overwrite an existing one:

>filename.txt

You can also use the >> operator, but it appends content to an existing file instead of creating a new one or leaving it empty.

Text Editor

There are many command-line text editors, such as nano, vim, or emacs, which allow you to create and edit files. Moreover, you can use these text editors to make an empty file.

For instance, to create an empty file with Vim, run:

vim file1.txt

When you open Vim, you’ll be in command mode by default. Press i to switch to insert mode, where you can type or edit content.

After entering insert mode, press Esc to return to command mode. Then, type :wq and hit Enter to save your work and exit Vim. And that’s it—you’ve created an empty file using Vim!

Wrapping Up

Linux offers several ways to manage files, but touch utility stands out for its simplicity and effectiveness. You can also learn how to perform other operation on Linux such as concatenating files, finding files, or removing files and others.

Image credit: Unsplash. All alterations and screenshots by Haroon Javed.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Haroon Javed
Haroon Javed - Contributor

Haroon is a lifelong tech enthusiast with over five years of experience writing thousands of articles about Linux, programming languages, and more. He loves exploring new technologies and experimenting with them to find innovative ways to use them. Haroon's work has been featured on various online platforms, including HTG, Baeldung, and LinuxHint.