この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:6
ページ更新者:guest
更新日時:2026-06-11 07:12:00

タイトル: touch 空ファイルの新規作成
SEOタイトル: Linux touch コマンド完全ガイド — 空ファイル作成とタイムスタンプ

この記事の要点
  • touch file.txt空ファイル新規作成。既存ならタイムスタンプを現在時刻に更新
  • 日時指定: touch -t 202401011200 file(CCYYMMDDhhmm 形式)
  • -a アクセス時刻のみ / -m 更新時刻のみ
  • -r ref別ファイルから時刻コピー-d "2024-01-01" で日付文字列指定
  • -c存在しなければ作成しない(mtime 更新のみ)
  • 複数同時: touch f1 f2 f3。ブレース展開: touch file{1..10}.txt
  • Windows 等価: PowerShell New-Item -ItemType File / cmd type nul > file

touch コマンドの目的

touch は本来ファイルのアクセス時刻 / 更新時刻を変更するためのコマンドです。副作用として存在しないファイル名を指定すると 0 バイトの空ファイルが作成される、というのがよく知られた使い方です。

# 1. 空ファイル作成
touch newfile.txt
ls -l newfile.txt
# -rw-r--r-- 1 user user 0 Apr  1 10:00 newfile.txt

# 2. 既存ファイルの mtime を現在時刻に更新
touch existing.txt
ls -l existing.txt
# → mtime が今に書き換わる

# 3. 複数ファイル一括
touch file1.txt file2.txt file3.txt

# 4. ブレース展開(Bash)
touch file{1..10}.txt
touch test_{a,b,c}.log

主要オプション

オプション意味
-aアクセス時刻(atime)のみ変更touch -a file
-m更新時刻(mtime)のみ変更touch -m file
-c / --no-create存在しなくても作成しないtouch -c maybe-missing
-r REF別ファイルの時刻をコピーtouch -r src.txt dst.txt
-t [[CC]YY]MMDDhhmm[.ss]直接指定(数値)touch -t 202401011200 f
-d STR / --date=STR日付文字列で指定touch -d "2024-01-01 12:00" f
--time=atime|mtime変更対象を明示touch --time=atime f
-h / --no-dereferenceシンボリックリンク自体を対象にtouch -h link

タイムスタンプ指定の各種形式

# -t は CCYYMMDDhhmm[.ss] 形式(YY のみも可)
touch -t 202401011200 file        # 2024-01-01 12:00:00
touch -t 202401011200.30 file     # 2024-01-01 12:00:30
touch -t 12251800 file            # 今年の 12-25 18:00(CCYY 省略)

# -d はもっと人間に優しい指定
touch -d "2024-01-01" file
touch -d "2024-01-01 12:00:00" file
touch -d "yesterday" file
touch -d "next monday" file
touch -d "2 hours ago" file
touch -d "@1700000000" file       # UNIX エポック秒

# 別ファイルから時刻をコピー
touch -r reference.txt target.txt
# → reference.txt と target.txt の atime/mtime が同一に

atime / mtime / ctime の違い

時刻意味更新タイミングtouch で変更
atime(Access time)最終アクセス時刻読み込み時-a で可
mtime(Modify time)最終更新時刻(中身が変わった)書き込み時-m で可
ctime(Change time)最終ステータス変更時刻パーミッション/所有者/中身変更時直接変更不可
# stat で 3 つすべて確認
stat file.txt
# Access: 2024-04-01 10:23:45
# Modify: 2024-03-30 15:00:00
# Change: 2024-03-30 15:00:00
#  Birth: 2024-03-30 15:00:00    ← btime(作成時刻)

# touch -a で atime のみ変更
touch -a file.txt
# atime のみが今に。mtime はそのまま

# touch -m で mtime のみ変更
touch -m file.txt
# mtime のみが今に

実用シナリオ

1. ログローテーション

# 古いログをクリアして空にする代わりに、
# 空ファイルとしてリセット
> /var/log/myapp.log
# または
truncate -s 0 /var/log/myapp.log

# 新規ログファイルの「枠だけ」先に作る(権限制御のため)
sudo touch /var/log/myapp.log
sudo chown myapp:myapp /var/log/myapp.log
sudo chmod 640 /var/log/myapp.log

2. Makefile のフラグファイル

# 「初期化済み」マーカー
.initialized: ; touch .initialized

build: .initialized
	# .initialized が更新されたら再ビルド

3. cron との連携

# 「最後にバックアップした時刻」を記録
/usr/local/bin/backup.sh && touch /var/lib/myapp/.last-backup

# 24h 以内にバックアップが取れていなければ警告
find /var/lib/myapp/.last-backup -mtime +1 -exec mail -s "Backup missed" ops@example.com \;

4. デバッグでファイル更新を検知

# inotifywait と組み合わせて変化を見る
touch /tmp/marker
# 別ターミナルで:
inotifywait -m /tmp/marker
# → 変更があれば即時通知

Windows での等価コマンド

Windows には標準の touch はありませんが、複数の方法で代替できます:

# PowerShell

# 1. 空ファイル新規作成
New-Item -ItemType File -Path .\newfile.txt

# 2. 既存ファイルのタイムスタンプ更新
(Get-Item .\file.txt).LastWriteTime = Get-Date

# 3. アクセス時刻も更新
(Get-Item .\file.txt).LastAccessTime = Get-Date

# 4. 関数化(touch 相当)
function touch {
    foreach ($p in $args) {
        if (Test-Path $p) {
            (Get-Item $p).LastWriteTime = Get-Date
        } else {
            New-Item -ItemType File -Path $p | Out-Null
        }
    }
}
:: cmd.exe

:: 空ファイル作成(type nul リダイレクト)
type nul > newfile.txt

:: copy で作成(CON からの入力)
copy nul newfile.txt

:: 時刻更新は標準では難しい。PowerShell 推奨

関連コマンド

  • stat FILE — atime / mtime / ctime / btime をまとめて表示
  • truncate -s 0 FILE — ファイルサイズを 0 にする(mtime は更新)
  • install -m 644 /dev/null FILE — 空ファイル作成 + パーミッション同時指定
  • mktemp — 一意な名前の一時ファイル作成
  • : > FILE / > FILE — シェル組込のリダイレクトで空ファイル作成

FAQ

Q: ファイルを上書きしてしまった?
A: いいえ。touch既存ファイルの中身は変更しません。時刻だけが更新されます。

Q: 1000 個のファイルを連番で作りたい
A: Bash のブレース展開: touch file_{001..1000}.txt 一発。

Q: シンボリックリンクの先のファイルを更新したくない
A: -h--no-dereference)でリンク自体を対象にできる(対応している OS のみ)。