今回はLinuxのコマンドについてまとめました。
以下にコマンドと内容を記載します。
前提
- はじめは以下のような構成になっています。
sample/
└─ linux_sample/
└─ command.txt
└─ .hidden.txt
- 基本的に
linux_sample
ディレクトリでコマンドを実行していきます。 - ホームディレクトリは
/Users/me
です。
コマンド
cd
コマンド
$ cd ディレクトリ名
ディレクトリに移動
例:linux_sample/
からsample/
に移動
linux_sample $ cd ..
sample $
..
はひとつ上の階層を表す
cd
だけの場合はホームディレクトリに移動する
pwd
コマンド
$ pwd
現在の階層パスを表示
例:現在の階層パスを表示
linux_sample $ pwd
/Users/me/XXX/YYY/sample/linux_sample
ls
コマンド
$ ls
カレントディレクトリにあるファイル・ディレクトリを表示
(-a
をつけると隠しファイル・隠しディレクトリも表示)
例:linux_sample/にあるファイルをすべて表示
linux_sample $ ls -a
. .. .DS_Store .hidden.txt command.txt
mkdir
コマンド
$ mkdir ディレクトリ名
ディレクトリを作成
例:dir1/を作成
linux_sample $ mkdir dir1
linux_sample $ ls
command.txt dir1
rmdir
コマンド
$ rmdir ディレクトリ名
空のディレクトリを削除
dir1/を削除
linux_sample $ rmdir dir1
linux_sample $ ls
command.txt
cat
コマンド
$ cat ファイル名
ファイルの中身を全て表示
command.txtの中身を表示
linux_sample $ cat command.txt
Hello World
less
コマンド
$ less ファイル名
ファイルの中身の一部を表示(続きはEnterで表示される)
そのため、ファイルの中身が長い場合はcat
よりこっちの方がいいかも
Vimモードになるので、最後は:q
などで抜ける
tails
コマンド
$ tails ファイル名
ファイルの中身の末尾10行を表示
-n 行数
をつけると表示する行数を指定できる
touch
コマンド
$ touch ファイル名
ファイルを作成
例:command2.txtを作成
linux_sample $ touch command2.txt
linux_sample $ ls
command.txt command2.txt
rm
コマンド
$ rm ファイル名
ファイルを削除
-r
をつけるとディレクトリを削除
GUIでの削除と違いゴミ箱に行かず完全に消去されるため、復元できない
例:command2.txtを削除
linux_sample $ rm command2.txt
linux_sample $ ls
command.txt
mv
コマンド
- 名前変更
$ mv 変更前ファイル・ディレクトリ名 変更後ファイル・ディレクトリ名
ファイル・ディレクトリ名を変更
例:command.txtをcommand1.txtに変更
linux_sample $ mv command.txt command1.txt
linux_sample $ ls
command1.txt
- 移動
$ mv ファイル・ディレクトリ名 移動先ディレクトリ
ファイル・ディレクトリを移動
cp
コマンド
$ cp コピー元ファイル名 コピー後ファイル名
ファイルを名前を指定してコピー
ディレクトリのコピーはできない
commmand1.txtのコピー
linux_sample $ cp command1.txt command1_copy.txt
linux_sample $ ls
command1.txt command1_copy.txt
ln
コマンド
$ ln 元ファイル・ディレクトリ名 リンク名
例:command1.txtのリンクを作成
linux_sample $ ln command1.txt command1-link.txt
linux_sample $ ls
command1-link.txt command1.txt command1_copy.txt
linux_sample $ cat command1_copy.txt
Hello World
find
コマンド
$ find ファイル名・ディレクトリ名
ファイルを検索
例:command1.txtを検索
linux_sample $ find command1.txt
command1.txt
chmod
コマンド
$ chmod オプション ファイル名・ディレクトリ名
権限を変更
command1.txtをすべてのユーザーに対して読み出し権限のみにする
chmod 444 command1.txt
chown
コマンド
$ chown 所有者or所有者:グループ ファイル名orディレクトリ名
所有権を変更
例:linux_sample
ディレクトリの所有者をyamadaに変更する
chown yamada linux_sample
ps
コマンド
$ ps
起動しているプロセスを表示
例:起動しているプロセスを表示
linux_sample $ ps
PID TTY TIME CMD
522 ttys000 0:00.03 -bash
545 ttys001 0:00.03 -bash
601 ttys002 0:00.02 -bash
614 ttys003 0:00.67 -bash
29659 ttys004 0:00.02 -bash
kill
コマンド
$ kill プロセスID
プロセスを終了させる
例:プロセスの終了
$ ps ax | grep gedit
16619 ? Sl 0:01 gedit
$ kill 16619