Skip to content

Linux Command Line Tools

TonyCai edited this page Sep 17, 2018 · 2 revisions

Table of Contents

curl

[tonycai@CAT]$ curl --head http://www.anjuke.com

HTTP/1.1 302
Server: nginx/0.6.32
Date: Fri, 21 Nov 2008 10:05:03 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
ajk: server=hawk2, version=2008_47_1
Set-Cookie: aQQ_ajkguid=6FE610FB-1F0D-71BF-E3FA-FEC6DB4E76D2; expires=Sat, 21-Nov-2009 10:05:03 GMT; path=/; domain=.anjuke.com
Set-Cookie: ctid=11; expires=Sun, 21-Dec-2008 10:05:03 GMT; path=/; domain=.anjuke.com
Location: http://shanghai.anjuke.com/

cat

split

 Split files into pieces

Examples

  • Split the file jdk_v11.jar into files that are 1.44MB in size and use the prefix "jdk_v11.". for the output files.
 split -b1440k jdk_v11.jar jdk_v11.
  • Split the file myapp.java into files of 100 lines each.
 split -l 100 myapp.java
 http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/rzahz/split.htm

bc

 bc - An arbitrary precision calculator language
[tonycai@CAT anjuke]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+2
3
quit

diff

  • 水平比较
 diff -y thai_provine_en.txt thai_provine_cn.txt

top

       k: %CPU  --  CPU usage
          The  task’s share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.  In a true SMP environment, if ’Irix mode’ is
          Off, top will operate in ’Solaris mode’ where a task’s cpu usage will be divided by the total number of CPUs.  You toggle ’Irix/Solaris’ modes with the ’I’ inter‐
          active command.

       l: TIME  --  CPU Time
          Total CPU time the task has used since it started.  When ’Cumulative mode’ is On, each process is listed with the cpu time that it and its dead children has used.
          You toggle ’Cumulative mode’ with ’S’, which is a command-line option and an interactive command.  See the ’S’  interactive  command  for  additional  information
          regarding this mode.

       m: TIME+  --  CPU Time, hundredths
          The same as ’TIME’, but reflecting more granularity through hundredths of a second.

       n: %MEM  --  Memory usage (RES)
          A task’s currently used share of available physical memory.

usermod -Gwheel evans

adduser

[root@frog2 ~]# sudo adduser tonycai -m
[root@frog2 ~]# usermod -Gwheel tonycai
[root@frog2 ~]# passwd tonycai

#
#less /etc/shadow
#less /etc/group
#less /etc/passwd

find

 find /tmp -atime +7 -type f -exec rm -f {} \; #删除一天以前创建的文件
 find . -atime +1 -type f | xargs rm -f #删除当前目录下一天以前创建的文件
 find -name '*.595' | xargs cat >> propertys.595.all #查找文件并合并文件
 find -name "*.C.html" |awk '{print $1" "$1}'|awk '{system("echo "$0" | sed 's/C\.html$/c/g'")}'|awk '{system("mv "$0)}'
 find . -type f -name "*.php" -exec sed -i 's/count(ProId)/count(*)/ig' {} \;
 find ./ -name "*.php" -exec grep -s ajk_proclicklog {} \; -print
 find -name "*.htm" -exec grep -isnE --color "majk" {} \; -print
 查找乱码文件名 ls -il
 ls -il
 find -inum 5116069
 find -inum 5116069 -exec rm {} \; -print
 find -inum 5116069 -exec mv {} test.txt \; -print
  • on mac os like sed -i
 find . -type f -exec perl -pi -e 's/lmysqlclient_r/lmysqlclient/g' {} \; -print
  • searches from ./
 find . -type f|xargs perl -pi -e 's/\t/    /g'

ignore case

 [tonycai@CAT 129_my_anjuke]$ find -iname "*myanjuke*"
 ./app-anjuke/classes/dao/my/MyAnjuke.php

name regex

find . -regex '.*/mp[0-4].*'

 $ find . -regex './ch0[1-2]_0[1-3].*'
 ./ch01_01.html
 ./ch01_02.html
 ./ch02_01.html
 ./ch02_02.html
 ./ch02_03.html
 $ find . -regex ".*/.*\.[h|c]" -exec grep --color -insE "add_history" {} \; -print
 $ find . -regex ".*/.*\.[php|java|xml]+" -exec grep --color -insE "select" {} \; -print

How to find symlink ?

 find /bin/ -lname "*" -exec ls -l {} \;

Free Software

 Software that isn't Free Software is called proprietary software.
 Free Software will change the world for the better

rsync

 rsync -av --progress --exclude='config.inc.php' --exclude='forumdata' --exclude='crontab' --exclude='web.log' -e ssh [email protected]:/home/www/release/v1/2008_50/anjuke/ /home/www/release/v1/2008_50/anjuke/
 rsync --progress -avre "ssh -p 22" /home/tonycai/dev-resource/* [email protected]:/home/tonycai/dev-resouce/

rsync:example

ssh-keygen -t rsa

 ssh-keygen -t rsa
 chmod 755 ~/.ssh
 cat id_rsa.pub
 romte:~/.ssh/authorized_keys. 
 http://www.chinaunix.net/jh/4/343905.html
  # ssh-keygen -t rsa /etc/ssh/ssh_host_rsa_key
  # ssh-keygen -t dsa /etc/ssh/ssh_host_dsa_key
  # ssh-keygen -t rsa1 /etc/ssh/ssh_host_key

jobs

 [1]+  Running                 nohup chown 500:500 image -R &
 nohup sh test.sh &

mod uid & gid

 id tonycai
 id evans
 vi /etc/passwd
 vi /etc/group
 cd /home/
 chown tonycai:tonycai ./tonycai/ -R
 chown evans:evans ./evans/ -R

nfs

 [evans@frog2 etc]$ cat /etc/exports
 /data/image     10.0.0.0/255.255.255.0(rw,all_squash,anonuid=500,anongid=501)
 sudo /etc/init.d/nfs restart

tput

 tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput,您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域。
 echo 着色,加粗
先隐藏光标,再显示光标
 tput civis; sleep 3; tput cnorm

执行以下示例命令可以将背景颜色更改为黄色,将前景颜色更改为红色:

 tput setb 6 ; sleep 1; tput setf 4

给字体加粗

 tput bold; echo "test" ; tput rmso

反白显示

 bold=`tput smso`;offbold=`tput rmso`; echo "$bold anywhere $offbold" ;

sed

 find . -type f -name "*.php" -exec sed -i 's/count(ProId)/count(*)/ig' {} \; 
 cat x.log | sed -n '/time/,/;$/ p'
 sed -i 's/old-word/new-word/g' *.txt
 sed -i -e '/^sql_c$/d' ./run_communities_update.sql
 echo "15/03/2009:00:00:01" | sed 's/^\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\):\([0-9]\{2\}\)/\3-\2\-\1 \4/g'
$ sed "s/${pattern}/FOOBAR/" file.txt
# or
$ sed "/${pattern}/{do something;}" file.txt
# Note double quotes rather than single quotes, so the shell can expand the variables

$ safe_pattern=$(printf "%s\n" "$pattern" | sed 's/[][\.*^$/]/\\&/g')
# now you can safely do
$ sed "s/${safe_pattern}/FOOBAR/g" file.txt
$ sed -n "/${safe_pattern}/p" file.txt
# these and the following are just examples, of course
 ./find_properties.sh 2 | sed 's/^[0-9]\+/http:\/\/shanghai.anjuke.com\/viewprop-act-rent-id-\0.html/'
  • skip drop table & skip create table
 sed -i -e '/^\-\- Table structure for table/,/^\-\- Dumping data for table/ d' ajk_proclicklog_daily.sql
 sed -i 's/^) ENGINE=MyISAM /) ENGINE=InnoDB /g' ajk_proclicklog_daily.sql
#!/bin/bash
OLD="xyz"
NEW="abc"
DPATH="/home/you/foo/*.txt"
BPATH="/home/you/bakup/foo"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
   sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
  else
   echo "Error: Cannot read $f"
  fi
done
/bin/rm $TFILE
 sed -n '5,8p' myfile	     # print lines 5,6,7 and 8 of myfile
 sed -e '/^$/d' myfile
 sed -n '1,261p' /home/tonycai/workspace/anjuke/include/lucene.class.php > ./app-anjuke/classes/dao/my/Lucene.php
 Perl Code to replace in every file in a directory
 perl -pi -e ’s/ora08/ora05/g’ *
替换掉所有的换行符
 cat tmp.txt | sed -nr ' H;
 $ {
 x;
 s/\n//g;
 p
 }'

替换大小写

 echo "innodb_data_pages_rows" | sed 's/\w/\u&/g'
  • 最小匹配
[root@frog2 nginx]# tail images.access.log.23 | awk '{print $10}' | sed 's/^\(\/[^/]*\/[^/]*\/\).*/\1/g'
/attachments/10/
/attachments_comm/7/
/attachments_comm/f/
/attachments/10/
/broker/icon/
/attachments/10/
/attachments_comm/4/
/attachments/10/
/attachments_comm/6/
/broker/icon/

tr

 tr - translate or delete characters
 cat tmp.txt | tr -d '\n'

awk

 awk '{print $1}'
 awk -F'averages:' '{ print $2 }'
 awk -F \t '{ print $2 }'
 awk '{ s += $1 } END { printf("%.0f\n", s )}'
 awk '/mysql(d)?\.sock/ { print $9 }'
 awk '/^MemTotal/ { printf("%.0f", $2*1024 ) }'
 awk '/^Memory\ size:/ { print $3*1048576 }'
 awk 'BEGIN {n = 0;} {if ($1 == "'"$driver"'") n = $3;} END {print n;}'
 module='ext3'; /sbin/lsmod | awk 'BEGIN {n = "no";} {if ($1 == "'"$module"'") n = "yes";} END {print n;}'
 cat ~/Desktop/show_global_status.txt | grep -viE "\+|variable_name" |sed 's/.*/\L\0/g' |awk '/[0-9]+/ {print $2"="$4}'
 awk 'BEGIN { FS="="; } \
 /^\#/ { print; } \
 !/^\#/ { if (NF == 2) { n = $1; gsub(/[^A-Za-z0-9_]/,"_",n); print n "=\"" $2 "\""; } else { print; } }'
用 awk 做简单的数值计算
 echo | awk '{print 19+7}' ==> 26 
 echo | awk '{print 19-7}' ==> 12
 echo | awk '{print 19*7}' ==> 133
 echo | awk '{print 19/7}' ==> 2.71429
 echo | awk '{print 19**7}' ==> 893871739
 echo | awk '{print 19%7}' ==> 5
 echo | awk '{print atan2(19, 7)}' ==> 1.21781

cut

  • view all crontabs
 cat /etc/passwd | awk -F':' '{system("crontab -l -u "$1)}'
 cat /etc/passwd | cut -d':' -f1 | crontab -l -u $1

sort

 #去掉相同内容的行
 sort -u bchart.txt > bchart2.txt

wget

nohup wget --mirror --convert-links --backup-converted http://www.cplusplus.com/reference/ -o ../weeklog &
 #Website Mirroring With wget 
 wget --mirror –w 2 –p --html-extension –-convert-links --directory-prefix="/home/www/susue_10_3_reps/" "http://download.opensuse.org/update/10.3/"
 wget --referer="" --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" -c "$full_url" -P $DATA_DIR/view/

grep

find . -type f -exec grep -s ajk_proclicklog {} \; -print
find ./ -name "*.php" -exec grep -s ajk_proclicklog {} \; -print 
grep -niE "select(.*)ajk_attachments" *.php

cat nohup.out.mobile | grep -E "^[0-9]" > nohup.out

[tonycai@CAT workspace]$ echo "w123wwZz9" | grep --color -isnE "[a-zA-Z]+"
1:w123wwZz9
[tonycai@CAT workspace]$ echo "w123wwZz9" | grep --color -isnE "[0-9]+"
1:w123wwZz9

tar

 tar --extract --file=mysql_datadir.tar.gz mysql_datadir/relay-bin.00036?
 tar -r --exclude="nginx-0.6.31/src" --exclude="nginx-0.6.31/Makefile" --file=nginx.tar nginx-0.6.31 -v
 sudo tar -r --exclude="search/data" --exclude="search/apps/resin/log/stdout.log" --exclude="search/apps/resin-3.0.25/log/stdout.log" --file=/home/tonycai/search.tar search -v
  • 用tar进行分卷打包
   tar -cvf test.tar test|split -b 1024

  这样 目录下就会生成多个小文件 默认为xaa 到xaz

  如果要解开

  能这样

  cat xa[a-z]|tar xvf -

wc

 wc - print the number of newlines, words, and bytes in files
 [tonycai@dolphin2 bz_files]$ du -sh
 3.1G    .
 [tonycai@dolphin2 bz_files]$ ls -l *.bz2 | wc
    389    3501   33937
 33937=3.1G

whois

 whois anjuke.com

dig

 [tonycai@CAT v2]$ dig -h
 Usage:  dig [@global-server] [domain] [q-type] [q-class] {q-opt}
            {global-d-opt} host [@local-server] {local-d-opt}
            [ host [@local-server] {local-d-opt} [...]]
 Example: dig anjuke.com @3dns.21vianet.com.cn.
 manual page : http://www.ss64.com/bash/dig.html

php

 yum install php-pecl-memcache
 apt-get install php5-memcache

mytop

  1. TermReadKey-2.30
  2. mytop-1.6.tar.gz
  3. vi ~/.mytop
  4. mytop -s 1 -uroot -p123456 -danjuke_db -hdolphin2

ls

 ls -t -r -1 HOSTNAME-bin* | xargs mysqlbinlog | mysql -uUser -pUserPWD
 ls -1 *.php|grep -v --color "config.*php" | xargs /bin/rm -f

lsof

list open files

  • 当前系统中单个进程打开的文件句柄数
 lsof -n|awk '{print $2}'|sort|uniq -c |sort -nr|more
 lsof -n| grep run.sh

netstat

  • 查看并发请求数及其TCP连接状态
 [tonycai@APP01-003 ~]$  netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
 TIME_WAIT 19120
 ESTABLISHED 213
  • 2010-02-20
 [root@APP01-003 etc]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
 TIME_WAIT 8417
 ESTABLISHED 792
  • 打印路由表信息
caixiaohuamatoMacBook-Air:bin caixiaohua$ netstat -r -f inet
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc           10        0     en0
127                localhost          UCS             0        0     lo0
localhost          localhost          UH              1     3787     lo0
169.254            link#4             UCS             0        0     en0
192.168.1          link#4             UCS             9        0     en0
192.168.1.1        f4:ec:38:12:76:d6  UHLWIi         11      324     en0   1199
192.168.1.60       0:e:e3:1:7a:e1     UHLWIi          0        0     en0   1199
192.168.1.102      38:e7:d8:e9:1b:a   UHLWIi          0        0     en0
192.168.1.110      8:10:75:c2:e7:6f   UHLWIi          0      304     en0   1194
192.168.1.111      fc:25:3f:25:1d:5a  UHLWIi          0        0     en0    712
192.168.1.112      8:10:75:8:fb:f6    UHLWIi          2    20252     en0   1187
192.168.1.116      localhost          UHS             0        0     lo0
192.168.1.127      ec:55:f9:a5:bb:46  UHLWIi          0      514     en0   1198
192.168.1.132      b0:48:7a:6:c9:8e   UHLWIi         13    29102     en0   1195
192.168.1.255      ff:ff:ff:ff:ff:ff  UHLWbI          0       20     en0

dstat

  • dstat 是一个用来替换 vmstat, iostat, netstat, nfsstat 和 ifstat 这些命令的工具,是一个全能系统信息统计工具。
 tonycai@titans:~/workspace/ops_repos/dev/web-sphinx$ dstat 5
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw 
  2   0  97   0   0   0|  38k   57k|   0     0 | 462B  616B| 310   891 
  3   0  97   0   0   0|   0    11k|4547B   39k|   0     0 | 353   810 
  1   0  99   0   0   0|   0  4915B|4449B   34k|   0     0 | 342   830 
  2   0  98   0   0   0|   0    43k|4115B   33k|   0     0 | 348   803 
  2   0  97   0   0   0|   0     0 |9573B  126k|   0     0 | 417  1118 
  3   0  96   0   0   0|   0    14k|  16k  170k|   0     0 | 534  2274 
  7   1  92   0   0   0|4915B  115k|  16k   93k|   0     0 | 538  2338 
  3   0  97   0   0   0|   0     0 |9594B   18k|   0     0 | 392  1729 
  2   1  98   0   0   0|   0    48k|4375B   16k|   0     0 | 333   997 
  4   3  94   0   0   0|   0  8192B|8743B 7435B|   0     0 | 421  1731 
  1   1  98   0   0   0|   0    34k|8538B 9511B|   0     0 | 390  1550 
  1   0  99   0   0   0|   0    13k|5544B 6203B|   0     0 | 325  1059 
  2   0  97   0   0   0|   0     0 |  12k   12k|   0     0 | 443  2038 

tail

 sudo tail -f slow_log

less

 sudo less -n100 log-slow-queries.log

ab

 /usr/local/apache/bin/ab -H 'Cache-Control: max-age=0' -X 192.168.1.81:3128 -k -c 300 -t 3000 http://shanghai.anjuke.com/v2/
 /usr/local/apache/bin/ab -H 'Cache-Control: max-age=0' -X 192.168.1.81:3128 -k -c 300 -t 3000 http://shanghai.anjuke.com/v2/
 /usr/local/apache/bin/ab -c 200 -t 60 -k "http://shanghai.fp151.dev.anjuke.com:30080/"

Arithmetic Expansion

 echo $((718818/86400))
 let r=718818/86400 && echo $r

Perl urlencode

 Urlencode:对 \n 不转码
 perl -p -e 's/([^\w\-\.\@])/$1 eq "\n" ? "\n":sprintf("%%%2.2x",ord($1))/eg' keywords.list
 UrlDecode:
 perl -p -e 's/%(..)/pack("c", hex($1))/eg' query.log

perl-convert

#!/bin/sh
`perl -MEncode -pi -e '$_=encode_utf8(decode(gb2312=>$_))' $1`

iconv

iconv -f -t input_file
-f: from encoding
-t: to encoding
比如:
iconv -f utf-8 -t gb2312 wget.output
iconv  -f gb2312 -t utf8 ad_paipai.html  > ad_paipai_utf8.html
用来做UTF-8输出文件的编码转换挺方便的。这样在Linux上也能直接查看UTF-8的输出了。

dmesg

 dmesg - print or control the kernel ring buffer

sysctl

  sysctl -a
  sysctl - configure kernel parameters at runtime

file system

 cat /etc/fstab

lsmod

 lsmod — program to show the status of modules in the Linux Kernel

tune2fs

 tune2fs -l /dev/sda1
 tune2fs - adjust tunable filesystem parameters on ext2/ext3 filesystems

telnet

 telnet 192.168.1.60 3306

hdparm

test disk performance

 /dev/sda2:
  Timing buffered disk reads:  244 MB in  3.01 seconds =  81.04 MB/sec

startup

检查启动项

 cat  /etc/rc.local

Suse Linux

sudo /sbin/chkconfig --list
cd /etc/rc.d/
sudo vi startup_script
sudo chmod +x  startup_script
cat startup_script
 #!/bin/sh
 /usr/local/mysql/bin/mysqld_safe &
 /usr/local/lighttpd/sbin/lighttpd -f /home/www/conf/lighttpd.conf
sudo chkconfig --add startup_script

Iptables

hawk2 ~ # cat /root/firewall.sh

#!/bin/bash
iptables -F
iptables -X
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i eth1 -j ACCEPT #allow LAN
iptables -A FORWARD -i eth1 -j ACCEPT #allow LAN
iptables -A INPUT -p ALL -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT #allow DNS
iptables -A INPUT -p tcp --sport 25 -j ACCEPT 
iptables -A INPUT -p udp --sport 123 -j ACCEPT #NTP
iptables -A INPUT -i eth0 -s 166.228.192.34/29 -j ACCEPT
iptables -A INPUT -i eth0  -s 222.66.14.14 -j ACCEPT 
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT

#iptables -A INPUT -s 222.73.242.124 -j ACCEPT 
#iptables -A INPUT -i tun0 -j ACCEPT 
#iptables -A FORWARD -i tun0 -j ACCEPT 
#iptables -A OUTPUT -o tun0 -j ACCEPT 
#iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
 iptables -L #lists your current rules in iptables.  
 iptables -F #Disabling the firewall
iptables -t nat -A PREROUTING -s 24.203.82.50 -p tcp --dport 3309 -j DNAT --to-destination 192.168.100.15:3306

iptables -t nat -A PREROUTING -s 74.207.253.49 -p tcp --dport 3309 -j DNAT --to-destination 192.168.100.15:3306

iptables -t nat -D PREROUTING 2

iptables -nvL -t nat

iptables-save  > iptables-rules

root@app00-001:/etc/ppp# iptables-restore iptables-rules 
root@app00-001:/etc/ppp# iptables -nvL -t nat

route

[root@db01-001 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
 route add default gw 10.0.0.112
 vi /etc/resolv.conf #nameserver 211.152.55.249
 dig
 ping www.baidu.com
 route  del -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.96
 route add -net 10.0.0.0/24 gw 192.168.1.96

route add

root@CDN02-001:~# route add -net 10.10.0.0/16  gw 10.0.1.104
root@CDN02-001:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.6.171     10.0.1.104      255.255.255.255 UGH   0      0        0 eth1
222.73.66.224   0.0.0.0         255.255.255.224 U     0      0        0 eth0
10.0.0.0        10.0.1.104      255.255.255.0   UG    0      0        0 eth1
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.1.0     10.0.1.104      255.255.255.0   UG    0      0        0 eth1
10.10.0.0       10.0.1.104      255.255.0.0     UG    0      0        0 eth1
0.0.0.0         222.73.66.225   0.0.0.0         UG    100    0        0 eth0
0.0.0.0         10.0.1.104      0.0.0.0         UG    100    0        0 eth1


route add test

 route add -host 14.17.57.217 netmask 0.0.0.0 gw 192.168.100.10
 [root@idb01-002 ~]# route add -host 183.57.48.39 netmask 0.0.0.0 gw 192.168.100.10
 [root@idb01-002 ~]# route add -host 14.17.57.217 netmask 0.0.0.0 gw 192.168.100.10
 [root@idb01-002 ~]# route del -host 183.57.48.39 netmask 0.0.0.0 gw 192.168.100.10
 [root@idb01-002 ~]# route del -host 14.17.57.217 netmask 0.0.0.0 gw 192.168.100.10

网关 IP Forworad

 -A POSTROUTING -s 192.168.100.0/255.255.255.0 -j MASQUERADE
 COMMIT

traceroute

 traceroute www.anjuke.com
 mtr -n my.anjuke.com

rpm

 root@hp-anjuke:~# rpm --rebuilddb
 [root@db01-001 mysql]# rpm -qf libmysqlclient_r.so.10.0.0
 mysqlclient10-3.23.58-4.RHEL4.1
 [root@db01-001 mysql]# rpm -ev --test mysqlclient10-3.23.58-4.RHEL4.1
 [root@db01-001 mysql]# rpm -ev --test mysqlclient10-3.23.58-4.RHEL4.1
 [root@db01-001 mysql]# cd
 [root@db01-001 ~]# rpm -ev mysqlclient10-3.23.58-4.RHEL4.1
 rpm -qa|grep MySQL
 rpm -e --test MySQL-devel-community-5.0.67-0.rhel4
 rpm -e MySQL-devel-community-5.0.67-0.rhel4
 check directory: /usr/lib64/mysql/
 rpm -ev --nodeps python-elementtree
 rpm -ivh python-devel-2.4.3-24.el5.x86_64.rpm

/etc/*-release

show operting system version

 tonycai@BEE:~$ cat /etc/lsb-release 
 DISTRIB_ID=Ubuntu
 DISTRIB_RELEASE=8.04
 DISTRIB_CODENAME=hardy
 DISTRIB_DESCRIPTION="Ubuntu 8.04.1"

rdesktop

 rdesktop -g1024x768 -a32 192.168.1.81

mysqladmin

 mysqladmin -u root -ptonycai123 flush-privileges password "tonycai1234"
 mysqladmin -u root -ptonycai123 -h192.168.1.16 flush-privileges password "tonycai1234"

nc

 netcat - TCP/IP swiss army knife
 echo stats | nc 127.0.0.1 11211

java

 /usr/bin/java -version

tcpdump

 tcpdump -i eth0 -s 0 -l -w - dst port 80 | strings
 tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings
 tcpdump -i eth0 -s 0 -l -w - dst port 8983 | strings

watch

 watch "echo stats | nc 127.0.0.1 11211"

scp

 scp -r ./* [email protected]:tomcat/

time server

 01 23 * * * (/usr/bin/rdate -s -u time-b.nist.gov)

Source

 source "/home/www/conf/nginx.conf"

seq

#!/bin/bash
for i in `seq 1 100`;
do
       echo "create table t$i(a integer primary key) engine=ndb;"
done    

nslookup

 nslookup shanghai.anjuke.com
 域名系统将域名解析为 IP 地址。 使用此工具可以将查询发送到名称服务器(DNS 服务器)。

history

 show listing of recently execute command

last

 show listing of last logged in users

date-server

Feb 3 22:34:23 db01-001 rc.sysinit: -e

date

 date --set="Wed Mar 18 19:42:11 CST 2009"
 for i in `seq 0 10`; do date_string=`date +%Y%m%d --date="$i days ago"`;  echo "$date_string" ; done
 for i in `seq 0 3`; do date_string=`date +%Y%m%d --date="$i days"`;  echo "$date_string" ; done
  • get timestamp
 [tonycai@BAK01-001 ~]$ date +%s -d "2009-11-14 08:00:00"
 1258156800

strace

 strace - trace system calls and signals
 tiger ~ # strace -p 22690 -T
 Process 22690 attached - interrupt to quit
 select(0, NULL, NULL, NULL, {0, 330000}) = 0 (Timeout) <0.324926>
 wait4(-1, 0x7fff40e2d344, WNOHANG|WSTOPPED, NULL) = 0 <0.000014>
 select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout) <0.999874>

tshark

 tshark - Dump and analyze network traffic
 tshark -r tcpdump.out -d tcp.port==3306,mysql -T fields -e mysql.query > query_log.out

tshark install on ubuntu

tonycai@dell-desktop:~$ sudo apt-get install tshark
[sudo] password for tonycai: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libadns1 liblua5.1-0 wireshark-common
Recommended packages:
  libadns1-bin
The following NEW packages will be installed:
  libadns1 liblua5.1-0 tshark wireshark-common
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 10.2MB of archives.
After this operation, 39.3MB of additional disk space will be used.
Do you want to continue [Y/n]? n
Abort.
tshark install on RH5
 yum install wireshark

packages:

 libsmi-0.4.5-2.el5.x86_64.rpm
 wireshark-1.0.8-1.el5_3.1.x86_64.rpm

unrar

 unrar x ./fs4.rar

printf

 printf -v sNextJob "%05g" 1;echo $sNextJob

dd

 dd拷贝磁盘分区
 dd if=/dev/hdb1 of=/dev/hdc1

Archive IMG Server

1、大量图片文件拷贝需求描述

  • Anjuke按照目前的存储方案,当图片在一台服务器上写满时需要启用新的写入存储设备,已满的图片需
  • 要归档到img01-t01的某个分区中(images对应相应的data目录),这就需要通过服务器局域网络拷
  • 贝大量(700G+)树形目录结构的图片文件。
2、拷贝方法分析
  • 初始方案
    • 文件拷贝的方法很多,首先我们排除了scp 远程拷贝和用cp通过ssh拷贝,因为这两种方法是有数据
    • 加密,大量文件不间断拷贝再加上数据的加密和解密会需要更长时间。最初我们尝试了用cp 通过NFS
    • 挂载拷贝,拷贝速度不理想,多线程拷贝也才40M/s,后来尝试rsync,只是计算这700多G就要
    • 17小时时间,tar打包重定向也不理想。
  • 最终方案
    • 将要拷贝的文件分区打包成一个镜像,相当于把大量小文件打包成一个大文件,打包采用dd命令直
    • 接输出磁盘镜像,镜像从一台到另一台的拷贝采用netcat命令通过网络输入到目标主机,测试效率
    • 很高。
  • 另:为了避免终端连接断掉从而导致命令中止,利用虚拟终端来screen来运行输入命令。
3、具体实现
  • 目标主机:
nc -l 7878 -w 10 > /data006/img01-001.data.img &
  • 源主机:
mount -o remount,ro /data  #设置打包目录为只读,否则无法被mount
screen -r
time dd if=/dev/sda9 | nc 192.168.1.88 7878

#设置打包目录为读写
mount -o remount,rw /dev/sda9
  • 目标主机:
mount -o loop /data006/img01-001.data.img /data007

time

  time - time a simple command or give resource usage
tonycai@cat:/home/www/release/v2/dev-2009_20> time df -h
Filesystem            Size  Used Avail Use% Mounted on
LABEL=root            220G   91G  118G  44% /
udev                 1009M  148K 1009M   1% /dev
/dev/sda3             198M   11M  177M   6% /boot
/dev/sr0              4.2G  4.2G     0 100% /media/SLED10SP_001

real    0m0.015s
user    0m0.000s
sys     0m0.004s

spawn

 spawn - Postfix external command spawner
 spawn是expect的算一个内建命令
 必须先安装expect!
 #!/usr/bin/expect  ,title要注意!

fdisk

 # fdisk -l
root@BEE:~# fdisk -l

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xa89e44b9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       18703   150231816   83  Linux
/dev/sda2           18704       19457     6056505    5  Extended
/dev/sda5           18704       19457     6056473+  82  Linux swap / Solaris=

hwinfo

 # hwinfo --disk
cat:/dev/cdrom # hwinfo --disk
20: IDE 00.0: 10600 Disk
  [Created at block.193]
  UDI: /org/freedesktop/Hal/devices/storage_serial_6RYJGVBL
  Unique ID: z70z.EGISQo1prf2
  Parent ID: w7Y8.mn_t_8FoQLC
  SysFS ID: /block/sda
  SysFS BusID: 0:0:0:0
  SysFS Device Link: /devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0
  Hardware Class: disk
  Model: "ST3250310AS"
  Device: "ST3250310AS"
  Revision: "4.AD"
  Serial ID: "6RYJGVBL"
  Driver: "ata_piix", "sd"
  Device File: /dev/sda
  Device Files: /dev/sda, /dev/disk/by-id/scsi-SATA_ST3250310AS_6RYJGVBL, /dev/disk/by-id/ata-ST3250310AS_6RYJGVBL, /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0, /dev/disk/by-id/edd-int13_dev80
  Device Number: block 8:0-8:15
  BIOS id: 0x80
  Geometry (Logical): CHS 30394/255/63
  Size: 488281250 sectors a 512 bytes
  Geometry (BIOS EDD): CHS 484406/16/63
  Size (BIOS EDD): 488281250 sectors
  Geometry (BIOS Legacy): CHS 1023/255/63
  Config Status: cfg=no, avail=yes, need=no, active=unknown
  Attached to: #17 (IDE interface)

raidutil

 raidutil - RAID Management Configuration Utility

dmidecode

 dmidecode能查询到包括bios、system、board、mem等多达39种信息;详细可查man帮助信息!
cat:~ # dmidecode -t 1
# dmidecode 2.7
SMBIOS 2.5 present.

Handle 0x0001, DMI type 1, 27 bytes.
System Information
        Manufacturer: Dell Inc.
        Product Name: Vostro 200
        Version:
        Serial Number: 85G5L2X
        UUID: 44454C4C-3500-1047-8035-B8C04F4C3258
        Wake-up Type: Power Switch
        SKU Number:
        Family:

smartctl

[root@app01-008 ~]# smartctl -a /dev/sda
smartctl version 5.38 [x86_64-redhat-linux-gnu] Copyright (C) 2002-8 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

Device: SEAGATE  ST9300603SS      Version: FS04
Serial number: 3SE0W9XL
Device type: disk
Transport protocol: SAS
Local Time is: Tue Nov 22 14:34:58 2011 CST
Device supports SMART and is Enabled
Temperature Warning Disabled or Not Supported
SMART Health Status: OK

Current Drive Temperature:     26 C
Drive Trip Temperature:        68 C
Elements in grown defect list: 0
Vendor (Seagate) cache information
  Blocks sent to initiator = 71533558
  Blocks received from initiator = 1847412712
  Blocks read from cache and sent to initiator = 1283418608
  Number of read and write commands whose size <= segment size = 280568894
  Number of read and write commands whose size > segment size = 0
Vendor (Seagate/Hitachi) factory information
  number of hours powered up = 17468.90
  number of minutes until next internal SMART test = 46

Error counter log:
           Errors Corrected by           Total   Correction     Gigabytes    Total
               ECC          rereads/    errors   algorithm      processed    uncorrected
           fast | delayed   rewrites  corrected  invocations   [10^9 bytes]  errors
read:   97931222        0         0  97931222   97931222       6630.695           0
write:         0        0         0         0          0       7610.302           0
verify:    19477        0         0     19477      19477          3.000           0

Non-medium error count:      147

SMART Self-test log
Num  Test              Status                 segment  LifeTime  LBA_first_err [SK ASC ASQ]
     Description                              number   (hours)
# 1  Background long   Completed                  16       1                 - [-   -    -]
# 2  Background short  Completed                  16       0                 - [-   -    -]

Long (extended) Self Test duration: 3180 seconds [53.0 minutes]

MegaCli

  • 疑似磁盘坏道信息 (MegaCli -FwTermLog -Dsply -aALL | grep -i --color "Error")
[root@db10-023 ~]# MegaCli -FwTermLog -Dsply -aALL 
10/20/11 11:10:38: DEV_REC:Medium Error DevId[2] devHandle f RDM=8077da00 retires=0
10/20/11 11:10:38: MedErr is for: cmdId=68, ld=0, src=1, cmd=1, lba=1d6cbc94, cnt=20, rmwOp=0
10/20/11 11:10:38: ErrLBAOffset (19) LBA(75b2f14) BadLba=75b2f2d
10/20/11 11:10:50: DEV_REC:Medium Error DevId[2] devHandle f RDM=80597e00 retires=0
10/20/11 11:10:50: MedErr is for: cmdId=16, ld=0, src=1, cmd=1, lba=326ab0e4, cnt=20, rmwOp=0
10/20/11 11:10:50: ErrLBAOffset (17) LBA(c9aac64) BadLba=c9aac7b

[root@db10-023 ~]# MegaCli -FwTermLog -Dsply -aALL | grep -i --color "Error"
11/22/11 13:46:50: EVT#04384-11/22/11 13:46:50: 110=Corrected medium error during recovery on PD 00(e0x20/s0) at 77d0bb7
11/22/11 15:04:04: DEV_REC:Medium Error DevId[0] devHandle 11 RDM=8081f000 retires=0
11/22/11 15:21:19: DEV_REC:Medium Error DevId[0] devHandle 11 RDM=80812400 retires=0
11/22/11 15:48:01: DEV_REC:Medium Error DevId[0] devHandle 11 RDM=8071f600 retires=0

References

References

  • Linux命令工具 http://cb.vu/unixtoolbox.xhtml