touch

touch linux command cheatsheet by Thamizhiniyan C S

Introduction

The primary function of the touch command is to modify a file's timestamp. While it is often used to create files, this is not its main purpose.


Syntax

touch [OPTION]... FILE...


Important Flags

FlagDescription

-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


Time Stamp Formats

Syntax: [[CC]YY]MMDDhhmm[.ss]

FormatDescription

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


Examples

CommandDescription
touch filename

To create a emtpy file

touch File1_name File2_name File3_name

To create multiple files at the same time. These files will be empty upon creation.

touch test{1..10}

To create ten files with appended numbering

touch test_{a..j}

To create ten files with appended alphabets

touch -a [filename]

To change a file's access time to the current timestamp.

touch -at [timestamp] [filename]

To modify access time explicitly.

touch -am [filename]

To change both modification and access times to the current timestamp.

touch -c fileName

Checks if a file is created; if not, it doesn’t create it.

touch -c -d fileName

Updates both access and modification times.

touch -m [filename]

To change a file's modification time to the current timestamp.

touch -mt [timestamp] [filename]

To set modification time explicitly.

touch -d "17 Mar 2023" fileName

Changes the modification date only.

touch -r [reference file] [file]

To set a file's timestamp based on another file's timestamp.

touch -r second_file_name first_file_name

Uses the timestamp of another file.

touch -t YYMMDDHHMM fileName

Creates a file with a specified time.

touch -t [timestamp] [existing_filename]

To set a specific timestamp for an existing file.

touch -h [filename]

To modify the timestamp of a symbolic link without affecting the referenced file.

Last updated