[Linux] Create Empty Files and Change Timestamps with the touch Command

目次

Overview

The touch command is used to update file timestamps (access time and modification time) to the current time or a specific date. If a file does not exist, the command creates a new “empty file” with a size of 0 bytes. This is commonly used for creating dummy files for testing, managing backup processes, or controlling build targets for the make command.

Specifications (Arguments and Options)

Syntax

touch [options] filename

Main Arguments and Options

OptionDescription
-aChanges only the access time (atime).
-mChanges only the modification time (mtime).
-cDo not create a new file if it does not exist (no-create).
-r <file>Uses the timestamps of the specified reference file.
-t <time>Uses a specific time format ([[CC]YY]MMDDhhmm[.ss]).
-d <string>Specifies the date and time using a string like “2 days ago”.

Timestamp Types

Linux file systems track three types of time attributes for each file:

TypeAbbreviationDescription
Access TimeatimeThe last time the file content was read (e.g., using cat or grep).
Modify TimemtimeThe last time the file content was changed or written to.
Change TimectimeThe last time the file metadata (permissions, owner, size) or content was changed.

Note: When you run the touch command, ctime is always updated to the current time because the file’s inode information is updated.

Basic Usage

The most basic way to use the command is to provide a filename without any options. This creates an empty file if it doesn’t exist or updates the timestamp of an existing file to the current time.

Command

# Create a new empty file (or update the time of an existing file)
touch application.log

# Verify the file
ls -l application.log

Execution Result

-rw-r--r-- 1 user user 0 Jan 20 14:30 application.log

Practical Commands

Copying Timestamp Information from Other Files

You can apply the timestamp of one file to another file. This is useful for maintaining consistency during backups.

# Apply the time of source_config to target_config
touch -r source_config.conf target_config.conf

Changing Access and Modification Times Individually

Use these options to update only a specific attribute instead of all timestamps.

# Update only the access time (atime)
touch -a readme.txt

# Update only the modification time (mtime)
touch -m script.sh

Creating or Updating Files with Specific Dates and Times

You can set timestamps to the past or future. This is helpful for creating test data or adjusting the order of files.

# Specify date with a string (3 days ago)
touch -d "3 days ago" old_backup.tar

# Specify an exact date and time (December 25, 2025, 10:00)
touch -d "2025-12-25 10:00" christmas_log.txt

$ ls -l
-rw-r--r-- 1 user user 0 Jan 14 14:35 old_backup.tar
-rw-r--r-- 1 user user 0 Dec 25  2025 christmas_log.txt

Customization Points

  • Date Formats: The -d option is flexible and accepts formats like “next Monday” or “2024/01/01”.
  • Multiple Files: You can update several files at once using wildcards (e.g., touch *.txt).
  • Reference Files: The -r option is frequently used to preserve file dates when migrating data between systems.

Important Notes

  • Behavior of ctime: Even if you use -a or -m, the ctime (status change time) will always be updated to the current time because the file metadata changes. You cannot set ctime to a past date using touch.
  • Permissions: You must have write permission for a file to change its timestamp. If you lack permission, you will see a “Permission denied” error (use sudo if necessary).
  • Inode Usage: Be careful not to create a massive number of empty files in a script, as this can exhaust the system’s available inodes.

Applications

Creating Multiple Serial-Numbered Empty Files

You can combine touch with Bash brace expansion to create many dummy files instantly.

# Create files from test_01.dat to test_10.dat at once
touch test_{01..10}.dat

# Create files with different extensions at once
touch index.{html,css,js}

$ ls
index.css  index.html  index.js  test_01.dat ... test_10.dat

Summary

The touch command is a critical tool for managing file timestamps. It is useful in many scenarios, such as verifying log rotation, maintaining timestamp consistency in backups, and managing “last modified” dates for build processes. Understanding how to use atime, mtime, and the reference file feature will significantly help with your system administration and script automation.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次