内容へ移動
ClownWiki
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
トレース:
centos:commands
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== コマンドのメモ書き ====== ===== rpm ===== <code> # rpm -ivh ~.rpm </code> * -i : インストール。 * -U : アップグレード。インストール済みパッケージはアップグレードする。未インストールは、新たにそのパッケージをインストールする。 * -F : アップグレード。インストール済みパッケージはアップグレードする。未インストールは何もしない。 * -v : 操作対象のパッケージ名を表示する。 * -h : 処理の進行状況を「#」記号で表示する。 ===== CentOSのバージョン確認 ===== <code> # cat /etc/redhat-release CentOS release 5.5 (Final) </code> ===== Grep ===== * -i 大文字を小文字を区別しません。 * -n 出力行頭に入力ファイルでの行番号を表示します。 * -v 検索に引っかからなかった行を出力します。 * -R -r ディレクトリ配下のファイルとサブディレクトリも対象にします。 <code> # grep -i exist /var/log/httpd/error_log </code> <code> # grep -r exist /var/log/httpd/ </code> * .svnファイルを除外 <code> # grep -r exist /var/log/httpd/ | grep -v ".svn" </code> ===== sed ===== ファイル内の文字列置換 * ファイル上書き <code> $ sed -i s/[置換対象]/[置換後文字列]/g [ファイル名] </code> * 別ファイルに出力 <code> $ sed -e s/[置換対象]/[置換後文字列]/g [入力ファイル名] > [出力ファイル名] </code> ===== wget ===== ファイルをダウンロードする <code> # wget http://download.url/download.file </code> デフォルトでは、コマンドを実行した場所にダウンロードしたファイルが保存される。 保存先ファイル名を指定して実行 <code> wget -O "/etc/yum.repos.d/glusterfs-epel.repo" "http://download.gluster.org/pub/gluster/glusterfs/3.4/LATEST/CentOS/glusterfs-epel.repo" </code> 保存先を指定して実行(ファイル名はオリジナル) <code> wget "http://download.gluster.org/pub/gluster/glusterfs/3.4/LATEST/CentOS/glusterfs-epel.repo" -P "/etc/yum.repos.d/" </code> ===== 圧縮ファイルを解凍せずに開く ===== <code> # gzip -dc "圧縮ファイル.qz" | less </code> <code> # bzip2 -dc "圧縮ファイル.qz" | less </code> ===== dig ===== IPアドレス<->名前解決を確認する(ドメイン名を問い合わせる) * インストールする <code> # yum -y install bind-utils </code> * 問い合わせる <code> # dig @127.0.0.1 server.example.com </code> ===== ランダムパスワード生成 ===== <code> # yum install expect # mkpasswd 6ln8,seXW # mkpasswd -s 1 -C 2 -d 2 -l 8 q^jPT56v </code> * s:記号 * C:大文字 * d:数字 * l:文字数 ===== ntp 手動時刻合わせ ===== <code> # /usr/sbin/ntpdate -b ntp.nict.jp </code> ===== 指定時間にコマンド実行 ===== <code> # yum install at # /etc/init.d/atd start # at 17:00 at> echo "hello world" at> (ctrl+d押下)<EOT> job 7 at 2013-06-19 17:00 # atq 7 2013-06-19 17:00 a root # at -c 7 ~省略~ echo "hello world" # atq 7 2013-06-19 17:00 a usu 8 2013-06-19 19:00 a usu # atrm 8 # atq 7 2013-06-19 17:00 a usu </code> ===== 削除できないファイルを削除 ===== 何かの拍子(操作ミス)でできたゴミファイル「#{node.nginx.app_name}」 通常の操作では削除できないので、inodeを指定して削除する <code> # ll 合計 20 lrwxrwxrwx 1 root root 43 8月 7 19:07 2013 #{node.nginx.app_name} -> /mnt/addVol/releases/#{node.nginx.app_name} drwxr-xr-x 2 root root 4096 5月 14 07:12 2013 cgi-bin drwxr-xr-x 3 root root 4096 8月 7 12:57 2013 error drwxr-xr-x 2 root root 4096 5月 14 07:12 2013 html drwxr-xr-x 3 root root 4096 8月 7 12:57 2013 icons </code> inodeを調べる <code> # ls -i 264338 #{node.nginx.app_name} 263824 cgi-bin 263825 error 263850 html 263851 icons </code> inodeを指定して削除する <code> # find . -inum 264338 -exec rm -f {} \; # ll 合計 20 drwxr-xr-x 2 root root 4096 5月 14 07:12 2013 cgi-bin drwxr-xr-x 3 root root 4096 8月 7 12:57 2013 error drwxr-xr-x 2 root root 4096 5月 14 07:12 2013 html drwxr-xr-x 3 root root 4096 8月 7 12:57 2013 icons </code> ===== 改行コードを確認する ===== <code> # cat -v テキストファイル名 </code> 行末に「^M」が表示されていれば、改行コード「CRLF(windows形式)」 ===== ログ圧縮等でワイルドカードはループで処理 ===== <code> #!/bin/sh # パラメータ TMP="/mnt/addVol/tmp/" COMPRESS_DAY=`date -d '15 days ago' '+%Y%m%d'` DELETE_DAY=`date -d '91 days ago' '+%Y%m%d'` #echo $TMP echo "===============" echo "COMP:"$COMPRESS_DAY echo "DEL:"$DELETE_DAY echo "===============" ############################################################################### # 圧縮 for txt in $TMP"*_"$COMPRESS_DAY".txt" ; do bzip2 ${txt} done # 削除 for txt in $TMP"*_"$DELETE_DAY".txt.bz2" ; do rm -f ${txt} done </code> ===== ワンライナーでループ ===== 元ネタ <code> for txt in /var/log/httpd/*2013* ; do bzip2 ${txt} done </code> ワンライナーで書き換え(行末の区切りを;でつなげる) <code> for txt in /var/log/httpd/*2013* ; do bzip2 ${txt} ; done ; </code> ===== ワンライナーでIPアドレス取得 ===== 通常 <code> # ip addr show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:50:56:b6:2e:5c brd ff:ff:ff:ff:ff:ff inet 192.168.10.2/24 brd 192.168.10.255 scope global eth1 inet 192.168.10.4/32 scope global eth1 </code> ワンライナー <code> # x="" ; for IP in `ip addr show eth1 | grep "^ *inet" | sed 's/^ *inet //g' | sed 's/\/.*//g'` ; do x="$x"" ""$IP" ; done ; echo $x ; 192.168.10.2 192.168.10.4 </code> ===== プロセス数取得 ===== e.g. apache httpdの場合 <code> /bin/ps -ef|grep -v grep|grep httpd |wc -l </code> ===== 不正終了などでlockファイルが残った時の対処 ===== <code> # /etc/init.d/mysqld status mysqld dead but subsys locked mysqld は停止していますがサブシテムがロックされています # ll /var/lock/subsys/ ~ -rw-r--r-- 1 root root 0 Sep 6 02:24 mysqld ~ # rm -rf /var/lock/subsys/mysqld </code> ===== 指定階層以下のディレクトリに実行権限をつける ===== <code> # find /path/dir -type d -print | xargs chmod +x </code> ===== apache(シェルのないユーザ)でログイン ===== <code> # su - apache --shell=/bin/bash </code> ===== apacheでsudo ===== ==== ユーザー「apache」でsudoしようとすると、以下のエラーが発生。 ==== <code> sudo: sorry, you must have a tty to run sudo </code> 設定ファイルを書き換えることでsudo可能 <code> # visudo -) Defaults requiretty +) #Defaults requiretty </code> ==== sudoでパスワードを不要にする ==== <code> sudo: no tty present and no askpass program specified </code> <code> # sudo visudo 実行したいコマンドをカンマ区切りで記述 +)apache ALL=(ALL) NOPASSWD: /bin/ls, /bin/chmod または、アリアリ +) apache ALL=(ALL) NOPASSWD:ALL </code> ===== プロセスの現在のメモリ使用量を確認 ===== psコマンドにcオプションをつけて、VSZでソート。 VSZ、RSSからメモリ使用量を確認。 <code> # ps auxc --sort -vsz USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 17860 3.5 6.8 3063908 267420 ? Sl 11:35 1:51 mysqld nobody 24738 1.9 0.2 203136 11240 ? Ssl 2013 898:00 gmond root 1024 0.0 0.1 197168 4772 ? S 2013 8:41 snmpd root 2019 0.0 0.0 117208 1268 ? Ss 2013 0:36 crond </code> ===== swapの掃除 ===== <code> # swapoff -a && swapon -a </code> ===== 正規表現:ある文字列を含まない ===== <code> ^(?!.*ある文字列).+$ </code> ===== inodeサイズを確認する ===== 通常、ext2/ext3では128byteで、ext4は256byteで管理されます。 <code> # debugfs -R stats /dev/mapper/VGroup00-main_lv00 | grep size debugfs 1.41.12 (17-May-2010) Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize Block size: 4096 Fragment size: 4096 Flex block group size: 16 Inode size: 256 Required extra isize: 28 Desired extra isize: 28 </code> 作成時に-Iオプションを指定して任意のサイズに変更することが可能です。 <code> mkfs -t ext4 -I 512 /dev/VGroup00/main_lv00 </code> ===== pgrep、pkillでプロセスを殺す ===== <code> # pgrep -f http 27057 27059 27061 </code> <code> # pgrep -fl http 27057 /usr/sbin/httpd 27059 /usr/bin/logger -t httpd -p local6.info 27061 /usr/sbin/httpd </code> <code> # pkill -f プロセス名 </code> ===== cpコマンドで確認なしに上書き ===== ファイル単位 <code> # cp -f source target </code> ディレクトリ単位 <code> # cp -rf source target </code> ところが、OSによってはディレクトリ単位でコピーしようとすると、1ファイルずつ確認してくる。 これは、エイリアスで「-i(プロンプトを表示する)」オプションが有効になっているため。 <code> # alias alias cp='cp -i' </code> コマンドの頭に 「\」を付けて一時的にエイリアスを無効化して実行する。 <code> # \cp -rf source target </code> ===== findで検索したファイルを削除する ===== <code> # find ./ -name '*.log' | xargs rm </code> ===== 検索結果のファイルの文字列一括置換 ===== <code> grep -rl '置換前文字列' 対象パス | xargs sed -i 's/置換前文字列/置換後文字列/g' </code> ===== ファイルのタイムスタンプを変更する ===== ^ オプション ^ 説明 ^ | -a | 最終アクセス時刻 | | -m | 最終更新日時 | | -t | 日時 [[CC]YY]MMDDhhmm[.ss] | | CC | 年(上2桁、省略可能) | | YY | 年(下2桁、省略可能) | | MM | 月(01-12) | | DD | 日(01-31) | | hh | 時(00-23) | | mm | 分(00-59) | | .ss | 秒(00-59、省略可能) | touchコマンドで現時刻のファイルを作成する。 (ls -lは最終更新日時、ls -luは最終アクセス日時を表示) <code> # touch file.txt # ls -l file.txt -rw-r--r-- 1 root root 0 4月 18 13:20 file.txt # ls -lu file.txt -rw-r--r-- 1 root root 0 4月 18 13:20 file.txt </code> 最終アクセス日時2014年4月1日1時1分に変更 <code> # touch -at 201404010101 file.txt # ls -l file.txt -rw-r--r-- 1 root root 0 4月 18 13:20 file.txt # ls -lu file.txt -rw-r--r-- 1 root root 0 4月 1 01:01 file.txt </code> 最終更新日時2014年4月1日4時1分に変更 <code> # touch -mt 201404010401 file.txt # ls -l file.txt -rw-r--r-- 1 root root 0 4月 1 04:01 file.txt # ls -lu file.txt -rw-r--r-- 1 root root 0 4月 1 01:01 file.txt </code> ===== 日時のファイル名を付ける ===== <code> # touch $(date +%Y%m%d_%H%M%S).txt # ll 合計 1 -rw-r--r-- 1 root root 0 6月 27 21:58 2014 20140627_215822.txt </code> ===== vimエディタで制御文字を入力 ===== <code> Ctrl+V 制御文字 </code> e.g. エスケープ文字を入力 <code> Ctrl+V Ctrl+[ </code> ===== シェルスクリプトでECHOに装飾 ===== <code> echo -e "\e[33m=== TEST ===\e[m" </code> 制御文字のESC(16進数で1b)を挿入できる場合には、「\e」「\033」は不要。 vimで入力する場合、「Ctrl+V ⇒ Ctrl+[ ⇒ [33m」となる。 <code> echo "ESC[33m=== TEST ===ESC[m" </code> カラーコード 2桁の数字の1文字目「3」は文字色を、「4」は背景色を指定。 ^0^1^2^3^4^5^6^7^ |Black|Red|Green|Yellow|Blue|Magenta|Cyan|White| 属性 ^属性番号^attributes^属性^ |1|bold|太字| |2|low intensity|弱強調| |4|underline|下線| |5|blink|点滅| |7|reverse video|反転| |8|invisible text|非表示| e.g. 黄色背景に黒文字強調 <code> echo -e "\e[43;30;1m=== flash ===\e[m" </code> ===== awkで動的な変数を正規表現にセットする ===== 変数を正規表現の検索文字列にしたい場合は、シングルクォート、ダブルクォートで囲む。 awk '$1 ~ /'"$検索文字列変数"'/' ファイル名 <code> $VAL="taro" awk '$1 ~ /'"$VAL"'/' log.txt </code> ===== awkでソートせずに重複行を削除する ===== <code> awk '!a[$0]++' FILE // 行全体で重複を確認、結果は行全体を返す awk '!a[$1]++' FILE // 1項目目で重複を確認して、結果は行全体を返す awk '!a[$6]++{print $6}' FILE // 6項目目で重複を確認して、結果は6項目目を返す </code> 以下の方法もあるが、sortでかなりのコストが必要なので、上の方が効率が良い。 <code> awk '{print $6}' FILE | sort | uniq </code> ===== ファイルを空にする ===== ログファイルなど、ファイルの中身を空にしたいときのコマンド <code> # > file </code> または <code> # : > file </code> ===== findである時期以前を一括圧縮 ===== あるディレクトリについて、その階層のみで、30日以前のファイルを個別にbzipに圧縮する。 <code> # find ./ -maxdepth 1 -mtime +30 -exec bzip2 {} \; </code> ===== cURL ===== ==== ベーシック認証など ==== <code> curl --anyauth --user user:password http://www.example.com/ </code> ==== httpsのエラー無視 ==== <code> curl --anyauth --user user:password http://www.example.com/ </code> ==== ホストヘッダ偽装 ==== <code> curl -H Host:www.example.co.jp http://www.example.com/ </code> ===== tar.bz2の圧縮・解凍 ===== ==== 圧縮 ==== <code> tar cf - /PATH/TO/BACKUP_DIR | bzip2 > /PATH/TO/BACKUP_DIR.tar.bz2 </code> ==== 解凍 ==== <code> time tar jxvf /PATH/TO/BACKUP_DIR.tar.bz2 </code>
centos/commands.txt
· 最終更新: 2025/02/16 13:53 by
127.0.0.1
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ