How to Use Touch Command in Linux

Spread the love

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

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

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.

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:

Option Description
-a Updates the file access time only.
-c Prevents the creation of a new file if it doesn’t already exist.
-d=, –date= Modifies a file’s timestamp using a specified date string.
-m Updates only the file modification time.
-r Uses the atime and mtime from a reference file.
-t Modifies 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:

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

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

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

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

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

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

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

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.