How to Use the Touch Command in Linux

The touch command in Linux is a versatile tool primarily used for creating new empty files or modifying the access and modification timestamps of existing files. This simple yet powerful command is a staple in any system administrator’s or developer’s toolkit. Whether you’re managing log files, automating scripts, or preparing files for later use, the touch command can make your work faster and more efficient.

In this user guide, we’ll explain how the touch command works, provide examples for common tasks, and cover advanced options to help you get the most out of it.

Prerequisites

Before you begin, ensure that:

  • You have access to a Linux terminal, either locally or via SSH.
  • You have a basic understanding of navigating the command line and working with files.

Basic Syntax of the touch Command

The general syntax of the touch command is:

touch [options] file_name

Where:

  • file_name is the name of the file you want to create or modify.
  • options are optional flags to customize the behavior of the command.

Example:

touch myfile.txt

This command creates a new empty file called myfile.txt if it does not already exist, or updates its timestamps if it does.

Check Out | How to Use the nslookup Command with Example

Practical Examples of the touch Command

Here are five practical examples to help you understand how to use the touch command:

Create a New Empty File

touch newfile.txt

This simple touch command creates an empty file named newfile.txt in the current directory. If the file already exists, its modification and access times are updated to the current date and time.

Update Timestamps of an Existing File

touch report.log

If you run this command for a file that already exists, touch will not modify the file’s contents. Instead, it updates the file’s access and modification times to reflect the moment the command was run.

Create Multiple Empty Files Simultaneously

touch file1.txt file2.txt file3.txt

This command creates three new empty files (file1.txt, file2.txt, and file3.txt) at the same time. This is handy when you need to create several placeholders or log files quickly.

Set a Specific Date and Time for a File

he touch command allows you to set specific timestamps using the -t option:

touch -t 202505310915.00 file.txt

This sets the modification and access time of file.txt to May 31, 2025, at 09:15 AM.

Alternatively, you can use the --date option to specify a human-readable date:

touch --date="2025-05-31 09:15:00" file.txt

Update Timestamps Only if the File Exists

touch -c oldfile.txt

Using the -c option with touch ensures that the file’s timestamps are updated only if the file exists. If oldfile.txt does not exist, touch will do nothing—this avoids accidentally creating new files.

Change Access and Modification Times to a File’s Reference File

touch -r reference.txt target.txt

This command uses the -r option to copy the timestamp of reference.txt and apply it to target.txt. It’s useful when you want two files to have matching timestamps.

Create an Empty File in a Different Directory

touch /tmp/testfile.log

This creates an empty file called testfile.log the /tmp directory. This helps when you need to create log files or temporary files in system directories.

Create a File Without Changing Its Timestamp

touch -a existingfile.txt

By using the -a option, touch updates only the access time of existingfile.txt, leaving the modification time unchanged. This can be helpful for certain scripts or file management scenarios.

Fixing Common Errors with the touch Command

If you encounter errors when using the touch command, don’t worry—here’s how to identify and fix them.

Permissions: A common issue is permission denial. If you see errors like “Permission denied” when using touch, it means you don’t have the required permissions to create or modify files in that directory. Try using sudo to gain elevated privileges:

sudo touch /protected/path/newfile.txt

Make sure you need to create or modify files in sensitive directories before using sudo to avoid unintended system changes.

Filesystem limitations: If the filesystem is mounted as read-only (e.g., external drives or certain system directories), touch will fail to create or modify files. You can check if a filesystem is read-only using:

mount | grep 'ro,'

If it is read-only, remount it with read/write permissions or choose a different location to create the file.

Syntax: Another frequent cause of errors is incorrect syntax. Double-check that you’re specifying valid filenames and correct options. For example, if you’re using the -t option to set a timestamp, ensure the date format is precise ([[CC]YY]MMDDhhmm[.ss]).

touch -t 202505311200.00 example.txt

If you’re working with multiple files, separate them with spaces, and ensure there are no typos.

Conclusion

Conclusion

The touch command is a simple yet powerful tool for managing files and timestamps in Linux. It’s perfect for quickly creating new files or updating existing ones without needing complex commands. As you’ve seen, it’s easy to use and works well in various scripts and system tasks. If you run into problems, checking your permissions, filesystem status, and syntax usually does the trick. With a bit of practice, you’ll be using touch confidently as part of your daily Linux work.

Leave A Comment