What is Touch Command and how does it work in Linux?

The touch command is used to modify or update the timestamp of the Linux file or directory.
It also allows us to create a file if that file does not already exist.

touch Command Syntax

The syntax for the touch command is:

touch <options> <file or directory name>

touch Command Options

OptionDescription
-aChange only the access time.
-c, –no-createDo not create any files.
-d, –date=STRINGParse STRING and use it instead of the current time.
-f(ignored)
-h, –no-dereferenceAffect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink).
-mChange only the modification time.
-r=<file>, –reference=<file>Changes a timestamp to the referenced file’s timestamp.
-t <stamp>Modifies a timestamp, where the stamp is the date/time format.
–helpOpens the help menu.
-v, –versionPrints the program version.

Example:

Create a Empty File

To create a file just type below command –

touch <filename>

For example, to create a files –

Create Multiple Empty Files

touch new_file.txt

If a file does not exist, touch creates the file but if the file already exists, touch changes the timestamp to the current time.

To create multiple file just type below command –

touch <filename1> <filename2>

For example, to create two files –

touch abc.txt xyz.txt

Create Large Batches of Files

touch command is best to create large batches of files.

touch <filename{<start>..<finish>}>

For example, to create eleven files –

touch filename{1..11}

We can also use this for letter, for example

touch filename{a..z}

Create Access Time

-a tag use to change a file’s access time to the current time. The general syntax is:

touch -a <filename>

Ignore Creating a New File

Using c option with touch command will not create an empty file if that file doesn’t exist. The general syntax is:

touch -c <filename>

Set File Timestamp Using Date String

touch uses the -d option to set a timestamp using a date string. The syntax is:

touch -d <string> <filename>

For Example

touch -d tomorrow <filename>

touch -d '9 Mar' <file_name>

Change Modification Time

The m option with the touch command, changes the modification time of a file to the current time:

touch -m <file_name>

Change Access and Modification Time

To change both access time and modification time with a single command, use the options a and m together:

touch -am <file_name>

Change Access Time Without Creating a New File

By default, touch creates a new file if it doesn’t exist. Add the -c option to avoid creating a new file when invoking the touch command:

touch -c <filename>

Set Specific Timestamp

Use the touch command to set a specific timestamp for an existing file, for example:

touch -t <timestamp> <filename>

touch -t 202203081047.30 file_name.txt

The timestamp format follows a specific pattern:

[[CC]YY]MMDDhhmm[.ss]

* CC – the first two digits of the year
* YY – the last two digits of the year
* MM – the month of the year
* DD – the day of the month
* hh – the hour of the day
* mm – the minute of the hour
* ss – the second of the minute

Set Timestamp Using a Reference File

touch command is used to change a file’s timestamp based on another file’s timestamp.

touch -r <reference file> <file>

touch -r fileone filetwo

Change Timestamp Using a Symbolic Link

touch command affect each symbolic link instead of any referenced file.

touch -h SymbolicLinkFile

Leave a Comment

Your email address will not be published. Required fields are marked *