linux commands

Commands which is basically called on terminal.

at

1分後に処理を実行

echo 'foo.sh -t bar -e' | at now + 1minute

autossh

Install.

sudo apt get install autossh

Launch on /etc/rc.local

sudo -u pi autossh -f -N -R 10022:localhost:22 -p 123 user@example.net

certbot

certbot delete --cert-name example.net

cron

毎日0時10分に処理を実行

PATH=/usr/local/bin:/bin:/usr/bin
10 0 * * * foo.sh 2>&1 | logger -p cron.info -t "foo"

curl

レスポンスをファイルに保存します。

curl -O http://sample.com/foo.tgz

プログレスバーを抑制(-s)、ただしエラーは出力します(-S)。

curl -s -S http://sample.com/api?foo=bar

dd

dd if=/dev/zero of=/tmp/testimg bs=1024 count=1024

find

Find invalid symbolic links.

find /opt/kanesue -xtype l

See http://qiita.com/kawaz/items/ba0e75b6828bd3951e52

git

git diff
git diff HEAD
git diff --staged
git diff -w  # Ignore white spaces.

To remove merged branches.

git branch --merged | egrep -v "(^\*|master|main|develop)" | xargs git branch -d

How to rename master to main.

git branch -m master main
git push -u origin main

For github: Settings -> Branches -> Rename default branch For bitbuchet: Repository Settings -> Repository Details -> Advanced -> Main branch

See also How to Rename the master branch to main in Git.

How to change default editor to nano.

git config --global core.editor "nano"

git remote

# 一覧
git branch -r
# 作成
git push -u origin branch_name
# 作業開始
git checkout --track origin/branch_name
# 削除
git branch -r -d origin/branch_name
git push origin :branch_name

others

CSVの差分を見る git diff --color-words="[^[:space:],]+" x.csv y.csv

rsync

特定の拡張子だけ対象にする

rsync -avz --include "*/" --include "*.csv" --exclude "*" foo/ host:foo/

ファイルのチェックサムだけを比較する(タイムスタンプを見ない)

rsync -crlpgoDv foo/ bar/

特定のディレクトリ(pub,sys)だけ転送する(bash)

rsync -avz ./{pub,sys} host:foo/

特定のディレクトリ(log,tmp)を除外して転送する

rsync -crlpgoDvn --exclude 'log' --exclude 'tmp' foo/ bar/

sed

Remove lines which contain specific string from multiple files.

find . -name "*.html" | xargs sed -i '/foobar/d'

Replace text in the file.

sed -i 's/foo/bar/' sample.txt

shutdown

Shutdown on the time.

sudo shutdown -r 23:00

swapon

Create swap memory on the host.

dd if=/dev/zero of=/mnt/1gb.swap count=1024 bs=1024K
mkswap /mnt/1gb.swap
chmod 600 /mnt/1gb.swap
swapon /mnt/1gb.swap

If you'd like to make different size, just adjust count parameter. i.e. count=4096 for 4G swap.

If it's needed to be permanent, add the following to /etc/fstab.

/mnt/1gb.swap none swap sw 0 0

findmnt

How to verify /etc/fstab without rebooting.

sudo findmnt --verify --verbose

https://serverfault.com/questions/174181/how-do-you-validate-fstab-without-rebooting

aplay

Play sound.

aplay foo.wav

For mp3 format, ffplay is available.

ffplay -v 0 -nodisp -autoexit foo.mp3

ab

the tool to benchmark web server responses. The example tries to acess 100 times with 10 concurrency.

ab -n 100 -c 10 https://lumber-mill.co.jp

lsof

Lists the processes which use the TCP port.

lsof -i:80

pgrep

Lists only the process numbers.

pgrep apache2

https://serverfault.com/questions/877783/apache-wont-stop-when-using-service-apache2-stop