0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux : touchコマンド

Posted at

今回は「touchコマンド」について紹介します。

touchコマンドとは

touchコマンドは、ファイルを新規作成したり、ファイルの編集日を変更したりする処理に対応しています。

ファイルの新規作成

新規ファイルを作成するコマンドは、以下のようになります。

touch 新規ファイル名

出力例

$ ls -a
.  ..  hoge.txt  test
$ touch sample.txt
$ ls -a
.  ..  hoge.txt  sample.txt  test #sample.txtが新たに作成された

ファイル編集日の変更

この処理では、オプションを使用します。

オプション 処理内容
-d ファイルの編集日を指定変更
-r 他ファイルの編集日と合わせる

※他にもオプションはあるみたいです...

dオプション

  • 指定したファイルの編集日を指定した日時に変更する
touch -d 変更後の日付 ファイル名

出力例
今回は、日付を「2025年4月9日21:05」から「2025年3月9日10:00」に変更した

$ ls -l
合計 8
-rw-rw-r-- 1 yamato yamato   18  4月  9 21:05 hoge.txt #変更前
drwxrwxr-x 2 yamato yamato 4096  3月 25 21:07 test
$ touch -d "2025-3-9 10:00" hoge.txt
$ ls -l
合計 8
-rw-rw-r-- 1 yamato yamato   18  3月  9 10:00 hoge.txt #変更後
drwxrwxr-x 2 yamato yamato 4096  3月 25 21:07 test

rオプション

  • 指定したファイルの編集日を他ファイルの編集日と合わせる
touch -r 変更後の編集日をもつファイル 編集日を変えたいファイル

出力例
sample.txtの編集日をhoge.txtの編集日に変更する

$ ls -l
合計 8
-rw-rw-r-- 1 yamato yamato   18  3月  9 10:00 hoge.txt
-rw-rw-r-- 1 yamato yamato    0  4月  9 21:37 sample.txt # 変更前
drwxrwxr-x 2 yamato yamato 4096  3月 25 21:07 test
$ touch -r hoge.txt sample.txt
$ ls -l
合計 8
-rw-rw-r-- 1 yamato yamato   18  3月  9 10:00 hoge.txt 
-rw-rw-r-- 1 yamato yamato    0  3月  9 10:00 sample.txt # hoge.txtと同じ編集日に変化した
drwxrwxr-x 2 yamato yamato 4096  3月 25 21:07 test

終わりに

少しでも参考になれば幸いです。
間違い等あれば、コメントにて指摘していただけたら幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?