0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

業務でよく使うLinuxコマンド

Posted at

概要

プログラマの私が業務でよく使うLinuxコマンドを紹介します。

df -h

システムのディスク情報を表示します。
-h:サイズに応じて人間が読みやすい単位で表示します。

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        482M     0  482M   0% /dev
tmpfs           492M     0  492M   0% /dev/shm
tmpfs           492M  476K  492M   1% /run
tmpfs           492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1      8.0G  5.5G  2.6G  69% /
tmpfs            99M     0   99M   0% /run/user/0
tmpfs            99M     0   99M   0% /run/user/1000

du -sh [ディレクトリ名]

ディレクトリのディクス使用率を表示します。[ディレクトリ名]は省略可能です。
-s:指定したディレクトリの合計のみを表示します。
-h:サイズに応じて人間が読みやすい単位で表示します。

$ du -sh folder
377M	folder

tail -f [ファイル名]

リアルタイムにファイル内容を確認します。

$ tail -f laravel.log 
# 65 /home/webapp/quiz-maker/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
# 66 /home/webapp/quiz-maker/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
# 67 /home/webapp/quiz-maker/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
# 68 /home/webapp/quiz-maker/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
# 69 /home/webapp/quiz-maker/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
# 70 /home/webapp/quiz-maker/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
# 71 /home/webapp/quiz-maker/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
# 72 {main}
"} 

find [検索先ディレクトリ] -name [ファイル名 | ディレクトリ名]

ファイルやディレクトリを再帰的に検索します。

$ find . -name cache
./bootstrap/cache
./storage/framework/cache
./public/phpmyadmin/libraries/cache
./public/phpmyadmin/vendor/psr/cache
./public/phpmyadmin/vendor/symfony/cache
./vendor/doctrine/cache
./vendor/psr/cache
./node_modules/webpack/lib/cache

また、ファイル名やディレクトリ名にワイルドカードを指定できます。
以下は、拡張子が「.php」のファイルを検索しています。

$ find  .  -name  \*.php
./bootstrap/cache/packages.php
./bootstrap/cache/services.php
./bootstrap/app.php

※ワイルドカードは直前に「\」でエスケープしないとエラーが出たり、想定した動作にならないことがあります。

grep [オプション] [検索正規表現] [ファイル名]

ファイルの内容から正規表現を検索します。
[オプション]
-i:大文字と小文字を区別せず検索を行います。
-E:拡張正規表現を使用できるようになります。
-v:一致しないものを検索します。
-n:検索結果に行番号を表示します。
-l:検索結果にファイル名のみ表示します。
-r:ディレクトリ内も再帰的に検索します。

$ grep -i test HomeController.php 
        $sort_item = $request->sort ?? 'latest';

cp -a --parents [ファイル名]

ディレクトリ構造を保ってファイルをコピーします。
-a:サブディレクトリや属性なども含め、可能な限り全てを保持しながらコピーします。
--parents:階層構造を保ったままコピーします。

$ cp -a --parents ./app/Http/Controllers/HomeController.php /home/admin/

zip -r [アーカイブ名] [ディレクトリ名]

ディレクトリをzipファイルに圧縮します。
-r: ファイルとディレクトリを再帰的に処理します。

$ zip -r ./app.zip ./app
  adding: app/ (stored 0%)
  adding: app/Actions/ (stored 0%)
  adding: app/Actions/Fortify/ (stored 0%)
  adding: app/Actions//Fortify/CreateNewUser.php (stored 45%)
  adding: app/Actions//Fortify/PasswordValidationRules.php (stored 54%)
  adding: app/Actions//Fortify/ResetUserPassword.php (stored 57%)
  adding: app/Actions//Fortify/UpdateUserPassword.php (stored 54%)
  adding: app/Actions//Fortify/UpdateUserProfileInformation.php (stored 56%)
  adding: app/Actions/Jetstream/ (stored 0%)
  adding: app/Actions/Jetstream/DeleteUser.php (stored 57%)

unzip [アーカイブ名]

zipファイルを解凍します。

$ unzip ./app.zip
Archive:  app.zip
   creating: app/
   creating: app/Actions/
   creating: app/Actions/Fortify/
  inflating: app/Actions//Fortify/CreateNewUser.php
  inflating: app/Actions//Fortify/PasswordValidationRules.php
  inflating: app/Actions//Fortify/ResetUserPassword.php
  inflating: app/Actions//Fortify/UpdateUserPassword.php
  inflating: app/Actions//Fortify/UpdateUserProfileInformation.php
   creating: app/Actions/Jetstream/
  inflating: app/Actions/Jetstream/DeleteUser.php
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?