2010-02-12
Linuxでファイルを空にする5つの方法
ログファイルを空にしたいときなど、いつも忘れるので・・・。
: > file
% : > file
これが一番シンプルですね。 : という区切りのコマンドがあり、それを file にリダイレクトしてあげるとファイルが空になります。man bash には以下のように記述されています。おそらくzshでは一番シンプルです。
・・・ A : separates the event specification from the word designator. ・・・
・
・
・
SHELL BUILTIN COMMANDS
Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options. For example, the :, true, false, and test builtins do not accept options. Also, please note that while executing in non-inter-active mode and while in posix mode, any special builtin (like ., :, break, continue, eval, exec, exit, export, readonly, return, set, shift, source, times, trap, unset) exiting with a non-zero status causes the shell to stop execution.
: [arguments]
No effect; the command does nothing beyond expanding arguments and performing any speci-fied redirections. A zero exit code is returned.
・
・
・
cp /dev/null file
% cp /dev/null file
/dev/null という擬似デバイスを利用して file を空にする方法です。
参考
echo -n > file
% echo -n > file
-n オプションがないと改行が入ってしまいます。
参考
cat > file
% cat > file
その後 ctrl-D や ctrl-C などで抜ける。
> file
% > file
単なるリダイレクトでもできますが、zshの場合、cat > と同じように、その後 ctrl-D や ctrl-C などで抜ける必要があります。bashはCtrl-Dは必要ないので、bashでは一番シンプルかもしれせん。
実験環境
| OS | CentOS 5 |
| シェル | zsh |
スペシャルサンクス
Twitterで色々教えて頂きました。感謝ですm(_ _)m
- Twitterの Catshop (@Catshop)
- Twitterの teramako (@teramako)
- Twitterの WATANABE Tetsuya (@nbtty)
- Twitterの はぇ? (@haxe)
- Twitterの なかだ のぷぎゃー (@n0kada)
まとめ
タイトルに5つの方法と書きましたが、もっと他にあると思いますので、教えて下さい><
- 1162 http://pipes.yahoo.com/pipes/pipe.info?_id=tDfBdGWF3RGl9XNm1L3fcQ
- 173 http://d.hatena.ne.jp/itokoichi/
- 124 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=cnE&q=flex+videodisplay+playheadTime+ずれ&btnG=検索&lr=&aq=f&oq=
- 117 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GGLL_jaJP319JP319&q=windows+ext3
- 63 http://pipes.yahoo.com/pipes/pipe.info?_id=12e453e301454b799b3ac6642aa089b5
- 44 http://www.google.co.jp/search?hl=ja&source=hp&q=規定のブラウザ+変更&lr=&aq=1r&oq=kitei
- 43 http://okyuu.com/ja/tips/1299
- 41 http://typex2.wordpress.com/2010/01/30/mac-os-xのfinderではパスワード付きzipが解凍できない!?/
- 38 http://www.google.co.jp/search?client=firefox-a&rls=org.mozilla:ja:official&channel=s&hl=ja&source=hp&q=既定のブラウザ&lr=&btnG=Google+検索
- 32 http://www.google.co.jp/search?hl=ja&source=hp&q=既定のブラウザ+変更&lr=&aq=0&oq=既定


