×

Search anything:

touch command in Linux

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

The touch command is used to modify a file's timestamp, this is its primary function even though it can also be used to create files. In this article we discuss both uses of the touch command and commonly used commands.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Commands.
  4. Summary.
  5. References.

Introduction.

The touch command is used to modify a file's timestamp, this is its primary function even though it can also be used to create files.
It changes the modification and access time for any given file.
Linux timestamps include,
access time or atime which change when a command such as cat reads a file's contents, ls -lu.
change time or ctime which change when a file's properties such as names, permissions or location changes, ls -lc.
modification time or mtime which change when a file's contents change, ls -l.

Syntax.

The syntax is as follows,

touch [OPTION]... FILE...

Commonly used command options include,

  • -a, to change the access time.
  • -m, to change the modification time.
  • -d=[string] or --date=[string], to change a timestamp using a date string.
  • -t [stamp], to modify a timestamp in the date/time format.
  • -c or --no-create, to avoid creating a new file.
  • -h or --no-dereference, to change the timestamp of a symbolic link.

Commands.

You can view a file's status by using the stat command as follows,

stat test.txt

Creating files

To create a file we write,

touch file.txt

If a file doesn't exist it is created otherwise if the file exists, touch will modify the file's timestamp to the current time.

We can also create multiple files by passing their names to touch as follows,

touch file1 file2 file3

We can also create multiple file by using a range as follows,

touch file{1..7}

The above command creates seven files from file1 all the way to file7.
You can also pass the file extensions as follows,

touch file{1..7}.txt

We can also use letters,

touch file_{a..g}

Modifying timestamps

To modify a file's timestamp the syntax is as follows,

touch -t [timestamp] [file]

The timestamp follows a specific pattern,

[[CC]YY]MMDDhhmm[.ss]
  • CC, stands for the first two digits of the year,
  • YY, for the last two digits of the year,
  • MM, for month,
  • DD, for the day,
  • hh, for the hour,
  • mm, for the minutes,
  • ss, for seconds.

An example

touch -t 202201181930 test.txt

The command modifies the timestamp of test.txt to the 18th of January 2020 at 1930 hours.

The -d option is used to set a timestamp using a date string, for example,

touch -d yesterday test.txt

Will modify the timestamp to yesterday's date.

Or

touch -d 18January2000 test.txt

To modify the timestamp to 18th of january the year 2000.

or

touch --date="next Thursday" venmo.txt

To modify it to next thursday.

Modifying access time

The -a option is used.

For example,

touch -a test.txt

The access time of test.txt will be changed to the current time.

We can also modify the access time to match a specified timestamp by using both -a and -t options,

touch -at 202201181930 test.txt

We can view the changes using the ls -lu command.

Changing modification time

To change modification time we use the -m option,
An example

touch -m test.txt

The command changes the modification time for test.txt to match the current timestamp.

The ls -l command can be used to confirm changes.

We can also change modification time to a specific timestamp by using both -m and -t options as follows,

touch -mt 202201181930 test.txt

To change modification and access time in the same command we use both -a and -m options as follows,

touch -am test.txt

The -c option is used so as to avoid creating new files,
An example

touch -c randomFile.txt

The randomFile.txt won't be created, if it doesn't exist, otherwise if it exists, touch will perform as usual.

We can also change a file's timestamp based on another file's timestamp by using the -r option which stands for a reference file.

touch -r file1.txt file2.txt

The command will assign the timestamp of file1.txt to file2.txt.

We can also change symbolic link's timestamps by using the -h option as follows,
Create a symbolic link by executing,

ln -s test.txt tLink

Then change its timestamp,

touch -h tLink

Summary.

The primary function of the touch command is to modify access time, modification time or change time. It can also be used to create empty files.
We can implement touch command during runtime to create and write to files.
Touch avoids opening, updating, saving and closing of files.

References.

  1. Touch manual pages, man touch or touch --help.
touch command in Linux
Share this