LoginSignup
10
9

More than 5 years have passed since last update.

Linuxの基礎的なところをおさらいしてみた(コマンド類)

Last updated at Posted at 2016-04-13

主要コマンド一覧(メモ)

pwd :
今いるディレクトリを表示する(Print Working Directory)

(ex).bash
[root@localhost ~]# pwd
/root

.. :
親のディレクトリ。.なら現在のディレクトリ

(ex).bash
[root@localhost var]# cd ..
[root@localhost /]# 

less
less : catより有能まである標準出力系(ファイルの中身を閲覧し易い形で閲覧できる)

(ex).bash
[root@localhost ~]# less t.txt 
→閲覧モードに移行+[q]で終わり

less -N (FileName)で行番号付きで閲覧できる。


ls
ls -a :
通常のlsとは違って.configなどの隠しファイルも列挙するls。

(ex).bash
[root@localhost ~]# ls -a
.              .bash_history  .cache   .esd_auth  .gnote           .gvfs            .pulse         .xsession-errors      install.log.syslog  デスクトップ  画像
..             .bash_logout 

ls -F :
ディレクトリ名に/'(スラッシュ)を,実行ファイルに*'(アスタリスク),シンボリックリンクに`@'(アットマーク)を付加

(ex).bash
[root@localhost ~]# ls -F
anaconda-ks.cfg  install.log  install.log.syslog  t.txt  ダウンロード/  テンプレート/  デスクトップ/  ドキュメント/  ビデオ/  音楽/  画像/  公開/

find (DirName) -name (fileName) :
DirName以下のfileNameを検索する。

(ex).bash
[root@localhost ~]# find /usr -name rgb.txt
/usr/share/X11/rgb.txt

-maxdepth 4とかにすると4階層までしか走査しない。ネストが深いディレクトリ構造をしてると結構時間がかかるのでこのオプションをつけると処理がめっちゃ早い!


grep :
指定された文字列をファイルの中から検索する

(ex).bash
[root@localhost ~]# grep pr t.txt 
foomatic              fprintd.conf   fstab
iproute2              issue      issue.net

rmdir : 空のディレクトリを消す(空じゃないやつは消せない。)

(ex).bash
[root@localhost ~]# rmdir testFolder1/
rmdir: failed to remove `testFolder1/': ディレクトリは空ではありません

(空にしたあと)
[root@localhost ~]# rmdir testFolder1/

su - (ユーザ名) :
現在のユーザから別のユーザに切り替える。

(ex).bash
[root@localhost ~]# su - user01
[user01@localhost ~]$

注意点:
root → user01の例を上げたけれど、もしまたrootに戻りたい場合は、su - userNameで移行するのではなく、$exitにする。
(こうしないと、su - userNameで移行する前の人のシェルが立ち上がったままでメモリが消費されるため)


chown : 指定されたファイルの所有権をユーザORグループの所有権に変更する。
これはchmodと合わせて力を発揮する。(chmod u,g,oに関わってくる。)

(ex).bash
(UserとGroupに読み権限を与えてGroupには書き込み権限を与えない)
[root@localhost /]# chmod 750 testFile 
[root@localhost /]# ls -l testFile
-rwxr-x---    1 root root     0  4月 13 15:22 2016 testFolder

(user01というユーザ、user02というグループに所有権を渡す)
[root@localhost /]# chown user01.user02 testFile 

(書き込み権限がないuser02では書き込みができない)
[user02@localhost /]$ echo miku >> testFile 
-bash: testFile: 許可がありません

(書き込み権限を与えたuser01では書き込みができる)
[user01@localhost /]$ echo miku01 >> testFile
[user01@localhost /]$ cat testFile 
miku01

userAddすると自動的にユーザ名と同じ名前のグループができあがる。
本来であればわかりやすさのためにGroupAdd,usermodを使うのが良さそう。


ln :
ショートカットのような仕組みを作れる。

ディレクトリの奥底にあるのをホームディレクトリですぐにアクセスできるようにできる。

(ex).bash
[root@localhost ~]# ln -s /etc/httpd/conf symlink
[root@localhost ~]# cd symlink/
[root@localhost symlink]# ls
httpd.conf  magic

注意点:
unlink (symlink名)
でリンクを消せる。rmよりも安全かも。

rmを使いましょう。


リダイレクト
>> : 標準出力のリダイレクトの追記

(ex).bash
[root@localhost ~]# ls -l >> test.txt
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  t.txt  test.txt  testFolder  ダウンロード  テンプレート  デスクトップ  ドキュメント  ビデオ  音楽  画像  公開
[root@localhost ~]# cat test.txt 
合計 124
…Qiitaでは省略

> : 標準出力のリダイレクトの上書き

(ex).bash
(ls -lの結果をtest2.txtに書く)
[root@localhost testFolder]# ls -l >> test2.txt
[root@localhost testFolder]# cat test2.txt 
合計 4
-rwx------ 1 user01 user01 5  4月 13 15:12 2016 test.txt
-rw-r--r-- 1 root   root   0  4月 13 16:30 2016 test2.txt

(mikusanという文字列でファイルを上書きする)
[root@localhost testFolder]# echo mikusan > test2.txt
[root@localhost testFolder]# cat test2.txt 
mikusan

< : 標準入力のリダイレクト

(ex).bash
[root@localhost ~]# echo 2+3 >> hikisuu.txt
[root@localhost ~]# bc < hikisuu.txt 
5

クソ長い引数とかを使いたい時にこれを使えば良さそう。

&> : 標準出力と標準エラー出力の両方をリダイレクト

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9