hana_shinのLinux技術ブログ

Linuxの技術情報を掲載しています。特にネットワークをメインに掲載していきます。

touchコマンドの使い方



1 touchコマンドとは?

空のファイルを作成したり、ファイルのタイムスタンプを変更するコマンドです。

2 検証環境

CentOS版数は以下のとおりです。

[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

カーネル版数は以下のとおりです。

[root@server ~]# uname -r
3.10.0-1160.el7.x86_64

3 タイムスタンプについて

ファイルのタイムスタンプには以下のものがあります。

種別 意味 概要
atime(Access Time) 最終アクセス時刻 cat,less等でファイルを読み込むと更新される
mtime(Modify Time) 最終変更時刻 ファイルの内容を書き換えると更新される
ctime(Change Time) 最終属性変更時刻 ファイルの属性(パーミッションや所有者など)を変更すると更新される

タイムスタンプが更新される状況を確認してみます。まず、touchコマンドを使って、空のファイルを作成します。

[root@server ~]# touch test.txt

タイムスタンプを確認します。atime,mtime,ctimeが全て同じ時刻になっていることがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 0               Blocks: 0          IO Block: 4096   通常の空ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 18:19:19.106080966 +0900
Modify: 2022-02-26 18:19:19.106080966 +0900
Change: 2022-02-26 18:19:19.106080966 +0900
 Birth: -

ファイルのパーミッションを644から744に変更してみます。

[root@server ~]# chmod 744 test.txt

パーミッションを変更すると、ctimeだけが変更されたことがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 0               Blocks: 0          IO Block: 4096   通常の空ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 18:19:19.106080966 +0900
Modify: 2022-02-26 18:19:19.106080966 +0900
Change: 2022-02-26 18:21:13.969696071 +0900
 Birth: -

次に、ファイルを読み込んでみます。

[root@server ~]# cat test.txt

ファイルを読み込むと、atimeだけが変更されたことがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 0               Blocks: 0          IO Block: 4096   通常の空ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 18:25:55.562204031 +0900
Modify: 2022-02-26 18:19:19.106080966 +0900
Change: 2022-02-26 18:21:13.969696071 +0900
 Birth: -

最後に、ファイルの内容を書き換えてみます。

[root@server ~]# echo 1 > test.txt

ファイルに書き込みをすると、mtimeとctimeが変更されたことがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 18:25:55.562204031 +0900
Modify: 2022-02-26 18:27:38.921757536 +0900
Change: 2022-02-26 18:27:38.921757536 +0900
 Birth: -

4 空のファイルを作成する方法

ファイル名を指定してtouchコマンドを実行します。

[root@server ~]# touch test.txt

ファイルサイズが0の空ファイルが作成されたことがわかります。

[root@server ~]# ls -l test.txt
-rw-r--r--. 1 root root 0  2月 26 17:31 test.txt

5 mtime,atimeを指定した時刻に変更する方法(-t)

空のファイルを作成します。

[root@server ~]# touch test.txt

ファイルのタイムスタンプを確認します。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 22:14:38.759588680 +0900
Modify: 2022-02-26 22:14:38.759588680 +0900
Change: 2022-02-26 22:14:38.759588680 +0900
 Birth: -

atime,mtimeを2020年11月22日3時4分5秒に変更します。

[root@server ~]# touch -t 202011220304.05 test.txt

ファイルのタイムスタンプを確認します。atime,mtimeが2020年11月22日3時4分5秒に変更されたことがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-11-22 03:04:05.000000000 +0900
Modify: 2020-11-22 03:04:05.000000000 +0900
Change: 2022-02-26 22:14:53.062685414 +0900
 Birth: -

6 atimeを指定した時刻に変更する方法(-at)

空のファイルを作成します。

[root@server ~]# touch test.txt

ファイルのタイムスタンプを確認します。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 19:36:52.470215476 +0900
Modify: 2022-02-26 19:36:52.470215476 +0900
Change: 2022-02-26 19:36:52.470215476 +0900
 Birth: -

atimeを2020年11月22日3時4分5秒に変更します。

[root@server ~]# touch -at 202011220304.05 test.txt

ファイルのタイムスタンプを確認します。atimeが2020年11月22日3時4分5秒に変更されたことがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-11-22 03:04:05.000000000 +0900
Modify: 2022-02-26 19:36:52.470215476 +0900
Change: 2022-02-26 19:37:31.572447733 +0900
 Birth: -

7 mtimeを指定した時刻に変更する方法(-mt)

空のファイルを作成します。

[root@server ~]# touch test.txt

ファイルのタイムスタンプを確認します。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 19:40:01.246336753 +0900
Modify: 2022-02-26 19:40:01.246336753 +0900
Change: 2022-02-26 19:40:01.246336753 +0900
 Birth: -

mtimeを2020年11月22日3時4分5秒に変更します。

[root@server ~]# touch -mt 202011220304.05 test.txt

ファイルのタイムスタンプを確認します。mtimeが2020年11月22日3時4分5秒に変更されたことがわかります。

[root@server ~]# stat test.txt
  File: `test.txt'
  Size: 2               Blocks: 8          IO Block: 4096   通常ファイル
Device: 803h/2051d      Inode: 33574996    Links: 1
Access: (0744/-rwxr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 19:40:01.246336753 +0900
Modify: 2020-11-22 03:04:05.000000000 +0900
Change: 2022-02-26 19:40:19.070442623 +0900
 Birth: -

lsコマンドが表示する時刻はmtimeの時刻であることがわかります。

[root@server ~]# ls --full-time test.txt
-rw-r--r--. 1 root root 2 2020-11-22 03:04:05.000000000 +0900 test.txt

8 タイムスタンプ(mtme,atime)を2つのファイルで同じにする方法

まず、test1.txtを作成します。

[root@server ~]# touch test1.txt

test1.txtのタイムスタンプを確認します。

[root@server ~]# stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 4096   通常の空ファイル
Device: 803h/2051d      Inode: 33584551    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 19:46:28.407652184 +0900
Modify: 2022-02-26 19:46:28.407652184 +0900
Change: 2022-02-26 19:46:28.407652184 +0900
 Birth: -

次に、test2.txtを作成します。

[root@server ~]# touch -r test1.txt test2.txt

test2.txtのタイムスタンプを確認します。atime,mtimeがtest1.txtのatime,mtimeと同じ時刻であることがわかります。

[root@server ~]# stat test2.txt
  File: `test2.txt'
  Size: 0               Blocks: 0          IO Block: 4096   通常の空ファイル
Device: 803h/2051d      Inode: 33584554    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-02-26 19:46:28.407652184 +0900
Modify: 2022-02-26 19:46:28.407652184 +0900
Change: 2022-02-26 19:47:48.099129716 +0900
 Birth: -

9 その他(lsコマンドのタイムスタンプオプションについて)

"-u"は、atimeを表示するオプションです。

[root@server ~]# ls --full-time -u test.txt
-rw-r--r--. 1 root root 2 2022-02-26 23:18:20.899574007 +0900 test.txt

"-c"は、ctimeを表示するオプションです。

[root@server ~]# ls --full-time -c test.txt
-rw-r--r--. 1 root root 2 2022-02-26 23:18:20.899574007 +0900 test.txt

オプションを指定しないとmtimeが表示されます。

[root@server ~]# ls --full-time test.txt
-rw-r--r--. 1 root root 2 2022-02-26 23:18:20.899574007 +0900 test.txt

Z 参考情報

私が業務や記事執筆で参考にした書籍を以下のページに記載します。
Linux技術のスキルアップをしよう! - hana_shinのLinux技術ブログ