Terminalの基本を知る

はじめに

こちらに取り組んでいきます。
fjord.jp

コマンド

lsコマンド

$ ls
$ ls -a   #全て表示
$ ls -l   #ファイルの詳細も表示する
$ ls -1   #リストを縦に並べる
$ ls -t   # 更新時間順に並べる
$ ls -lS #降順にして一覧表示
$ ls -lSr # 昇順にして一覧表示
$ ls -R #サブディレクトリの中も表示(ホームディレクトリだと大量に表示されるので注意)

ファイルやディレクトリの一覧を表示するコマンド。
listの略。

参考リンク
lsコマンドの使い方と覚えたい15のオプション【Linuxコマンド集】

pwdコマンド

$ pwd

現在いるディレクトリの絶対パスを表示するコマンド。
Print Working Directoryの略。

echoコマンド

$ /bin/echo helloworld
helloworld
$ hoge = 3 #変数に代入するときは半角スペースは入れない
-bash: hoge: command not found
$ word_1="pen"
$ word_2="pineapple"
$ echo word_1+word_2
word_1+word_2
$ echo $word_1$word_2 #参照するときは、$をつける 文字列の連結するにはそのまま並べる
penpineapple
$ word_3="apple"
$ echo $word_1-$word_2-$word_3-$word_1
pen-pineapple-apple-pen #古い
$ exit # 次回ターミナルを開くと、$word_? は参照できなくなっていた。一時的な変数?
(=> シェル変数)

シェル変数は、そのシェル内のみ有効。
シェル変数と環境変数の違いをコマンドラインで確認する - Qiita

(ターミナルはGUI上でCUI操作をするためのもの、シェルはOSとの橋渡しをするもの)
【初心者向け】シェル・ターミナル・コンソールの違いとは?

環境変数として定義すると、子プロセスでも有効となる
(子プロセスとは?-> 後日調べる)

cdコマンド

$ cd /bin
$ pwd
/bin

現在のワーキングディレクトリを変更するコマンド。
Change Directoryの略。

manコマンド

$ man ls

LS(1)                     BSD General Commands Manual                    LS(1)

NAME
     ls -- list directory contents

SYNOPSIS
     ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls
     displays its name as well as any requested, associated information.  For
     each operand that names a file of type directory, ls displays the names
     of files contained within that directory, as well as any requested, asso-
     ciated information.

     If no operands are given, the contents of the current directory are dis-
     played.  If more than one operand is given, non-directory operands are
     displayed first; directory and non-directory operands are sorted sepa-
     rately and in lexicographical order.

     The following options are available:
     #  以下略

マニュアル読むコマンド。

curlコマンド

$ curl -I http://abehiroshi.la.coocan.jp/
HTTP/1.1 200 OK
Date: Sun, 22 Sep 2019 01:07:39 GMT
Content-Type: text/html
Content-Length: 336
Connection: keep-alive
Last-Modified: Mon, 05 Sep 2016 05:15:36 GMT
ETag: "150-53bbbc52a6bee"
Accept-Ranges: bytes
Server: Apache

Client for URLs”の略
-I オプション ヘッダー情報を取得

openコマンド

$ open http://abehiroshi.la.coocan.jp/ #早い
$ open . # Finderでカレントディレクトリを表示

関連付けられているソフトでひらく。

saykanaコマンド, sayコマンド

$ saykana -s 10 ゆっくりしていってね!!!

SayKanaをダウンロード
https://www.a-quest.com/quickware/saykana/

$ say すいすい  すいすい  水曜日 -r 200 --interactive
すいすい すいすい 水曜日

sayコマンドはデフォルトで使える。
-rを使うと、1分間で喋る速度を指定。
--interactiveを使うと、今しゃべっているところをハイライトしてくれる。
カラオケ機能?

Homebrew

$ sudo chown -R `whoami` /usr/local
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

/usr/localディレクトリの所有者を自分のユーザーに変更
su(Super User)でdo
whoami 自分のユーザー名を表示するコマンド

ruby
- e 指定されたスクリプトRubyで実行する

curl
-f(fail) 失敗してもエラーメッセージを表示しない
-s(silent) 実行中のメッセージを表示しない
-S (show-error) 進捗は表示しないが、エラーは表示する(sと併用して使う)
-L リダイレクト設定されている URL にアクセスした時、そのリダイレクト先へ飛ぶ(デフォルトでは飛ばない)

curl option 覚え書き - Qiita

Homebrewでtreeをインストール、アンインストール

$ brew install tree
$ brew uninstall tree

treeはカレントディレクトリ下のファイル構造をツリーで表示するコマンド。

ファイルの作成

$ touch hoge.html

ディレクトリの作成

$ mkdir sample
$ mkdir -p sample/hoge  # エラーを表示せず、記述したディレクトリがなければ作成

https://eng-entrance.com/linux-command-mkdir#-p--parents

ファイルのコピー

$ cp atti kotti   # あっちファイルをこっちディレクトリにコピー
$ cp sample sample2  # sampleファイルをコピーし、sample2と命名する
$ cp -r hoge hoge2  # hogeディレクトリを丸ごとコピーし、hoge2と命名する

ファイルの移動

$ mv sample1 myhouse   #sample1ファイルをmyhouseディレクトリに移動

ファイルの削除

$ rm obutu.txt # 消毒
$ rm -r obutu # ディレクトリごと消毒
$ sudo rm -rf / # この世界ごと消毒(最近はOSが待ったを一応かけてくれるとか)

-f 削除するかどうかのメッセージを省略

ファイルを表示

$ cat /etc/hosts

猫がファイルを表示してくださるコマンド
conCATenate(連結させる、の意) で、引数に渡した2つのファイルを繋げて表示するコマンドだが、 1つだけ指定して単に中身を表示する用途で主に使われる。

shebang(シバン)

$ vim echo-hello
#!/bin/sh
echo hello
:wq
$ ls -l echo-hello
-rw-r--r--  1 mac  staff  30  9 22 11:56 echo-hello    # 所有者に読み、書き権限
$ chmod u+x echo-hello  # 所有者に実行権限を付与
$ ls -l echo-hello
-rwxr--r--  1 mac  staff  30  9 22 11:56 echo-hello
$ ./echo-hello
hello world!

#! <= shebang <= sharp(#) bang(!)

「1行目の最初の二文字が#!だったら、その後に書いてあるコマンドに2行目以降の全てを渡す」機能

$ mkdir bin
$ mv echo-hello bin/
$ export PATH=$PATH:~/bin
$ echo-hello
hello world!

export PATH=$PATH:追加したいコマンド検索パス
環境変数を設定する
~(チルダ) = $HOME