Skip to content

Instantly share code, notes, and snippets.

@tamata78
Last active April 5, 2021 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamata78/2191d85e508d1b335100b3608c21ea67 to your computer and use it in GitHub Desktop.
Save tamata78/2191d85e508d1b335100b3608c21ea67 to your computer and use it in GitHub Desktop.
linux_cmd_tips

1.ファイル・ディレクトリ

1.1 ファイル検索

ファイル名を名称指定で検索

$ find ./*/*file_name* -type f | wc

ディレクトリ名を名称指定で検索

$ find dir_name -type d

指定日時以降に作成されたファイルを検索

$ find . -newermt '2018-10-09 15:00:00'

実行日の年月日ディレクトリ以下の1分前以降に作成されたファイルを検索

$ find /var/log/`date '+%Y-%m-%d'` -newermt "`date '+%Y-%m-%d %H:%M:%S' -d '1 minute ago'`"

1.2 ファイル、ディレクトリ作成

連番複数ディレクトリ作成

$ touch aaa{1,2,3}
$ aaa1 aaa2  aaa3

複数ディレクトリ直下に同一複数ファイルを作成

$ mkdir {aa,bb}f
$ touch {aa,bb}/test{1,2}
$ ls -l {aa,bb}
aa:
total 0
-rw-rw-r-- 1 app app 0 Oct 10 12:25 test1
-rw-rw-r-- 1 app app 0 Oct 10 12:25 test2

bb:
total 0
-rw-rw-r-- 1 app app 0 Oct 10 12:25 test1
-rw-rw-r-- 1 app app 0 Oct 10 12:25 test2

1.3 ファイル圧縮・解凍

zip

圧縮

$ zip archive_name file_name
$ zip archive_name -r directory

解凍

$ unzip file_name
$ unzip directory

gz

圧縮

$ gzip file_name
$ gzip -r directory

解凍

$ gzip -d file_name
$ gunzip file_name
$ gunzip -c file_name # 標準出力。容量大きいファイルでファイル出力したくない場合

tar

圧縮

$ tar -cvf xxxx.tar directory

解凍

# 1ファイル
$ tar -xvf xxxx.tar

# 複数ファイル
for tar_file in `ls *.tgz` ; do
    tar zxvf ${tar_file}
    rm -f ${tar_file}
done

1.4 ファイルの移動 rsync ユーザーとグループを変更しつつコピーする

# 凡例
rsync -arv --usermap=変更元ユーザー:変更先ユーザー --groupmap=変更元グループ:変更先グループ 変更元ディレクトリ/* 変更先
ディレクトリ

# 実行例
rsync -arv --usermap=ckenko25:root --groupmap=ckenko25:sambashare  /media/usb0/確定申告/* /media/usb0/samba/tax

2.出力結果加工

2.1 一括実行 xargs

複数のファイルに対してwcコマンドを個別に実行する方法

$ ls *キーワード* | xargs wc

xargs iオプションは引数設定位置を{}と指定可能とする

$ find . -name "*.log" | xargs -i cp {} /tmp/.

/var以下のファイル、ディレクトリ以下のファイルで指定文字列を全置換する

$ grep -rl 'hogehoge' /var | xargs perl -i -pe "s/hogehoge/fugafuga/g" 

2.2 ランダム1000件サンプリング

$ cat output.txt | shuf | head -n 1000

3.ユーザー

3.1 ユーザー一覧

$cat /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

3.2 ユーザー追加

sudo su -
useradd -m [user]
passwd [user]

4.サーバー状態確認

4.1 HD容量

ファイルシステム毎の容量

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        488M   56K  488M   1% /dev
tmpfs           497M     0  497M   0% /dev/shm
/dev/xvda1       30G   19G   12G  62% /

サイズが大きいファイル(上位200件)

# アクセスできないファイルが多い場合は、rootで実行
$ du -b / | sort -nr | head -200

4.2 メモリ確認

メモリ確認のみ

$ free

OSリソース全体

$ top
$ ps aux

CPU, メモリ確認

$ vstat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment