touch command in Linux /w examples

touch command in Linux /w examples

The touch command in Linux is a versatile tool for creating empty files or updating timestamps on existing ones. Despite its simplicity, it plays a crucial role in file management. In this article, we’ll delve into the basics of using the touch command in Linux, complete with practical examples to help you become proficient in file creation and timestamp management.

This article is a follow-up to the previous 90 Linux Commands frequently used by Linux Sysadmins post. As time allows, I will continue to publish articles on each of these 90 commands geared toward Linux sysadmins and Linux power users.

Understanding the touch Command

Before we dive into examples, let’s grasp the basic syntax of the touch command:

touch [options] file(s)
  • [options]: These are optional flags that modify the behavior of the touch command. We’ll cover some common options in the examples.
  • file(s): These are the names of the files you want to create or update.

Touch Command to Create Empty Files

The primary function of the touch command is to create empty files. Here’s how to create a single empty file:

touch my_file.txt

This command creates a file named my_file.txt in the current location. If the file already exists, it updates its timestamp without altering its contents.

Use touch to Create Multiple Files

You can create multiple empty files at once by specifying their names:

touch file1.txt file2.txt file3.txt

This command creates three empty files: file1.txt, file2.txt, and file3.txt.

Setting Timestamps with touch

Besides creating files, the touch command is handy for modifying timestamps. You can specify a timestamp using the -t (timestamp) option in the format [[CC]YY]MMDDhhmm[.ss]:

touch -t 202108301530 my_file.txt

This command sets the timestamp of my_file.txt to August 30th, 2021, at 15:30.

touch Common Options

Here are some common options used with the touch command:

  • -c (no create): Prevents file creation if the file doesn’t exist.
  • -d (date): Uses a specified date/time instead of the current date.
  • -t (timestamp): Sets a custom timestamp, as demonstrated above.

Timestamp Manipulation

Manipulating timestamps can be valuable for various tasks, such as organizing files, backup management, and version control. The touch command’s -t and -d options allow you to precisely control these timestamps.

Additionally, for more advanced timestamp manipulation and scripting, consider tools like touch combined with date and find commands.

Reference: IBM touch command.

Conclusion

The touch command is a simple yet indispensable tool in Linux for file creation and timestamp management. Whether you need to create empty files, update timestamps, or set custom dates, it offers flexibility and efficiency. By mastering the touch command, you can become more proficient in file management and timestamp manipulation in the Linux environment.

Now that you’ve learned the essentials of the touch command, you can confidently create and manipulate files with precision.

If you’re interested in expanding your knowledge of Linux commands and want to explore more frequently used commands by Linux sysadmins, I recommend checking out the parent article at the following link: 90 Linux Commands Frequently Used by Linux Sysadmins.

 

Tags: , ,



Top ↑