
Linuxでテキストファイル作成したいです。



Linuxサーバーでファイル作成するのは基本ですね。コマンドでテキストファイル作成のやり方を教えますよ。
という事で今回はLinuxサーバーでテキストファイル作成する時のコマンドを教えます。
筆者はWindowsサーバーやLinuxサーバーの運用構築を11年以上おこなってきました。その経験を元に知識を公開していきます!
LinuxサーバーのCentOSコマンドでファイル作成する方法


Linuxサーバーでファイルを作成するには以下のコマンドでよく行います。
touch ファイル名
CentOSコマンドではこれでファイルを作成する事が可能です。
ファイル作成するtouchコマンドのオプション
ファイル作成するtouchコマンドのオプションをまとめておきます。
touchのオプション | 効果 |
---|---|
-a | アクセス日時を変更する |
-m | 更新日時を変更する |
-t タイムスタンプ | タイムスタンプを変更する |
実践:CentOSコマンドでファイル作成する方法


では実際にLinuxサーバーのCentOSコマンドでファイル作成してみます。
「file1.txt」のテキストファイルを作成してみます。
touch file1.txt
これで「ls」コマンドで確認すると、結果は作成されています。
CentOSコマンドでファイル作成で失敗する時の回避策


では今度は「-file1.txt」というテキストファイルをコマンドで作成してみます。
touch -file1.txt


結果はエラーが出てしまいました。
エラーが出るファイルを作成する方法
エラーが出るファイルを作成する方法をまとめます。
- –オプションを使う
- ./をつける
–オプションを使いファイル作成する
Linuxで「–」はその後にオプションが来ませんという意味です。
つまりファイル名の「-file1.txt」がオプションとして解釈されているという事です。
touch -- -file1.txt


エラーが出ずファイル作成できました。
./をつけてファイル作成する
Linuxで「./」はその階層という意味です。先程はファイル名の初めが「-」だったためにオプションと解釈された事が原因でした。
今回はファイル名の先頭を「-」にしないように「./」をつけています。
touch ./-file1.txt


エラーが出ずテキストファイル作成できました。


Linuxでその他の作成に関するコマンドの記事
Linuxサーバーでディレクトリ作成コマンドについて


Linuxサーバーでユーザー作成について


まとめ:【Linux】CentOSでコマンドでファイル作成する方法


Linuxサーバーでファイルを作成するコマンドはtouchです。
touch ファイル名
ファイル名の先頭に「-」がつくファイル名はオプションと解釈されて作成できない
その時の回避方法は以下にまとめています。
- –オプションを使う
- ./をつける



LinuxのCentOSコマンドでファイル作成できました!