How to Use the Linux Command touch and Its 6 Options

Using the Linux Command touch allows you to change the access and modification times of a file or create an empty file. In this post, we will explore the basic usage of the touch command and its various options, as well as introduce some useful ways to utilize it.

What is the Linux Command touch?

The touch command is mainly used for two purposes. First, it is used to create new empty files. Second, it is used to change the access time and modification time of existing files. These functions are very useful in system management and script writing.

Basic Usage

The most basic usage of the touch command is as follows: you write the touch command followed by the option and the filename.

touch [option] filename
ShellScript

If the filename exists, the command updates the access and modification times of the file to the current time. If the filename does not exist, the command creates an empty file with that name.

Below is the result of creating a new file called new_file.txt.

Figure 1. Linux Command touch: Creating a new empty file
Figure 1. Linux Command touch: Creating a new empty file

Next, let’s check the access and modification times of an existing file.txt, update them to the current time, and verify the changes.

Figure 2. Linux Command touch: Updating the access and modification times of an existing file to the current time
Figure 2. Linux Command touch: Updating the access and modification times of an existing file to the current time

Main Options

The touch command has various options that allow you to manipulate the time information of a file more precisely. Let’s take a look at each of the main options.

-a (access: change only the access time)

If you need to change only the access time and leave the modification time unchanged, use the -a option.

touch -a filename
ShellScript

Using the -a option, you can see that only the access time is updated, while the modification time remains unchanged.

Figure 3. Linux Command touch: Updating only the access time of an existing file
Figure 3. Linux Command touch: Updating only the access time of an existing file

-m (modify: change only the modification time)

Contrary to the -a option, the -m option is used to change only the modification time while leaving the access time unchanged.

touch -m filename
ShellScript

In the figure below, you can see that the access time remains unchanged, but the modification time is updated.

Figure 4. Linux Command touch: Updating only the modification time of an existing file
Figure 4. Linux Command touch: Updating only the modification time of an existing file

-c (do not create: do not create the file if it does not exist)

The -c option prevents the creation of a new file if it does not exist. However, if the file exists, it updates the access and modification times as usual.

touch -c filename
ShellScript

-d (date: set a specific date)

The -d option updates the access and modification times to the specified date.

touch -d [YY]YYMMDD filename
ShellScript

Using a 6-digit or 8-digit year-month-day format, you can update the access and modification times of a file to the specified date.

Figure 5. Linux Command touch: Updating the date of a file
Figure 5. Linux Command touch: Updating the date of a file

Since the -d option sets only the date, the hour, minute, and second values are set to zero.

Next, let’s look at the -t option, which allows you to specify not only the date but also the hour, minute, and second.

-t (time: set a specific time)

The -t option allows you to set the access and modification times to the specified time. The time format is [[CC]YY]MMDDhhmm[.ss]. Here, CC is the century, YY is the year, MM is the month, DD is the day, hh is the hour, mm is the minute, and ss is the second. The parts enclosed in brackets are optional. If you omit YY, it defaults to the current year, and if you omit ss, it defaults to 0.

touch -t [[CC]YY]MMDDhhmm[.ss] filename
ShellScript

Let’s use the -t option to set the time of the file.txt to 3:45 PM on July 30, 2023. You can see that the access and modification times are updated to the desired time.

Figure 6. Linux Command touch: Updating the time of a file
Figure 6. Linux Command touch: Updating the time of a file

-r (reference: copy the time information from another file)

The -r option allows you to copy the time information from another file and set it to the target file.

touch -r reference_file target_file
ShellScript

In the example below, you can see that the access and modification times of target.txt are set to the same as those of file.txt. Note that the birth time and change time of the file are not affected.

Figure 7. Linux Command touch: Updating the time by referencing another file
Figure 7. Linux Command touch: Updating the time by referencing another file

Additional Tips: Wildcards

When you need to change the times of multiple files at once, it is convenient to use the touch command with a wildcard (*).

The following command sets the access and modification times of all files in the current directory to the current time.

touch *
ShellScript

Precautions

When using the touch command, there are a few precautions to keep in mind.

Time Format

When using the -t option, you must enter the time format correctly. If the format is incorrect, an error may occur, or an unexpected time may be set.

Permission Issues

You need appropriate permissions for the file to change its time information. If you try to use the touch command on a file without the necessary permissions, you will get a “touch: cannot touch ‘filename’: Permission denied” message.

In such cases, you can use the sudo command along with the touch command.

Figure 8. Linux Command touch: Permission issues
Figure 8. Linux Command touch: Permission issues

Summary

The Linux Command touch is a useful tool for easily managing the creation of empty files and time information in a Linux system. It provides a variety of options from basic file creation to detailed time settings, making it applicable in various situations. Proper use of this command can greatly aid in system management and script writing.

I hope this post helps you understand and effectively use the touch command’s basic usage, various options, and practical applications.

References

Leave a Comment