LinuxCommandLibrary

git-touch

Create empty files and update timestamps

TLDR

Create new files and add them to the index

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

SYNOPSIS

touch [OPTION]... FILE...

PARAMETERS

-a
    Change only the access time.

-c, --no-create
    Do not create any files that do not exist.

-d, --date=STRING
    Parse STRING and use it instead of current time.

-f
    Ignore. For compatibility only.

-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: access, atime, or use are equivalent to -a; modify, mtime, or touch are equivalent to -m.

--help
    Display help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The `touch` command is a standard Unix and Linux utility used to update the access and modification times of files and directories. If a file does not exist, `touch` creates an empty file with the specified name. Its primary function is to change file timestamps without altering the file content. This is useful for maintaining build dependencies (e.g., with `make`) or simply ensuring a file's timestamp reflects a more recent activity. The command supports options to specify the time to be set (using variations of date format), to not create the file if it doesn't exist, and to only update either the access or modification time. It's a fundamental tool in managing file system metadata and is frequently used in scripting and automation tasks. The command is simple and frequently used in daily work to create new files or update the timestamps.

CAVEATS

Using the `-t` option with incomplete date/time information may yield unexpected results. Ensure the format is accurate.

TIME FORMATS

-d, --date=STRING allows for flexible date/time specifications. Common formats include 'YYYY-MM-DD hh:mm:ss' or 'yesterday'.

-t STAMP requires the format [[CC]YY]MMDDhhmm[.ss], where CC is the century, YY is the year, MM is the month, DD is the day, hh is the hour, mm is the minute, and ss is the optional seconds.

USAGE EXAMPLES

1. Create a new file: touch newfile.txt

2. Update the modification time of an existing file: touch existingfile.txt

3. Update both access and modification times to a specific date: touch -d '2023-12-25 10:00:00' myfile.txt

4. Prevent creation if the file doesn't exist and update the timestamp: touch -c existingfile.txt

HISTORY

The `touch` command has been a standard part of Unix-like operating systems since the earliest versions. Its initial purpose was to update file timestamps, primarily for use with `make` and other build systems. Over time, it has remained a consistent and reliable utility, with minor additions to its functionality. The core functionality has remained stable, reflecting its utility in basic file management.

SEE ALSO

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

Copied to clipboard