Touch

From Unix SME

Description

  The 'touch' command is often utilized in the creation of empty text files, given that one with the desired name does not already exist. The more common purpose amongst administrators, however, is for the creation, modification, and general manipulation of access and modification time stamps for the file. This command can be used when the user doesn’t have data to store at the time of file creation.

Syntax and Structure

  touch [option] [file_name]
  touch 202401120643 DONOTREAD.txt

Remember, the timestamp format is

  [YY]YYMMDDhhmm[.ss]
   YY or YYYY: Year (two or four digits)
   MM: Month (01-12)
   DD: Day of the month (01-31)
   hh: Hour (00-23)
   mm: Minute (00-59)
   .ss: Optional seconds (00-59)

Use Case

  The 'touch' command would be used for the simple creation of an empty file or the manipulation and modification of access and modify dates and times of a file for whatever reason that may be.

Options

  -a This option changes the access time only.
  -c Suppresses file creation if the file does not exist.
  -d Sets the access and modification times using the specified STRING.
  -m This option changes the modification time only.
  -r Uses the access and modification times from the reference file.
  -t Changes the time with the timestamp format(see above) instead of the current time.
  -h Affects each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink).


Examples

Create Empty Files

  [user@host ~]$ touch file1_folder2 file2_folder1

Touch Multiple Files at Once

  [user@host ~]$ touch file{1..4}.txt

Access Time Change

  [user@host ~]$ touch -a file.txt

Modify Time Change

  [user@host ~]$ touch -m newfile.txt

Timestamp Used for Another File

  [user@host ~]$ touch -r file1.txt file2.txt

Specified Format(see above)

  [user@host ~]$ touch -t 202312301200 file.txt

Symbolic Link Timestamp

  [user@host ~]$ touch -h misc.txt

Easier Entry Format

  [user@host ~]$ touch -d '1 February 2024 12:32' file.txt

Update Access and Modification Times

  [user@host ~]$ touch -c-d editandmodify.txt.

Attempt to Force Read-Write Permissions

  [user@host ~]$ touch -f reallyneedthistowork.txt

Wildcard Touch

  [user@host ~]$ touch miscfiles{1..5}.ogg