12 Ways The Touch Command In Linux Enhances File Management

Try this guide with our instant dedicated server for as low as 40 Euros

touch command

By providing your email address or using a single sign-on provider to create an account, you agree to our Terms of Service and that you have reviewed our Privacy Policy.

Thank you for contacting us! We will get in touch with you soon.

Do you know that Linux treats everything as a file or process? 

That’s why file management is a core OS capability in all Linux distributions. You will find several file management utilities and native commands to create and manage files. We will discuss the touch command in Linux, a file management utility you can find in almost all mainstream distributions.

At its core, the touch command in the Linux System is designed to alter the timestamps of files, including their access and modification times. 

While it’s often employed to create new files, it is actually a secondary feature of this versatile utility. The touch command is used to adjust the timestamps of files, but it will also bring a new file into existence if the specified file is not found in the directory.

In this guide, we’ll delve into the usage of the touch utility, covering both its fundamental functionalities and more sophisticated options to help you harness its full potential. But before that, let’s look at the idea of timestamps in the context of file management.

Table Of Contents

  1. The idea of File Timestamps in Linux
  2. The Linux touch Command Syntax
    1. The touch Command Options
  3. 12 Examples of Using the touch Command in Linux
    1. The Prerequisites
    2. Example #1: Create a New File
    3. Example #2: Create More Than One File in Linux
    4. Example #3: Set a Specific Timestamp
    5. Example #4: Assign File Timestamps with Date Strings
    6. Example #5: Update a File’s Access Time to the Current Time
    7. Example #6: Set a Specific Access Time
    8. Example #7: Update a File’s Modification Time to Current Time
    9. Example #8: Set a Specific Modification Time
    10. Example #9: Update Both Access and Modification Times Simultaneously
    11. Example #10: Prevent New File Creation with touch
    12. Example #11: Use a Reference File to Set Timestamps
    13. Example #12: Update Timestamps for a Symbolic Link File
  4. Conclusion
  5. FAQs

The idea of File Timestamps in Linux

In Linux file management, it’s important to be familiar with three key timestamps associated with files:

  • Access Time (atime): This timestamp updates whenever a file’s contents are read by commands like grep or cat. To view a file’s atime, you can use the ls -lu command. 
  • Change Time (ctime): This timestamp reflects changes to a file’s properties, such as when you rename files, alter file permissions, or move the file to a different location. The ls -lc command will display a file’s ctime.
  • Modification Time (mtime): This timestamp is updated when the actual contents of a file are modified. The ls -l command is commonly used to show a file’s mtime.

The Linux touch Command Syntax

 The basic syntax of the touch command is as follows:

# touch [options] [file name]

touch supports several flags and options that can modify the behavior of the utility. However, in most instances, the command operates smoothly without any options. Many options come in both short and long versions. Note that you need to provide these extra details for both short and long formats.

Also Read: 13 Examples That Showcase the xargs Command in Linux

The touch Command Options

Here are some options you can add to the standard touch command.

touch command options

12 Examples of Using the touch Command in Linux

The following examples illustrate various ways to employ the touch command in Linux, showcasing different options and the expected outcomes.

Let’s start with the prerequisites.

The Prerequisites

  • A system running mainstream Linux distribution
  • A user account with root or sudo privileges

Example #1: Create a New File

A common use of the touch command in its most basic form:

# touch [filename]

When the specified file doesn’t exist, touch will create it. For instance, to create a file named test_file, you would execute:

# touch test_file

Since you will not receive any notification about the success or failure of the operation, we recommend listing the contents of the directory to verify the creation of the file.

# ls -l

touch test file

If the file you’re touching already exists, touch will update its timestamp to the current system time. 

touch test file 2

Note that this operation does not alter the file’s contents or its permissions.

Example #2: Create More Than One File in Linux

A single touch command is capable of generating multiple files. For this, simply provide the filenames you wish to create, separated by a space:

# touch [filename] [another_filename]

For example, to create two files named file-1 and file-2, you would use:

# touch file-1 file-2

touch file1 file2

This method is particularly efficient for creating a large number of files simultaneously. By using curly braces {} with a range indicated by two dots (..), you can specify the start and end of a sequence. The syntax of the command is as follows:

# touch [filename{start..end}]

For instance, to create ten sequentially numbered files, you would execute:

# touch file{1..10}

touch file 1..10

This approach can also be applied to alphabetical sequences. For example, to create files named from file-a to file-m, you would run:

# touch test_{a..m}

touch file a..m

Note: It’s important to remember that this command does not support mixing numbers and letters in the sequences.

Example #3: Set a Specific Timestamp

The original use case of the touch utility is to assign a particular timestamp to an existing file. The syntax of the command in this case would be:

# touch -t [timestamp] [filename]

In this syntax, the timestamp needs to be formatted according to the following structure:

[[CC]YY]MMDDhhmm[.ss]

  • CC – Century part of the year (first two digits)
  • YY – Year part of the year (last two digits)
  • MM – Month
  • DD – Day
  • hh – Hour
  • mm – Minutes
  • ss – Seconds (optional)

Note that the square brackets in the date format indicate optional components. If you use the YY format and the value is between 0 and 68, the century (CC) is presumed to be 20. Alternatively, if YY is between 69-99, CC is presumed to be 19.

For instance, use the following command to update a file named test with a timestamp of January 1st, 1998, at midnight:

# touch -t 199801010000 test

touch -t 199801010000 test

We recommend using the ls command with the –full-time option to view the detailed timestamp of a file.

Example #4: Assign File Timestamps with Date Strings

The traditional timestamp format of the touch command is not very human-friendly, and many users might not be able to read the timestamps.

You can use the -d flag with the touch command to use human-readable date strings. The command structure for this is:

# touch -d 2024 [filename]

The date string parameter is quite versatile, supporting various formats that can include:

  • Specific calendar dates (e.g., “06 October 2020”).
  • Times of the day (e.g., “10:01 pm” or “9:02 am”).
  • Weekdays (e.g., “Tuesday”, “Monday”).
  • Relative times (e.g., “3 years ago”, “yesterday”, “next Tuesday”).

Let’s see this command syntax in action. We will use the following command to update the timestamp of a file named test to tomorrow:

# touch -d tomorrow test-file

touch -d tomorrow test-file

This functionality allows for intuitive and straightforward timestamp modifications using a human-friendly time and date format. 

Example #5: Update a File’s Access Time to the Current Time 

You can update a file’s access time to the current moment with the -a option of the touch command. The basic command format is as follows:

# touch -a [filename]

For instance, if you want to update the access time of a file named test-file, you would proceed as follows: 

First, check the current access time of the file with the following command:

# ls -lu

 

Next, update the access time of test-file by executing the following command in the terminal:

# touch -a test-file

Finally, verify that the access time has been updated to the current timestamp by running the following command: 

# ls -lu

ls -lu

Example #6: Set a Specific Access Time

In some cases, you wish to set the access time of a file to a precise timestamp. In this case, you can use a combination of the -a and -t flags with the touch command. 

The syntax for this operation is:

# touch -at [timestamp] [filename]

However, before making any changes, you might want to inspect the current access time of the files by running the following command:

# ls -lu

Now, to update the access time of a file named test to January 1st, 1999, at midnight, you would use the following command:

# touch -at 19981010000 test-file

After executing the command, confirm the updated access time by checking the file’s details again with the following ls command: 

# ls -lu

ls -lu 2

The access time should now reflect the timestamp you specified in the touch command.

Also Read: A Guide on How to Install and Use Linux Screen Command in 2024

Example #7: Update a File’s Modification Time to Current Time

The following touch command uses the -m option as a straightforward way to update a file’s modification time to the current moment. 

The standard form for this command is:

# touch -m [filename]

For instance, if you want to adjust the modification time of a file named test-file, you can use the following sequence of commands.

First, view the current modification time (mtime) of the file with this command:

# ls -l

Then, execute the following to update the modification time of the test-file:

# touch -m test-file

Finally, verify the update by checking the file’s mtime once more with this command:

# ls -l

ls -l

Example #8: Set a Specific Modification Time

We recommend using the -m and the -t flags together in the touch command to precisely set a file’s modification time to a designated timestamp. The syntax for this command is as follows:

# touch -mt [timestamp] [filename]

Before making the change, it’s a good idea to review the current modification time (mtime) of the file:

# ls -l

Next, execute the following command to adjust the modification time of a file named test to January 1st, 1999, at midnight:

# touch -mt 9801010000 test

After applying the change, verify the new modification time by inspecting the file’s details again with the following:

# ls -l

ls -l 2

 Example #9: Update Both Access and Modification Times Simultaneously

The touch command is capable of altering both the access (atime) and modification (mtime) times of a file in a single operation. The syntax of the command is as follows:

# touch -am [filename]

This command updates both the times to the current system time. 

Before proceeding with the changes, it’s useful to examine the current atime and mtime of the file:

# ls -lu

Next, run the following command to update the access and modification times of a file named test:

# touch -am test-file

Afterward, you can verify the updated times by checking them again with the following command:

# ls -lu 

for atime and 

# ls -l

for mtime.

ls -l test file

Example #10: Prevent New File Creation with touch

In its default behavior, the touch command will create a new file if the specified file does not already exist. 

However, there are times when you might want to prevent this automatic file creation. We recommend using the -c option with the touch command in these scenarios. This command syntax will only modify the timestamps if the file already exists.

# touch -c [filename]

For instance, if you attempt to use the touch command with the -c option on a file that doesn’t exist:

# touch -c new-test-file

and then check the directory contents with: 

# ls -l

you’ll see that no new file has been created.

touch -c new test file

In contrast, if the file specified with the -c option does exist, touch will go ahead and apply any specified timestamp modifications to it as it normally would.

Example #11: Use a Reference File to Set Timestamps

The touch command includes a handy feature that allows you to set the timestamp of one file based on the timestamp of another, known as a reference file. This can be particularly useful for synchronizing file timestamps among files.

 To apply this, use the -r option followed by the reference file and the target file:

# touch -r [reference file] [target file]

For instance, if you want to create a new file named new_test and assign it the same timestamp as an already existing file named test, you would execute:

# touch -r test-file new-test-file

To verify the output of the command, inspect the timestamps of both files:

# ls -l

touch -r new test file

As you can see the timestamps for new-test-file match those of the test-file file, effectively inheriting its timestamp settings.

Example #12: Update Timestamps for a Symbolic Link File

Symbolic link files or symlinks are files that point to other files on the system. 

By default, when you use the touch command with a symbolic link file, the command updates the timestamps of the target files. 

touch ls -l

The touch command provides the capability to update the timestamps of symbolic links directly without affecting the timestamps of the files they point to. This is achieved through the -h option, which targets the link itself. The syntax of this command is as follows:

# touch -h [symbolic link name]

To see how this works, first examine the current timestamp of an existing symbolic link:

# ls -l

To update the timestamp of a symbolic link named link to the current system time, you would use:

# touch -h link

After making the change, confirm that the timestamp has been updated by checking again with:

# ls -l

touch test file ls -l

Also Read: 6 Simple Examples of Using the Linux watch Command

Also Read: lsof Command in Linux with Examples

Conclusion

The touch command is an essential utility in the Linux environment, offering a versatile set of functionalities for managing files, particularly in terms of creation and timestamp modification. 

As we’ve seen, whether you’re adjusting access and modification times, creating new files, or working with symbolic links, touch provides a simple yet powerful interface for these tasks. 

For those managing Linux servers, especially in more demanding and performance-critical environments like those hosted on RedSwitches’ bare metal hosting, understanding and utilizing the touch command can enhance your efficiency and control over your system. RedSwitches’ bare metal solutions offer the robustness and reliability needed for high-performance applications, making the mastery of fundamental Linux commands an invaluable asset.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the touch command in Linux?

The touch command in Linux is a versatile tool primarily used to update the access and modification times of files in the current directory. It can also create a new file if the specified file does not exist, making it an essential utility for managing file timestamps efficiently.

Q. How do I use the touch command to create a new file?

To create a new file using the touch command in Linux, simply use the basic syntax: touch filename. For instance, touch filename.txt will create a new empty file called “filename.txt” in the current directory.

Q. What are the options available with the touch command?

The touch command in Linux provides several options, such as touch -a to change only the access time, touch -m to change only the modification time, and touch -t to specify a specific timestamp according to your requirements.

Q. Can the touch command be used to update the timestamp of an existing file?

Yes, the touch command in Linux can update the timestamp of an existing file. By specifying the existing file as an argument, the command will modify the access and modification times of the file accordingly without altering its contents.

Q. How can I create multiple files using the touch command?

To create multiple files simultaneously in Linux using the touch command, provide the names of the files as separate arguments after the command. For example, touch file1.txt file2.txt file3.txt will create three new files in the current directory.

Q. What is the syntax for using the touch command in Linux?

The syntax for the touch command is simple: touch file_name. This command is used to update the timestamp of a single file or create a new file if it doesn’t already exist in the specified location.

Q. Can the touch command edit the contents of a file?

No, the touch command cannot modify the contents of a file. It is specifically designed to change file timestamps or create new empty files, providing a quick and efficient way to manage file metadata without altering the original file contents.

Q. How do I edit a touch file in Linux?

To edit a file created with the touch command in Linux, you can use a text editor like Nano or Vim. Simply open the file with your preferred text editor and make the necessary changes to its contents.

Q. How can I use the touch command in conjunction with other powerful tools like cat to manage log files?

The touch command, combined with other powerful tools like cat, can be utilized to manage log files efficiently. For example, you can create a new log file using touch and then append content to it using cat. This approach helps in maintaining organized and updated log files for various system activities.

Q. Is the touch command commonly used for updating timestamps in log files?

Yes, the touch command is frequently used to update timestamps in log files. By modifying the access and modification times of log files using touch, system administrators can accurately track when specific events occurred, aiding in troubleshooting and analysis.

Q. Can I use the touch command to create log files for system monitoring purposes?

Absolutely! The touch command is an excellent tool for creating log files tailored to system monitoring needs. By creating empty log files with touch and periodically updating their timestamps, administrators can establish a structured logging system to monitor system performance, errors, and events effectively.

Try this guide with our instant dedicated server for as low as 40 Euros