Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Linux 'touch' command

May 29, 2024

The Power of the Linux ‘touch’ Command: A Handy Guide

In the Linux command line, there are numerous powerful utilities that can make your life easier and improve your productivity. One such command is touch, which serves a simple yet essential purpose: creating and modifying files' timestamps. Although touch seems straightforward on the surface, it offers a host of useful features that can streamline your workflow. In this blog post, we will explore the different use cases of the touch command and how you can leverage them effectively.

Creating New Files

The most basic usage of touch is to create new files. By invoking the command followed by the desired name of the file, you can instantly create an empty file. Here’s an example:

1
$ touch example.txt

Executing the above command will create a new file named example.txt in the current directory. If the file already exists, touch will update its access and modification timestamps without altering its contents.

Updating Timestamps

The primary purpose of touch is to modify timestamps of files. By default, touch updates both access and modification timestamps. However, you can modify them individually using the -a and -m options, respectively.

1
$ touch -a example.txt

The command above will update only the access time of the file, leaving the modification time unaffected. You can achieve the same result for the modification time using the -m option.

Setting Custom Timestamps

In addition to updating timestamps to the current time, touch allows you to set custom timestamps for a file. You can specify a specific date and time using the -t option along with the YYYYMMDDhhmm.ss format.

1
$ touch -t 202103151230.50 example.txt

Executing this command will set the modification and access timestamps of example.txt to March 15, 2021, at 12:30 PM and 50 seconds, respectively.

Updating Multiple Files

touch also supports operating on multiple files simultaneously. By providing multiple filenames as arguments, touch will update the timestamps for all the specified files.

1
$ touch file1.txt file2.txt file3.txt

This command will update the timestamps for file1.txt, file2.txt, and file3.txt in a single operation. It is an efficient way to ensure consistency in timestamp settings across multiple files.

Recreating Timestamps

In some cases, you may need to recreate a file’s timestamps exactly as they are of another file. The --reference option comes in handy here.

1
$ touch --reference=source.txt destination.txt

Using --reference, you can copy the timestamps from source.txt and apply them to destination.txt. This feature is especially useful when you want to synchronize timestamps between related files.

Conclusion

The touch command may appear simple, but it offers a variety of options and use cases that can greatly enhance your file management tasks. Whether you need to create new files, modify timestamps, or copy timestamps from one file to another, touch provides a flexible and efficient solution. Understanding the power of this command will undoubtedly make you a more capable and productive Linux user.

Next time you find yourself needing to create or modify timestamps, keep in mind the versatility of the touch command and give it a try. Happy programming!


➡️ Secure Communication with ESP32: Implementing TLS/SSL


⬅️ Advanced Power Management Techniques for ESP32: Maximizing Battery Life


Go back to Posts.