Skip to content
Linux Basics: Copy Reference Timestamps with GNU touch

Linux Basics: Copy Reference Timestamps with GNU touch

October 30, 2026

A generated fixture sometimes needs a known modification time without changing its content. GNU touch -r takes timestamps from a reference file. Adding -m changes only the modification time, which is useful when testing a timestamp-based workflow.

Prepare a scratch directory

Tested on Linux with GNU Bash 5.2.37 and GNU coreutils 9.7. You need Bash, a text editor, and the utilities shown below. The script creates and removes its own temporary fixture directory.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-gnu-touch-reference-times.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
export TZ=UTC
printf 'reference\n' > reference.txt
printf 'payload\n' > result.txt
touch -a -d '2020-01-01 00:00:00 UTC' result.txt
touch -m -d '2024-02-03 04:05:06 UTC' reference.txt
before_access=$(stat -c %X result.txt)
touch -m -r reference.txt result.txt
printf 'Modification time:\n'
date -u -r result.txt '+%Y-%m-%dT%H:%M:%SZ'
test "$(stat -c %X result.txt)" = "$before_access"
printf 'Access time preserved: yes\n'
printf 'Content: '
cat result.txt
touch -c -m -r reference.txt missing.txt
test ! -e missing.txt
printf 'Missing file created: no\n'

Run it:

bash demonstrate-gnu-touch-reference-times.sh

Observed output:

Modification time:
2024-02-03T04:05:06Z
Access time preserved: yes
Content: payload
Missing file created: no

Read the result

-r reference.txt selects the timestamp source, and -m limits the update to modification time. The script compares access times before reading the payload, because reading a file can itself update its access time. -c prevents creation of a missing target; without it, touch normally creates an empty file. The UTC output avoids differences caused by the terminal’s time zone.

Apply it carefully

A matching timestamp does not prove two files have matching bytes. Use a content comparison or hash for that question. Changing timestamps also updates filesystem status-change time; touch cannot restore that value or arbitrarily set a file’s creation time. Filesystem precision and permission rules can affect timestamp operations. Only use this technique where preserving or controlling timestamps is intentional: build tools and synchronizers may use modification times to decide whether to process a file.

The command reference documents the available options.

Clean up

Remove the saved script, return to the starting directory, and remove the empty practice directory:

rm -- demonstrate-gnu-touch-reference-times.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect what remains rather than deleting unknown files.

Acknowledgements

This post was created with assistance from AI.

Last updated on