目的
12月から運よく業務でデータ解析に携わることになりました。
ただ、伴って、Linuxコマンドを使用する機会も増えてきましたので、復習用にまとめてみました。
ドットインストール様の講座を参考にさせて頂いております。
これが無料とは信じられませんが...
参照動画
環境
ローカル開発環境のCentOS上を利用
↓ 環境立ち上げ方法
https://dotinstall.com/lessons/basic_localdev_win_v2
*versionが最新版だと上手く立ち上がらない可能性があります。
コマンド一覧
pwd:現在のディレクトリを確認
[vagrant@localhost ~]$ pwd
/home/vagrant
clear, Ctrl + l:画面のリセット
cd:ディレクトリ移動
cd [ディレクトリ名]:[ディレクトリ名]に移動
[vagrant@localhost ~]$ pwd
/home/vagrant
[vagrant@localhost ~]$ cd unix_lessons/
[vagrant@localhost unix_lessons]$ pwd
/home/vagrant/unix_lessons
cd ..:一つ上のディレクトリへ移動
[vagrant@localhost unix_lessons]$ pwd
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ cd ..
[vagrant@localhost ~]$ pwd
/home/vagrant
cd -:直前のディレクトリに戻る
[vagrant@localhost unix_lessons]$ pwd
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ cd ..
[vagrant@localhost ~]$ pwd
/home/vagrant
[vagrant@localhost ~]$ cd -
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ pwd
/home/vagrant/unix_lessons
cd !$:直前のコマンドに渡した最後の文字列を引用してファイル移動
[vagrant@localhost unix_lessons]$ ls myapp/
hello.txt
[vagrant@localhost unix_lessons]$ cd !$
cd myapp/
[vagrant@localhost myapp]$
myappへ移動成功
ls:下位ファイルの確認
mkdir:ディレクトリの作成
[vagrant@localhost unix_lessons]$ mkdir myapp
[vagrant@localhost unix_lessons]$ ls
myapp
cp :ファイルのコピー
cp [コピーするファイル] [コピー先]
同じディレクトリ内にmyappをmyapp2の名前でコピー
[vagrant@localhost unix_lessons]$ cp -r myapp myapp2
[vagrant@localhost unix_lessons]$ ls
myapp myapp2
*cp [コピーするファイル] . で同じディレクトリにコピー
*ディレクトリをコピーする場合、「-r」オプションが必要
$ cp -r ./foo /home/hoge/aaa
コピー元の属性を維持する場合は「-p」オプションも併用します。
$ cp -rp ./foo /home/hoge/aaa
mv:ファイルの移動
mv [移動するファイル] [移動先]
myapp3ファイルをmyapp2下位に移動させる
*mkdir -p myapp3/configの「-p」は、app3ファイルがない状態でconfigファイルを作成するのに必要。
[vagrant@localhost unix_lessons]$ ls
myapp myapp2
[vagrant@localhost unix_lessons]$ mkdir -p myapp3/config
[vagrant@localhost unix_lessons]$ ls
myapp myapp2 myapp3
[vagrant@localhost unix_lessons]$ ls myapp3
config
[vagrant@localhost unix_lessons]$ mv myapp3 myapp2
[vagrant@localhost unix_lessons]$ ls myapp2
myapp3
rmdir:ファイルの削除
rmdir [削除したいファイル名]
*ただし、削除できるファイルは空のみ
[vagrant@localhost unix_lessons]$ ls myapp3
config
[vagrant@localhost unix_lessons]$ rmdir myapp2/myapp3/config
[vagrant@localhost unix_lessons]$ ls myapp2/myapp3
*中身があるファイルを削除する場合
[vagrant@localhost unix_lessons]$ rmdir myapp2
rmdir: failed to remove `myapp2': ディレクトリは空ではありません
[vagrant@localhost unix_lessons]$ rm -r myapp2
[vagrant@localhost unix_lessons]$ ls
myapp
cat:ファイルの中身確認
[vagrant@localhost unix_lessons]$ cat ./myapp/hello.txt
less:ファイルの中身確認
catと使い方が異なる
・矢印:スクロール
・Space/ Ctrl+F 一画面先
・Ctrl+B 一画面前
・g 先頭へ移動
・Shift+g 末尾へ移動
・q 終了
・/[検索語] (n:次 Shift+n:前)
Ctrl+c:キャンセル
Ctrl+r コマンドの履歴を検索
history:過去のコマンド一覧を表示
一覧中のコマンドを実行したければ、 ![任意の数字]で実行できる。
! 直前のコマンドを色々引用できる
[vagrant@localhost unix_lessons]$ !pw ←直近でpwから始まるコマンドを実行
pwd
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ !pw:p ←あやふやなら :pで表示だけも可
pwd
[vagrant@localhost unix_lessons]$ !! ←実行
pwd
/home/vagrant/unix_lessons
help, man:コマンドのhelp表示
[vagrant@localhost unix_lessons]$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
ディレクトリを作成する。ただし既にディレクトリがあれば何もしない。
長いオプションに必須の引数は短いオプションにも必須です.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z, --context=CTX set the SELinux security context of each created
directory to CTX
When COREUTILS_CHILD_DEFAULT_ACLS environment variable is set, -p/--parents
option respects default umask and ACLs, as it does in Red Hat Enterprise Linux 7 by default
--help この使い方を表示して終了
--version バージョン情報を表示して終了
Report mkdir bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report mkdir translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'mkdir invocation'
*man [コマンド]でもっと詳しい表示ができる。less表示だが...
vim
vi [ファイル名]でvimを起動
vimにはコマンドモードと編集モードがある。左下に「INSERT」表示があれば編集モード。
〇コマンドモード ⇒ 編集モード:i
〇編集モード ⇒ コマンドモード:Esc
終了・保存する場合はコマンドモードで、
:w 保存
:q 終了
:q! 変更内容を破棄して終了
環境立ち上げ
〇Windows PowerShellで仮想マシンを立ち上げる
PS C:\Users\takuy> cd MyVagrant/MyCentOS
PS C:\Users\takuy\MyVagrant\MyCentOS> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-6.8' version '2.3.4' is up to date...
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
PS C:\Users\takuy\MyVagrant\MyCentOS> vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
〇PuTTyで作成したMyCentOSをloadする
ユーザー名とpassはvagrant
終了するとき
〇PuTTy上でexitコマンド
〇Powershell上で仮想マシンを停止させる
PS C:\Users\takuy\MyVagrant\MyCentOS> vagrant suspend
==> default: Saving VM state and suspending execution...