←  linux commands

Views:

touch command

The touch command is used to create an empty file, or change the access timestamp of a file.

Syntax: touch [options] filename

Example:

touch main.js
example-output

Options:

-a To change the access time to current time

To demonstrate this flag, we will do following steps:

  • create a new file
  • check its access time (atime)
  • after a minute, change its access time using touch command
  • check its access time again
touch note.txt
ls -lu
touch -a note.txt
ls -lu
change-atime-output
-m To change the modify time to current time

Example:

touch main.js
stat main.js
touch -m main.js
stat main.js
modify-time-output
-t Change the access and modification times to the specified time instead of the current time of day

The timestamp should be in [[CC]YY]MMDDhhmm[.ss] format, where:

  • CC: First two digit of the year (the century) [OPTIONAL]
  • YY: Last two digits of the year [OPTIONAL]
  • MM: Month (two-digit numeric month)
  • DD: Day (two-digit numeric day)
  • hh: Hour
  • mm: Minutes
  • ss: Seconds [OPTIONAL]

Example:

touch note.txt
stat note.txt
touch -t 192009191819 note.txt
stat note.txt
custom-time-output

Creating multiple files at once

# creating files from a set
touch file{4,5,6}.js

# creating files from a loop
touch file{1..3}.js
multiple-files-output