LinuxCommandLibrary

touch

Update file access/modification timestamps

TLDR

Create specific files

$ touch [path/to/file1 path/to/file2 ...]
copy

Set the file [a]ccess or [m]odification times to the current one and don't create file if it doesn't exist
$ touch [[-c|--no-create]] -[a|m] [path/to/file1 path/to/file2 ...]
copy

Set the file [t]ime to a specific value and don't create file if it doesn't exist
$ touch [[-c|--no-create]] -t [YYYYMMDDHHMM.SS] [path/to/file1 path/to/file2 ...]
copy

Set the files' timestamp to the reference file's timestamp, and do not create the file if it does not exist
$ touch [[-c|--no-create]] [[-r|--reference]] [path/to/reference_file] [path/to/file1 path/to/file2 ...]
copy

Set the timestamp by parsing a string
$ touch [[-d|--date]] "[last year|5 hours|next thursday|nov 14|...]" [path/to/file]
copy

Create multiple files with an increasing number
$ touch [path/to/file{1..10]}
copy

Create multiple files with a letter range
$ touch [path/to/file{a..z]}
copy

SYNOPSIS

touch [OPTION]... FILE...
OPTION: Represents zero or more command-line options that modify touch's behavior.
FILE: Represents one or more file paths. If a file doesn't exist, touch creates it by default.

PARAMETERS

-a, --time=atime, --time=access, --time=use
    Changes only the access time (the time the file was last read).

-c, --no-create
    Prevents touch from creating any files that do not already exist.

-d STRING, --date=STRING
    Uses the specified STRING as the timestamp instead of the current time. STRING can be in various formats (e.g., 'YYYY-MM-DD HH:MM:SS', 'yesterday').

-h, --no-dereference
    Affects symbolic links themselves rather than the files they point to. This option is only effective on systems that support changing timestamps of symbolic links.

-m, --time=mtime, --time=modify
    Changes only the modification time (the time the file's content was last changed).

-r FILE, --reference=FILE
    Uses the access and modification times from the specified FILE as the new timestamps for the target files.

-t STAMP
    Uses the timestamp specified by STAMP, which must be in the format [[CC]YY]MMDDhhmm[.ss].

--help
    Displays a help message and exits.

--version
    Displays version information and exits.

DESCRIPTION

touch is a fundamental Unix/Linux command used to update the access and modification timestamps of files and directories. When executed without any options on an existing file, it sets both the access time (last read) and modification time (last content change) to the current system time. A key feature of touch is its ability to create new, empty files if the specified file does not already exist. This makes it an efficient way to quickly generate placeholder files or ensure a file's presence for subsequent operations. It is extensively employed in scripting and build systems (such as Make) to manage dependencies, often forcing recompilations or re-executions based on file age. Users have the flexibility to specify custom timestamps or use the timestamps of another reference file for more precise control.

CAVEATS

  • The --no-dereference (-h) option's effectiveness on symbolic links depends on the underlying file system and operating system support; some systems do not allow direct modification of symlink timestamps.
  • When using -d or -t, ensure the timestamp format is correct; otherwise, touch may fail or produce unexpected results.
  • Modifying timestamps can impact build systems, caching mechanisms, or backup processes that rely on file ages.
  • touch requires appropriate permissions to modify existing files or create new ones in a directory.

DEFAULT BEHAVIOR

When touch is executed on an existing file without any options (like -a or -m), it updates both the access and modification times to the current system time. If the specified file does not exist, it is created as an empty file, and its timestamps are set to the current time.

TIMESTAMPS EXPLAINED

  • Access Time (atime): The last time the file's data was read.
  • Modification Time (mtime): The last time the file's content was changed.
  • Change Time (ctime): The last time the file's metadata (e.g., permissions, owner, or link count) or content was changed. While touch directly modifies atime and mtime, ctime is automatically updated whenever atime, mtime, or other file metadata changes.

HISTORY

The touch command has been a standard utility in Unix-like operating systems since their early development. It is a part of the POSIX standard, ensuring its consistent presence and behavior across various Unix and Linux distributions. Its fundamental simplicity and utility for file manipulation, particularly in conjunction with build tools like Makefiles and scripting for managing file dependencies, have solidified its continued relevance and widespread use in system administration, software development workflows, and automation tasks.

SEE ALSO

stat(1), find(1), ls(1), date(1)

Copied to clipboard