# touch — Create Files and Set Timestamps

> Create empty files or update file access and modification timestamps. Essential for creating placeholder files, triggering build systems, and managing file metadata.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/touch/

<!-- PROSE:intro -->
touch does two things: it creates empty files and it updates the timestamps of existing files – the access time (atime) and the modification time (mtime). If the file does not exist yet, it is created empty; if it already exists, its contents are left untouched and only the timestamps move to "now". That makes touch the go-to tool for placeholders like `.gitkeep`, for health-check markers in containers, and for nudging build systems such as make into rebuilding. This guide covers creating files, setting specific timestamps, and common real-world patterns.
<!-- PROSE:intro:end -->

## Create Files

`touch <file>` — Create an empty file (or update timestamp if it exists).

```bash
touch newfile.txt
```

`touch <file1> <file2> <file3>` — Create multiple files at once.

```bash
touch index.html style.css script.js
```

`touch -c <file>` — Update timestamp only if file exists (do not create).

```bash
touch -c maybe-exists.txt
```

## Set Specific Timestamps

`touch -t <YYYYMMDDhhmm> <file>` — Set a specific date and time.

```bash
touch -t 202601011200 file.txt
```

`touch -t <YYYYMMDDhhmm.ss> <file>` — Set date, time and seconds.

```bash
touch -t 202601011200.30 file.txt
```

`touch -d '<date string>' <file>` — Set timestamp using a human-readable date string.

```bash
touch -d '2026-01-15 14:30:00' file.txt
```

`touch -d 'next Monday' <file>` — Set timestamp using relative date expressions.

```bash
touch -d 'next Monday' reminder.txt
```

`touch -d '-2 days' <file>` — Set timestamp to 2 days ago.

```bash
touch -d '-2 days' oldfile.txt
```

## Modify Specific Timestamps

`touch -a <file>` — Update only the access time (atime).

```bash
touch -a data.log
```

`touch -m <file>` — Update only the modification time (mtime).

```bash
touch -m config.yaml
```

`touch -r <reference> <file>` — Set timestamp to match another file's timestamp.

```bash
touch -r original.txt copy.txt
```

`touch -a -d '<date>' <file>` — Set a specific access time.

```bash
touch -a -d '2026-06-01' data.csv
```

## Common Patterns

`touch .gitkeep` — Create a placeholder to keep an empty directory in Git.

```bash
mkdir -p logs && touch logs/.gitkeep
```

`touch -r <src> <dest>` — Synchronize timestamps between files.

```bash
touch -r photo.jpg photo_edited.jpg
```

`find . -name '*.txt' -exec touch {} +` — Update timestamps of all matching files.

```bash
find . -name '*.cache' -exec touch {} +
```

`touch /tmp/.healthcheck` — Create a health-check marker file (common in Docker).

```bash
touch /tmp/.healthcheck
```

`touch -d '1970-01-01' <file>` — Set timestamp to Unix epoch (force rebuild in build systems).

```bash
touch -d '1970-01-01' Makefile
```

<!-- PROSE:outro -->
## Conclusion

touch is unassuming but surprisingly versatile in daily work: the fastest way to create empty files and the simplest way to manipulate timestamps deliberately. With `-t` and `-d` you set date and time absolutely or relatively, with `-a`/`-m` you change only the access or modification time, and with `-r` you copy the time from a reference file. This is especially handy with build systems like make, which base their decisions on mtime – a targeted touch forces or prevents a rebuild. Keep in mind that touch never empties an existing file: it only changes metadata, never the contents.

## Further Reading

- [GNU coreutils: touch invocation](https://www.gnu.org/software/coreutils/manual/html_node/touch-invocation.html) – official reference for every option
- [Linux man page: touch(1)](https://man7.org/linux/man-pages/man1/touch.1.html) – the canonical manual page
<!-- PROSE:outro:end -->

## Related Commands

- [mkdir](https://www.jpkc.com/db/en/cheatsheets/files-text/mkdir/) – create directories
- [stat](https://www.jpkc.com/db/en/cheatsheets/files-text/stat/) – show a file's timestamps and metadata
- [ls](https://www.jpkc.com/db/en/cheatsheets/files-text/ls/) – list directory contents and timestamps

