fbpx Touch Command in Linux: Create Files and Update Timestamps | Certificação Linux

Touch Command in Linux: Create Files and Update Timestamps

📅 01/01/2019 ⏱ 4 min ✍️ Uira Ribeiro
Linuxcommands
Touch Command in Linux: Create Files and Update Timestamps
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

Change file metadata in Linux with the touch command

Every file contains metadata information, such as its name, creation date, modification date, and last access date, among other information.

The touch command is used to change the time metadata of files, modifying the last access or modification date of the files.

This can be useful to mark a file for incremental or differential backup from a certain date, or to be synchronized in a repository, without the need to change the file content, just by changing its modification date.

The touch command is also useful for creating an empty file if the specified file does not exist.

Touch command options

The most common options are:

  • -a                Changes the last access information of the file
  • -c               Does not create the file if it does not exist
  • -d time       Allows a date in text format to be specified instead of the current date and time
  • -m               Changes the modification time of the file
  • -t stamp    Uses the format [[CC]YY]MMDDhhmm[.ss] instead of the current date and time
  • --time=WORD    Changes the specified time information, which can be atime (access) or modification (mtime)

The -d=data option is human-readable date text, mostly free-form, such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date sequence can contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers.

Create an empty file in Linux

In the example below, touch is used to create an empty file called config.txt:

$ touch config.txt$ ls -l-rw-rw-r-- 1 ec2-user ec2-user       0 set 13 22:29 config.txt

Create multiple files at once in Linux

A list can also be used to create multiple files:

$ touch teste{1..3}.txt$ ls -l-rw-rw-r-- 1 ec2-user ec2-user     226 set 12 16:14 teste1.c-rw-rw-r-- 1 ec2-user ec2-user       0 set 13 22:31 teste1.txt-rw-rw-r-- 1 ec2-user ec2-user       0 set 13 22:31 teste2.txt-rw-rw-r-- 1 ec2-user ec2-user       0 set 13 22:31 teste3.txt

Change the modification date of a file in Linux

Touch can be used to change the modification date of a file, such as test1.c which was modified on Sep 12:

$ touch -d "09/12" teste1.c
$ ls -l -rw-rw-r-- 1 ec2-user ec2-user   226 set 12 22:35 teste1.c-rw-rw-r-- 1 ec2-user ec2-user     0 set 13 22:31 teste1.txt-rw-rw-r-- 1 ec2-user ec2-user     0 set 13 22:31 teste2.txt-rw-rw-r-- 1 ec2-user ec2-user     0 set 13 22:31 teste3.txt

You can also specify a specific date and time instead of the current date and time. In this example: 05/11/1978 at 18:40:30:

$ touch -t 197805111840.30 teste1.c$ ls -l -rw-rw-r-- 1 ec2-user ec2-user   226 mai 11  1978 teste1.c

The specific time format is defined by the CCYYMMDDhhmm.ss standard, where:

  • CC – The first two digits of the year.
  • YY – The next two digits of the year.
  • MM – The month of the year [01-12].
  • DD – The day of the month [01-31].
  • hh – The hour of the day [00-23].
  • mm – The minute of the hour [00-59].
  • ss – The second of the minute [00-59]

You can also specify the date and time as a string using the -d option. The following command line shows an example that sets the date to February 23 and the time is automatically set to 00:00:

$ touch -d '23 Feb'  teste1.c$ ls -l teste*-rw-rw-r-- 1 ec2-user ec2-user   226 fev 23  2019 teste1.c

Watch this short video showing how the touch command works:

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp