1. 概要

このチュートリアルでは、touchコマンドについて学習します。 つまり、このコマンドを使用すると、ファイルまたはディレクトリの最終変更時刻と最終アクセス時刻を更新できます。

そこで、このコマンドとそのさまざまなオプションの使用方法に焦点を当てます。

ここに示すすべてのコマンドをBashを使用してテストしたことに注意してください。 ただし、POSIX準拠のシェルで動作する必要があります。

2. デフォルトの動作

touch を実行すると、ファイルシステム上の1つ以上のファイルまたはディレクトリが更新され、最終変更時刻と最終アクセス時刻が現在のシステム時刻に設定されます。

したがって、今日の日付が2020年2月1日で、時刻が午前7時であるとします。

説明したように、このコマンドはexample-file.txtファイルの両方のタイムスタンプを更新します。

# ls -l example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan  1 20:00 example-file.txt

# touch example-file.txt

# ls -l example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 example-file.txt

2.1. 新しいファイルの作成

さらに、指定されたファイルが存在しない場合、コマンドは空のファイルを作成し、それに応じて時間を設定します。

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

# touch sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

新しいファイルが作成された場合、新しく作成されたファイルのアクセス許可は、デフォルトで、指定されたファイルシステムの標準のアクセス許可になります。

2.2. 複数のファイルまたはディレクトリがある

複数のファイルまたはディレクトリが指定されている場合、コマンドはそれらすべてのタイムスタンプを更新します。

# ls -l example-file.txt sample-file.txt example-dir
drw-r--r--  1 baeldung  baeldung  0 Jan  1 22:00 example-dir
-rw-r--r--  1 baeldung  baeldung  0 Jan  1 20:00 example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan  1 16:00 sample-file.txt

# touch example-file.txt sample-file.txt example-dir

# ls -l example-file.txt sample-file.txt example-dir
drw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 example-dir
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 example-file.txt 
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

3. 特定の時間を定義する

オプションで、-tオプションを使用して設定するタイムスタンプを定義できます。 その結果、これによりシステム時刻を使用するデフォルトの動作が変更され、代わりにオプションの設定で指定された時刻が使用されます。 このオプションで時間を指定するために使用される形式は、「 [[CC] YY] MMDDhhmm[.SS]」です。

  • CC :世紀
  • YY :年の2番目の2桁。 YY が指定されているが、 CC が指定されていない場合、 YY の値が69〜99の場合、CCの値は19になります。 それ以外の場合は、CCの値20が使用されます
  • MM :その年の月(01-12)
  • DD :月の日(01-31)
  • hh :その日の時間(00-23)
  • mm :時間の分(00-59)
  • SS :分の秒(00-59)

したがって、定義されたタイムスタンプを使用してファイルまたはディレクトリを更新するには、次のコマンドを実行できます。

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt
 
# touch -t 202001262359.59 sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 26 23:59 sample-file.txt

CCおよびYYが指定されていない場合、デフォルトで現在の年になります。

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# touch -t 01282359.59 sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 28 23:59 sample-file.txt

SS が指定されていない場合、値はデフォルトで0になります。

4. 時間の調整

デフォルトの動作のもう1つの代替方法は、明示的に設定したりシステム時刻を使用したりする代わりに、時刻を相対的に変更することです。 -A を使用すると、ファイルまたはディレクトリのタイムスタンプを現在のタイムスタンプと比較して調整できます。 時間調整の指定に使用される形式は、「 [-] [[hh] mm]SS」です。

  • -:調整を負にします
  • hh :時間数(00-99)
  • mm :分数(00-59)
  • SS :秒数(00-59)

したがって、アクセス時間を-25時間調整するには、次のように実行します。

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 27 22:59 sample-file.txt

# touch -A -250000 sample-file.txt

# ls -l sample-file.txt 
-rw-r--r--  1 baeldung  baeldung  0 Jan 26 21:59 sample-file.txt

5. その他のオプション

5.1. 参照ファイルからのタイムスタンプの使用

-r オプションは、更新時に使用するタイムスタンプとして、指定された参照ファイルのタイムスタンプを使用します。 ここで、 -r を指定してコマンドを実行すると、 example-file.txt からタイムスタンプが取得され、sample-file.txtに適用されます。

# ls -l example-file.txt sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 17:00 example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 23 22:45 sample-file.txt

# touch -r example-file.txt sample-file.txt

# ls -l example-file.txt sample-file.txt
-rw-r--r-- 1 baeldung baeldung 0 Feb 1 17:00 example-file.txt 
-rw-r--r-- 1 baeldung baeldung 0 Feb 1 17:00 sample-file.txt

5.2. シンボリックリンクの操作

ファイルまたはディレクトリへのシンボリックリンクでコマンドを実行すると、リンクターゲットのタイムスタンプが更新されます。

# ls -l example-file.txt link-to-example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 23 22:00 example-file.txt
lrw-r--r--  1 baeldung  baeldung  0 Jan 23 22:30 link-to-example-file.txt -> example-file.txt

# touch example-file-link.txt

# ls -l example-file.txt link-to-example-file.txt
-rw-r--r--  1 baeldung  baeldung 0  Feb  1 07:00 example-file.txt
lrw-r--r--  1 baeldung  baeldung 0  Jan 23 22:30 link-to-example-file.txt -> example-file.txt

-h を使用すると、ターゲット自体ではなく、ターゲットファイルまたはディレクトリへのシンボリックリンクのタイムスタンプを更新できます。

5.3. ファイル作成の防止

-cでファイル作成を無効にできます。 このオプションを指定して実行すると、ファイルが存在しない場合、コマンドは何もしません。

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

# touch -c sample-file.txt

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

5.4. 最終アクセス時間のみの更新

ファイルまたはディレクトリの最終アクセス時刻のみを更新する場合は、-aオプションを使用できます。

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# ls -l -u sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt  

# touch -a sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# ls -l -u sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

前に説明したように、デフォルトの動作では、最終アクセス時刻と最終変更時刻の両方が更新されます。 このオプションを使用すると、両方のタイムスタンプを設定するデフォルトの動作が上書きされます。 -aと-mを一緒に使用すると、両方のタイムスタンプを更新するデフォルトの動作になります。

5.5. 最終変更時刻のみを更新する

同様に、ファイルまたはディレクトリの最終変更時刻のみを更新する場合は、-mオプションを使用できます。

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# ls -l -u sample-file.txt 
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# touch -m sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

# ls -l -u sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

6.  互換性

古いBSDシステムのtouchとの下位互換性を維持するために、いくつかの廃止された機能とオプションが引き続きサポートされています。

このコマンドは、古いバージョンのtouchを引き続きサポートします。 これにより、最初に「 MMDDhhmm [YY] 」の形式で時刻形式を指定し、次にファイルシステムで更新するファイルまたはディレクトリの名前を指定できます。

MM DD hh 、および mm の文字ペアは、-tで指定された対応する文字のペアと同様に扱われます。オプション。 YY が39から99の間の場合、その年は20世紀になります。 それ以外の場合、年は21世紀です。

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb 10 12:00 sample-file.txt

# touch 0123000020 sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 23 00:00 sample-file.txt

このコマンドには、-fオプションを使用してファイルを強制的に更新する機能がありました。 ただし、このオプションは現在無視されています。

7. 結論

この記事では、 touch コマンドラインユーティリティ、そのさまざまなオプション、およびその下位互換性について説明しました。

結論として、 touch は、ファイルに変更を加える必要がないが、最後にアクセスした時刻または最後に変更した時刻を更新したい場合に便利です。