Guide to the Linux touch Command


Introduction

The Linux touch command is a versatile tool that allows you to create new files and update timestamps on existing files. In this guide we will explain the basic use of the touch command and some of its advanced options. We will also provide examples of how to use the command and show the corresponding output. By the end of this guide, you'll have a solid understanding of how to use the touch command to manage files on your Linux system.

Basic usage touch command

The basic syntax of the touch command is as follows −

$ touch [options] file

The file argument can be a new file or an existing file. If the file doesn't exist, the touch command will create it. If the file already exists, the touch command will update the access marks and modification to the date and time current.

Here is an example of how to use the touch command to create a new file called "Example.txt" −

$ touch example.txt

As you can see, the touch command does not produce any exit when used to create a new file. To verify that the file was created, you can use the ls command to list the contents of the current directory:

$ ls -l example.txt
-rw-r--r-- 1 user user 0 Jan 25 15:00 example.txt

The ‘-l’ option used with the ls command provides a detailed listing of the file, including its permissions, timestamps, and ownership.

Advance options in touch command

The touch command has several advanced options that can be used to customize its behavior. Some of the most used options are −

  • -a − This option updates the access timestamp of the file.

  • -m − This option updates the modification timestamp of the file.

  • -d − This option allows you to specify a specific date and time for the timestamps, instead of using the current date and time.

  • -t − This option allows you to specify a specific timestamp in the format.

Here's an example of how to use the ‘-d’ option to change timestamps on an existing file −

$ touch -d "2022-01-01 12:00:00" example.txt

To verify that the timestamps have been updated, you can use the ls command with the ‘-l’ option as before.

$ ls -l example.txt
-rw-r--r-- 1 user user 0 Jan  1 12:00 example.txt

Multiple Files

The touch command can also be used to update timestamps on multiple files at once. To do this, simply specify multiple file arguments during command execution.

For example, to update the timestamps in two files named "file1.txt" and "file2.txt", use the following command −

$ touch file1.txt file2.txt

Using touch in scripting and automation

The touch command is not only useful for manual file management but also for scripting and automation. The ability to create new files, update timestamps, and specify specific timestamps makes it a useful tool for automating tasks like backups, log rotations, and other scheduled tasks.

For example, a script that creates a new log file with the current date and time in the file name could use the touch command to create the new file and update the timestamps accordingly.

#!/bin/bash

# create a new log file with the current date and time
filename="log_$(date +%Y%m%d_%H%M%S).txt"
touch $filename

# add some content to the log file
echo "Log file created at $(date)" >> $filename

This script creates a new log file with a name that includes the current date and time, and then adds a line of text with the current date and time to the file. This can be useful for tracking when the log file was created, and it also makes it easier to identify which log file contains the information you need.

Additionally, the touch command can also be used in cron jobs to touch a file at a specific time to trigger an event or script. This can be useful for tasks like automatic backups, log rotations, and other scheduled tasks.

Conclusion

In this guide, we provide a comprehensive introduction to Linux touch control. We've covered basic usage, advanced options, and examples of how to use the command to create new files and update timestamps on existing files. We hope this guide has helped you understand how to use the touch command and that you find it a useful tool in your daily work. Remember that the touch command can be used not only for file management, but also for scripting and automation, making it an invaluable tool for any Linux user.

Updated on: 13-Feb-2023

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements