Linux-shell小命令整理集合

mpstat用于报告多路CPU主机的每颗CPU活动情况,以及整个主机的CPU情况。
下边的命令可以隔2秒报告一次处理器的活动情况,执行3次
[ root@localhost  ~]# mpstat 2 3
 
 
如下命令每隔1秒显示一次多路CPU主机的处理器活动情况,执行3次
[ root@localhost  ~]# mpstat -P ALL 1 3
 
 
————————————————————————————————————————————————————————————————————————
 
在当前目录下列出所有的扩展名
[ root@localhost  ~]# find . -type f | awk -F'.' '{print $NF}' | sort| uniq -c | sort -g
——————————————————————————————————————————————————————
批量替换php中的关键字    for I in `find . -name "*.php"`; do sed -i "s/old name/new name/g" $I; done 
 
—————————————————————————————————————————————————
快速的将当前目录中所有的.php扩展名修改给.html    [ root@localhost  ~]# for i in *.php; do mv $i ${i%.php}.html; done
 
_____________________________________________________________________________________________________________________
用备份的superblock来挂载文件系统   [ root@localhost  ~]# mount -o sb=98340 /dev/sda1 /mnt/data1/
————————————————————————————————————————————————
 
删除B目录中与A目录同名的文件    [ root@localhost  ~]# for file in A/*; do rm B/`basename $file`; done
彻底删除B目录中与A目录同名的文件: [ root@localhost  ~]# for file in A/*; do rm -rf B/`basename $file`; done
—————————————————————————————————————————————————
 
找出10个大文件  [ root@localhost  ~]#  du -sh * | sort -rh | head 
—————————————————————————————————————————————————
寻找非html的文件   [ root@localhost  ~]# find . -type f ! -name "*html"
————————————————————————————————————————————
用脚本获取eth0上的IPv4地址 [ root@localhost  ~]# ip addr show eth0 |grep 'inet\b' |awk '{print $2}' |sed -r -e 's/\/.*?//g'
—————————————————————————————————————————————————
 
以html的形式获取服务器的硬件配置报告  [ root@localhost  ~]# lshw -html >hardware.html
—————————————————————————————————————————————————
 
统计服务器上已经建立的TPC的链接 
[ root@localhost  ~]# netstat -an | awk '$1 ~ /[Tt][Cc][Pp]/ && $NF ~ /ESTABLISHED/{i++}END{print "Connected:\t", i}'
 
——————————————————————————————————————————————————————————
限制传输速率 [ root@localhost  ~]#   cat /dev/urandom | pv -L 3m | dd bs=1M count=100 iflag=fullblock > /dev/null 
 
————————————————————————————————————————————————————
上一条命令的快捷方式    [ root@localhost  ~]# alias foo="!!"
————————————————————————————————————————————————————————
备份制定后缀的文件并且打包到根目录下   [ root@localhost  opt]# tar --exclude=".??*" -zcvf ./home_backup_2008.tar.gz my_home
 
——————————————————————————————————————————————————————————
替换:将hh文件下的内容192替换成193    [ root@localhost  opt]# for I in "hh"; do sed -i "s/192/193/g" $I; done
 
——————————————————————————————————————————————————————————
 
Linux 限制传输速率    cat /dev/urandom | pv -L 3m | dd bs=1M count=100 iflag=fullblock > /dev/null
 
——————————————————————————————————————————————————————
格式化另一个日期  date --date=yesterday +%Y%m%d  
 
————————————————————————————————————————————————————————
合并多个文件到一个文件中  cat file1 . . . fileN > NEWFile
————————————————————————————————————————————————————————
递归删除所有的htm   [ root@localhost  opt]# find . -type f -name '*.htm' -delete
 
————————————————————————————————————————————————————————
列出并删除一岁的文件   [ root@localhost  zhiwen]# find <目录路径> -mtime +365 -and -not -type d -delete
————————————————————————————————————————————————————————
 
查找日志中的所有问题    [ root@localhost  opt]# grep -2 -iIr "err\|warn\|fail\|crit" /var/log/*
 
————————————————————————————————————————————————————————
小游戏《星球大战》  [ root@localhost  ~]# telnet towel.blinkenlights.nl
————————————————————————————————————————————————————————
Linux和远程机器同步时间  [ root@localhost  ~]# date --set="$(ssh  user@server  date)"
 
————————————————————————————————————————————————————————
查看用户提交的数量   [ root@localhost  ~]# svn log 2>&1 | egrep '^r[0-9]+' | cut -d "|" -f2 | sort | uniq -c
————————————————————————————————————————————————————————
复制时直接创建目录     [ root@localhost  opt]# cp -r zhiwen $(mkdir -p new; echo new)
 
————————————————————————————————————————————————————————
移动时直接创建目录     [ root@localhost  opt]# mv 文件 $(mkdir -p 新目录; echo 新目录)
————————————————————————————————————————————————————————
查看端口是否被占用     [ root@localhost  opt]# netstat -a --numeric-ports |grep 80
——————————————————————————————————————————————————
改变时区:  [ root@localhost  ~]# sudo cp /usr/share/zoneinfo/Europe/Paris /etc/localtime
————————————————————————————————————————————————————
显示BASH版本    [ root@localhost  opt]# echo $BASH_VERSION
 
————————————————————————————————————————————————————
查找大文件   [ root@localhost  opt]# ls -s | sort -nr |more
————————————————————————————————————————————————————
生成md5         [ root@localhost  opt]# echo -n "string" | md5sum -
————————————————————————————————————————————————————
打印第五行      [ root@localhost  opt]# sed -n 5p file.txt
————————————————————————————————————————————————————
远程tail另一台文件     ssh  remoteUser@remoteHost  "tail -f /var/log/scs/remoteLogName" | tee localLogName
如: [ root@localhost  ~]# ssh 192.168.83.229 "tail -f /opt/file.txt" | tee 192.168.83.225
[ root@localhost  ~]# ssh 192.168.83.229 "tail -f /opt/file.txt"
 
————————————————————————————————————————————————————
查询服务器机器型号:   [ root@localhost  ~]# dmidecode -s system-product-name
————————————————————————————————————————————————————
查看默认网关   [ root@localhost  ~]# ip route | grep default | awk '{print $3}'
————————————————————————————————————————————————————————
寻找最大的10个文件夹 
[ root@localhost  ~]# find . -type d -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {} | sort -rn
 
______________________________________________________________________________________________________________________________________________
检查端口是否开放    [ root@localhost  ~]# nmap -p 80 hostname
_________________________________________________________________________________________________________________________________________________
显示命令的输出值到file.txt文件中      [ root@localhost  opt]# command | sed '/file.txt/q'
 
——————————————————————————————————————————————————————————————————
保存man页面为pdf文件     [ root@localhost  opt]# man -t awk | ps2pdf - awk.pdf
——————————————————————————————————————————————————————————————————————
列出所有zip压缩包里边的文件并解压     [ root@localhost  opt]# find . -name "*.tgz" -or -name "*.zip" | while read file; do echo "$file:"; unzip $file; done
 
——————————————————————————————————————————————————————————————————————————
显示所有目录内容    [ root@localhost  opt]# while read f;do echo "$f";done < <(find .)
————————————————————————————————————————————————————————————————————————————
列出50个大文件   [ root@localhost  ~]# find . -type f -name '*.pm' -printf '%6s %p\n' | sort -nr | head -n 50
 
——————————————————————————————————————————————————————————————————————————————
用 echo 查看文件内容  :      [ root@localhost  ~]# echo "$(<d.txt)"
————————————————————————————————————————————————————————————————————————
关闭孤儿终端   [ root@localhost  ~]# skill -KILL -t ttyS0
 
————————————————————————————————————————————————————————————————————————————————-
 
生成一个随机MAC地址  :  [ root@localhost  ~]# od -An -N10 -x /dev/random | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'
 
——————————————————————————————————————————————————————————————————————————————————
显示一个文件的去掉注释和空行的正文部分  :    [ root@localhost  ~]# egrep '^[^#]' jj.sh
 
————————————————————————————————————————————————————————————————————————
 
找到最大的5个文件 : [ root@localhost  ~]# find . -type f -exec ls -s {} \; | sort -n -r | head -5
 
——————————————————————————————————————————————————————————————————————————————
查看是32还是64       [17:33:14 root:/tmp 173] # grep " lm " /proc/cpuinfo > /dev/null && echo "64-bit" || echo "32-bit"
待续。。。
————————————————————————————————————————————————————————————————————————————
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自zhiwen1022.iteye.com/blog/2171117