I/O Redirection についての学習

Tuyoshi Akiyama
Jul 10, 2017 · 3 min read

input /output redirection とは、コマンドを打って現れる出力を、あるファイルに

入力することができる。逆も然りで、あるファイルからのデータをターミナル上に

出力も可能。やり方としては、

$ cd
$ cd Desktop/
$ ls > sample.txt

ls で得られる出力がsample.txtに書き込まれる。

ただし、$ ls > sample.txtは行うごとに、空のファイルに塗り替えられる。

追加して上書きを行う場合は下記のコマンド

$ ls >> sample.txt

また、ファイルの入力情報をターミナルに表示する方法は

$ ls < sample.txt

Piplines and Filters

シェルでは複数のコマンドを一緒に使用することが可能。

実際にいくつかのコマンドを打って、確認した。

$ ls -lt | head

現在のDirectory内で新しくできたファイル10こが表示される。

$ find . -type f -print | wc -

現在のDirectory内のファイル、SubDirectory全てを合わせた数が表示される。

またこの際に、よく使われるフィルターのリストが以下リンク先となる。

  • sort
    ソートしたインプットを、出力する
  • uniq
    ソートされたデータを受け取り、重複したものを取り除く
  • fmt
    テキストファイルを決められた桁に整形する
  • pr
    プリントようにデータを整形する
  • head
    ファイルのヘッダーを取得する際に用いる
  • tail
    logファイルなどの、ファイルの末尾を取得する際に用いる
  • tr
    文字の置換に用いる
  • sed
    文字変換がtrより得意なeditor
    http://www.grymoire.com/Unix/Sed.html
  • awk
    プログラミング言語のようにデータファイルに処理を走らせることができるよう

実際に業務でエラーログ見たりするときにtail -f とか使っていたことを、思い出しました。

ただ、Linuxは奥が深くて、awk一つを見ても3、4日はかかりそうな情報量。

一応、メモ用に

$ ls
$ ls < sample.txt

    Tuyoshi Akiyama

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade