8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

先頭が -(ハイフン) なファイルの使い道を考えてみた

Posted at

概要

個人的に linux や Unix を触り初めて一通りのファイル操作ができるようになってきた段階で遭遇する、-(ハイフン)が先頭のファイル問題について思うところをまとめてみました。

-(ハイフン)が先頭のファイルの操作について

環境

  • MacOS10.9

操作

  • -(ハイフン)が先頭にあるファイルを作成して確認
$ touch -- -hoge.txt
$ ls -- -hoge.txt
-hoge.txt
$ ls -la ./-hoge.txt
-rw-r--r--  1 hoge  hoge Users  0  8 19 14:21 ./-hoge.txt
$ ls -la -- -hoge.txt
-rw-r--r--  1 hoge  hoge Users  0  8 19 14:21 -hoge.txt

要は -- で打ち消すんですね。

man bash

man bash-- の項目を以下に引用しておきます。

A -- signals the end of options and disables further option
processing. Any arguments after the -- are treated as file-
names and arguments. An argument of - is equivalent to --.

オプションの終わりを示し、さらにオプションを無効にする。
-- の後の引数は -ファイルとして扱われる。

使い所を考えてみる

その1

  • タイミングを間違えて実行するとマズイことになるスクリプト郡には - を先頭につけてスクリプト化しておくという運用方法にしてみる
  • 明示的に -- を付けないと怒られるので有効か??
  • ./ でオペレーションをする文化だった場合あまり効果なし??
  • タブ補完等で流れで作業してオペレーションミスが起こすということは防げそう
  • (そもそもそんな危険な作業はできるだけ減らしていくことが重要なのでは...

その2

  • 新入社員に配布するPCや開発環境に予め仕込んで地味な洗礼を与える
  • あっさりと消されてしまうか、少しだけ悶絶してもらえるかの2択

所感

  • いい勉強になりました。
8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?