LoginSignup
1
1

More than 1 year has passed since last update.

snippet: linux

Last updated at Posted at 2015-07-11

キャッシュメモリ開放

free -m

echo 1 > /proc/sys/vm/drop_caches

free -m

ed25519でSSH鍵を作成

ssh-keygen -t ed25519 -N "passphrase" -C email@example.com
# ほかの例
ssh-keygen -t ed25519 -C user@ed25519

GDMからlightdmに変更 (ubuntu)

sudo dpkg-reconfigure lightdm

パーティションの空き容量の拡張

growpart /dev/sda 1

pdf を画像化

pdftoppm -r 125 -png a.pdf a

dockerコンテナのメモリなど使用率を表示

docker container stats

vfatをmount時に文字化けする場合

mount -o codepage=932,iocharset=utf8 \
 /dev/sda1 /mnt/

wgetにHost指定する

wget \
 --header='Host: def.com' \
 --header='Referer: def.com' \
 'http://192.168.0.2/index.html'

変数を使用する

# $VAR が空の場合、aaaを代入する。
VAR=${VAR:=aaa}

文字の削除削除

# 空行の削除
sed -e '/^$/d'

# "{この文字列}"を削除
sed -e 's/{[^{}]*}//g'

subversion (svnコマンド)

# リビジョン 44351 を checkout
svn co -r r44351 http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8_7/ ruby_1_8_7-p375

# リビジョン 67826 を checkout
svn co -r r67826 http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8_7/ ruby_1_8_7-p375

# リポジトリ情報
svn info http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8_7/

bonding設定 (CentOS6.9)

/etc/modprobe.d/bonding.conf
# mode=1 : active-backup

alias bond0 bonding
options bond0 mode=1

alias bond1 bonding
options bond1 mode=1

アクティブ・アダプタを手動で切り替える

現状確認
cat /proc/net/bonding/bond0
eth2をbond0のアクティブに変更
ifenslave -c bond0 eth1

マルチキャストのインターフェース確認

$ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
bond0           1      226.94.1.1
bond0           1      all-systems.mcast.net

xsetrootで指定できる色一覧

xsetroot -solid gray

このgrayの箇所はどの色が使えるのかと言うと/etc/X11/rgb.txtを参考にすればよい。

Warning: RPMDB altered outside of yum. エラーが出た場合

yum history sync

hexeditor

sudo apt install -y ht
hte

上のメニューはEsc押しながらFキーなど押す。

crontab -e で編集するeditor再選択

$ /usr/bin/select-editor
  1. /bin/ed
  2. /bin/nano
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

cron実行環境に近い状態で実行

bash -l コマンド

duで合計サイズも求める

-cオプションを用いる。
du -hc *.dump

debian用sources.listミラーサイト

/etc/apt/sources.list
deb http://httpredir.debian.org/debian/ stretch main
deb http://security.debian.org/ stretch/updates main
deb http://httpredir.debian.org/debian/ stretch-updates main

csplit

指定した文字列でファイルを分割
csplit backup_20151201.sql '/DROP TABLE IF EXISTS/' {*}

lsで特定のファイルを除外

ls --ignore access_log* --ignore php*log

DMGをISOに変換

sudo apt-get install -y dmg2img
dmg2img hoge.dmg
mv hoge.img hoge.iso

grubからmemtestを外す

sudo chmod -x /etc/grub.d/20_memtest86+
sudo update-grub

文字コード指定してunzip

windowsで作成したzipを解凍
unzip -Ocp932 hoge.zip

thunderbirdが常に起動していてほしい

  • thunderbirdのフィルタ機能に頑張ってもらいたいが、ついついthunderbirdを終了してしまうので
14 * * * *  DISPLAY=:0 thunderbird

優先度最低でコマンド実行

nice -n 19 ionice -c 2 -n 7 コマンド

boot時mount失敗で止まらないようにする

/etc/fstab
- /dev/sda1       /mnt/usb_1      ext4    defaults,noatime         0       0
+ /dev/sda1       /mnt/usb_1      ext4    defaults,noatime,nofail  0       0

SSD用fstab

noatime,discardをつける
- /dev/sda1       /mnt/usb_1      ext4    defaults,noatime,nofail  0       0
+ /dev/sda1       /mnt/usb_1      ext4    defaults,noatime,nofail,discard  0       0

サスペンド

sudo pm-suspend

fizsh

sudo apt-get install -y fizsh
fizsh
  • $HOME/.fizsh/.fizshrc

バイナリから文字を抽出

strings file.txt

Gitlab5のデータバックアップ、リストア

sed

マッチした行を削除
RUN sed -i -e '/to_sym/d' config/environments/production.rb

文字列のシャッフル

$ echo "b79c5636cbc6dd" | fold -w1 | shuf | tr -d '\n'
73c66ccb9b65dd

メモリ型番確認

sudo /usr/sbin/dmidecode --type memory

installコマンドでディレクトリ作成

install --directory --mode=700 -o vagrant /home/vagrant/.ssh
install -d -m=777 hoge/fuga

xargs

  • 参考: xargsメモ - ぱせらんメモ
    • -t : 実行するコマンドを表示
    • -p : 実行前に確認
    • -I(アイ) : 置換文字列。任意の位置に挿入できる。
お題:どこかにあるhoge.jpgを~/にコピーしたい。
cp hoge.jpg ~/
find . | grep jpg  | xargs -tpI % \
  cp % ~/

find + tail

find /var/log -maxdepth 1 -type f -exec tail -n0 -F -q {} \;
  • 深さの -d オプションはなくなった。
  • tail -n0 で既存の行は出力しない
  • tail -q でファイル名を出力しない

ファイルの解凍

  • unrar-freeで解答できなくてもunarなら解凍できたりする。
unar hoge.rar

shellscript : 関数、if、拡張子の取得のサンプル

関数、if、拡張子の取得のサンプル
is_allowed_ext(){
  EXTENSION=$1
  case $EXTENSION in
    "doc" | "docx" | "rtf") echo 0;;
    "pdf") echo 0;;
    *) echo 1;
  esac
}
if [ ! $# -eq 1 ];then
    echo "ファイルを指定してください。"
    exit 1
fi
FILE=$1
if [ ! -e $FILE ];then
    echo "$FILEは存在しません。"
    exit 1
fi

BASENAME=`basename $FILE`
#FILENAME=${BASENAME%.*}
EXTENSION=${BASENAME##*.}
CHECKED_RESULT=`is_allowed_ext $EXTENSION`
if [ ! $CHECKED_RESULT -eq 0 ];then
    echo "その拡張子のファイルは利用できません。"
    exit 1
fi

yumをproxy経由で通信

/etc/yum.conf
+ proxy=http://192.168.11.44:8080

fdisk後の再認識

partprobe

vfatなUSBメモリのボリューム名を変更

64gbというボリューム名に設定
mlabel -i /dev/sdc1 ::64gb

urldecode

urldecodeしutf8で出力
nkf -w --url-input

urlencode

nkf -WwMQ | tr = %

mercurial(hg)

hg init
hg add
hg commit -m "init"
hg status
hg log
hg diff

Xvfb

sudo apt-get install xvfb cutycapt
xvfb-run --server-args="-screen 0, 1024x768x24" \
  cutycapt --out=hoge.png --url="http://yahoo.co.jp/"

Seleniumでメール送信

a.selenium
from selenium import webdriver
from xvfbwrapper import Xvfb

class CapturePages(object):
    def __init__(self, _width, _height):
        fp = webdriver.FirefoxProfile()
        self.xvfb = Xvfb(width=_width, height=_height)
        self.xvfb.start()
        self.browser = webdriver.Firefox(firefox_profile=fp)
    def capPage(self, page, file_name):
        self.browser.get(page)
        self.browser.save_screenshot(file_name)
        self.browser.close()
    def endCapture(self):
        self.xvfb.stop()
        self.browser.quit()

if __name__ == '__main__':
    capture = CapturePages(1280, 800)
    capture.capPage("https://www.moongift.jp", "hoge.png")
    capture.endCapture()

メールにCC

echo "本文" | mail -s "タイトル" -r from@example.com \
  -c cc1@example.com -c cc2@example.com \
  to1@example.com to2@example.com

HTMLメール送信

htmlの生成
URL="http://qiita.com/tukiyo3/items/6f0009f9746e05395e4a"
pandoc -f html -t markdown "$URL" \
 | pandoc -f markdown -t html -o tmp.html
メール送信(tmp.htmlにコンテンツタイプを付けて送信)
(echo 'To: toaddr@myserver.com';
 echo 'Subject: test';
 echo 'Mime-Version: 1.0';
 echo 'Content-Type: text/html';
 echo ) \
 | cat - tmp.html  | sendmail -t

password変更

echo "user1:password" | chpasswd
echo "vagrant:vagrant" | chpasswd

smbclient

$ smbclient --no-pass -L 192.168.1.1
Anonymous login successful
Domain=[WORKGROUP] OS=[Apple Base Station] Server=[CIFS 4.32]

	Sharename       Type      Comment
	---------       ----      -------
	IPC$            IPC
Anonymous login successful
Domain=[WORKGROUP] OS=[Apple Base Station] Server=[CIFS 4.32]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------

ランダムに並び替え

seq 0 9 | shuf
shuf -e {0..9}

# 改行してほしくない場合
shuf -z -e $(seq 0 9)

64bit環境で32bit向けコンパイル

sudo apt install -y \
  lib32ncurses5-dev gcc-multilib g++-multilib

./configure CFLAGS='-m32' CXXFLAGS='-m32'
1
1
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
1
1