touch

touch

The touch command in Unix and Linux is used to create empty files and update timestamps of existing files. It is a simple yet versatile command that allows you to interact with files by modifying their timestamps or creating new ones.

Basic Usage

The basic syntax for the touch command is:

touch [option] file...
  • option: Optional command-line options to control the behavior of touch.

  • file: The name(s) of the file(s) to be created or updated.

Examples

Creating a New File

To create a new empty file:

touch newfile.txt

This command creates a new file named newfile.txt in the current directory.

Updating File Timestamp

To update the access and modification times of a file to the current time:

touch existingfile.txt

This command updates the timestamp of existingfile.txt to the current time. If existingfile.txt doesn't exist, it creates an empty file with that name.

Creating Multiple Files

To create multiple files at once:

touch file1.txt file2.txt file3.txt

This command creates three empty files named file1.txt, file2.txt, and file3.txt in the current directory.

Options

-a Option: Change Access Time Only

To update only the access time of a file:

touch -a file.txt

This command updates the access time of file.txt to the current time.

-m Option: Change Modification Time Only

To update only the modification time of a file:

touch -m file.txt

This command updates the modification time of file.txt to the current time.

-c Option: Do Not Create a New File

To prevent touch from creating a new file if it doesn't exist:

touch -c file.txt

This command updates the timestamp of file.txt only if it already exists. If file.txt doesn't exist, touch does nothing.

Practical Use Cases

Creating Placeholder Files

To create placeholder files for testing or organizational purposes:

touch README.md LICENSE.txt TODO.txt

This command creates empty files named README.md, LICENSE.txt, and TODO.txt in the current directory.

Updating Timestamps for Scripts

To update the modification time of a script file to reflect recent changes:

touch script.sh

This command updates the modification time of script.sh, which can be useful for tracking when changes were last made.

Summary

The touch command is a straightforward tool for creating empty files and modifying timestamps in Unix and Linux environments. Its simplicity and various options provide flexibility for tasks such as file creation, timestamp manipulation, and script management. Understanding these options and practical use cases can help you efficiently manage files and timestamps on your system.

help

Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.

Last updated