WebMasterCampus
WEB DEVELOPER Resources

Linux touch Command

Learn Linux touch Command with examples


Linux touch Command

The touch command’s functionality is to change file timestamp. The touch command can also creates a file, if the file doesn’t already exist.

touch Command Syntax

~$  touch [OPTION]... FILE...

# The touch command's functionality is to change file timestamp. The touch command can only creates a file, if the file doesn't already exist.

[option] are optional arguments that we can provide. We can create change a timestamp by just write touch and file name. If file already exist then it’s timestamp change with current timestamp. Also, if file doesn’t exist then it will create the file with current timestamps.

Argument Description
-a Changes the access time.
-c
--no-create
Avoids creating a new file.
-d
--date=string
Changes a timestamp using a date string.
-f No effect In older BSD's the option forces changes.
-h
--no-dereference
Changes a symbolic link's timestamp.
-m Changes 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.
--help Opens the help menu.
-v
--version
Prints the program version.

Create a File

~$  touch <filename>

~$  touch fruits.txt

# This touch command will create a file fruits.txt if not already exist. If already exist then it will just update it's timestamp with current.

Create Multiple File

~$  touch <filename> <filename> <filename>

~$  touch fruits.txt vegetables.txt 

# This touch command will create two files fruits.txt and vegetables.txt if not already exist. If already exist then it will just update it's timestamp with current.

Avoid Creating a New File

The touch command avoid creating a new file when invoking with -c option.

~$  touch -c <filename>

~$  touch fruits.txt

~$  ls -l

# This touch command will not create a file fruits.txt if it's not already created. So, we use -c to just update timestamps.

Create Large Batch of files with Increasing Number

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

~$  touch student-list{1..10} 

# This touch command will create ten files with appended numbering.

Files Created:
--------------
student-list1 
student-list2
student-list3 
...
...
student-list10

Create Large Batch of files with increasing alphabets

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

~$  touch studentlist-{a..g} 

# This touch command will create files with appended alphabet from a to g.
Files Created:
--------------
studentlist-a
studentlist-b
studentlist-c
...
...
studentlist-g

Set Specific Timestamp

~$  touch -t <timestamp> <filename>

$ touch -t 200903050000 test.txt

# TimeStamp Format [[CC]YY]MMDDhhmm[.ss] 

# 200903050000 

# CC: 20
# YY: 09
# MM: 03
# DD: 05
# hh: 00
# mm: 00
# ss: optional and not provided.

TimeStamp Format [[CC]YY]MMDDhhmm[.ss]

the timestamp test file to midnight March 5th, 2009, if we use touch -t 200903050000 test.txt

TimeStamp Format Argument Description
CC the first two digits for a year
YY the last two digits for a year
MM the month
DD the day
hh the hour
mm the minutes
ss the seconds
-d Changes a timestamp using a date string.

Set Tomorrow or Yesterday Timestamp

~$  touch -d tomorrow test.txt

~$  touch -d yesterday test.txt

Linux Access Time, Change Time, Modification Time

Each file of Linux has three different timestamps.

  • Access time or atime changes when a command reads the file’s contents, such as grep or cat. The ls -lu command displays the atime for files.
  • Change time or ctime changes when a file’s property changes, such as renaming files, modifying file permission, or moving the file. The ls -lc command shows the ctime for files.
  • Modification time or mtime changes when a file’s contents change. The ls -l command shows the mtime for files.

Change Access Time to Current

~$  touch -a <filename>

~$  touch -a test.txt

~$  ls -lu

Change Access Time Explicitly

~$  touch -at <timestamp> <filename>

~$  touch -at 2001020000 test.txt

~$  ls -lu

Change Modification Time to Current

~$  touch -m <filename>

~$  touch -m test.txt

~$  ls -l

Change Modification Time Explicitly

~$  touch -m <filename>

~$  touch -mt 9901010000 test.txt

~$  ls -l

Change Both Modification and Access Time

~$  touch -am <filename>

~$  ls -lu

~$  touch -am test.txt

~$  ls -lu

To see a complete list of the possible string input options, visit the Date input formats GNU documentation.

~$  touch -h <filename>

~$  ls -l

~$  touch -h mylink

~$  ls -l

Set Timestamp Using a Reference File

~$  touch -r <reference file> <file>

~$  touch -r test my-test

~$  ls -l

touch command in Linux (Documentation)

NAME
       touch - change file timestamps

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       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  refer‐
              enced  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.
    
    DATE STRING
       The --date=STRING is a mostly free format human readable
       date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or
       "2004-02-29  16:21:42"  or even "next Thursday".  A date
       string may contain items indicating calendar date,  time
       of  day, time zone, day of week, relative time, relative
       date, and numbers.  An empty string indicates the begin‐
       ning of the day.  The date string format is more complex
       than is easily documented here but is fully described in
       the info documentation.
Created with love and passion.