Sagar.BlogArticle
All posts
All posts
Linux

touch — Create Files and Manipulate Timestamps

touch creates empty files and updates timestamps. Learn its options and the creative ways it's used in scripts and makefiles.

January 11, 20254 min read
LinuxFilesCommands

touch has two main jobs:

  1. Create empty files — if the file doesn't exist, touch creates it with zero bytes
  2. Update timestamps — if the file already exists, touch updates its access and modification times to now

The name comes from its original purpose — "touching" a file to update its timestamp, signalling to build tools like make that the file has changed.

Options

touch Options

OptionDescription
-aUpdate access time only
-mUpdate modification time only
-cDon't create file if it doesn't exist
-d STRINGSet timestamp from human-readable string (e.g. "2024-01-15 10:30")
-t STAMPSet timestamp in [[CC]YY]MMDDhhmm[.ss] format
-r FILECopy timestamp from another file

Creating Files

touch file.txt               # Create empty file (or update timestamp)
touch file1.txt file2.txt    # Create multiple files at once
touch .hidden_file           # Hidden file (starts with .)

# Pro: combine with brace expansion
touch index.{html,css,js}   # Creates index.html, index.css, index.js
touch file{1..10}.txt        # file1.txt through file10.txt

Updating Timestamps

Every file in Linux has two timestamps:

  • Access time (atime) — last time the file was read
  • Modification time (mtime) — last time the file content was changed

By default, touch updates both. Use -a or -m to update only one.

touch file.txt               # Update both atime and mtime to now
touch -a file.txt            # Update access time only
touch -m file.txt            # Update modification time only
touch -c nonexistent.txt     # Update if exists, don't create if not

# Set a specific timestamp
touch -d "2024-06-15 09:30" report.txt
touch -t 202406150930 report.txt        # Same: YYYYMMDDhhmm

# Copy timestamp from another file
touch -r reference.txt target.txt       # target gets reference's time

Checking Timestamps

# See modification time
ls -l file.txt

# See both access and modification time
stat file.txt
# File: file.txt
# Size: 0               Blocks: 0          IO Block: 4096   regular empty file
# Access: 2025-01-11 10:30:00
# Modify: 2025-01-11 10:30:00
# Change: 2025-01-11 10:30:00

Quick Check

You run `touch existing.txt` on a file that already exists and has content. What happens?

Exercise
  1. Create a set of test files using brace expansion: log-{jan,feb,mar}.txt and config.{yaml,json,toml}
  2. Check their timestamps with ls -l
  3. Wait a few seconds, then touch one file
  4. Compare timestamps with ls -lt (sorted by time) to see which was touched most recently