SlideShare a Scribd company logo
1 of 24
Effective Linux(2):
tools
hongjiang 2012.2.29
tools
► findutils 包
► procps 包
► coreutils 包
► util-linux 包
► ssh
findutils
► $ dpkg -L findutils | grep bin/
/usr/bin/find
/usr/bin/oldfind
/usr/bin/xargs
find + xargs 威力非常强大
findutils
► 查找所有mp3/MP3文件
$ find –iname “*.mp3”
► 查找当前目录下所有后缀是.c或者.h的文件,但是
忽略.svn目录
$ find . -type f -name '*.[ch]' -not -regex './.svn.*'
► 找出所有 .svn 目录下的文件:
$ find . -type f -regex '.*/.svn.*‘
findutils
► 删除当前目录下的空目录
$ find -type d -empty | xargs rmdir
►当前目录下文件长度大于1 M字节的文件:
$ find . -size +1M -print
$ find . –size -10M 小于10M的
单位:k, M, G, c(bytes)
findutils
►find 的时间参数 -mtime 修改时间 -atime 访
问时间
$find / -amin -10 # 查找系统中最后10分钟访问的文件
$find / -atime -2 # 查找系统中最后48小时访问的文件
$find / -mmin -5 # 查找系统中最后5分钟里修改过的文件
$find / -mtime -1 #查找系统中最后24小时里修改过的文件
$find / -mtime +7 #查找7天前的文件
findutils
► $ find -iregex ".*.(xml|java|vm)" | grep -v "test/" |
xargs grep ldap
► $ find -type f | sed -n '/test//d;/(.java|.vm|.xml)$/p' |
xargs grep ldap
第二个在一些低版本的linux (redhat as3) 上更快一些,
在高版本上两者已经接近。(可能首次执行时
iregex要慢一些,之后再执行已经很接近了)
findutils
► find 避开某个子目录的问题
$ find . -path "./a/b" -prune -o -type f
# 虽然不会列出./a/b目录下的文件,但这样会把 ./a/b 这个文件也打印
出来。上面的命令和 find . ( -path './a/b' -prune -o -type f ) -print 效
果是一样的。find 不指定-print参数,默认会对满足条件的进行print操作
$ find . -path "./a/b" -prune -o -type f -print # 这样没有问题。
它和
$ find . -path "./a/b" -prune -o ( -type f -a -print ) 效果一样。
把逻辑顺序交换一下,find -type f -print -o -path ‘./a/b’ -prune 也能得
到一样的效果,相当于:
$ find ( -type f -print ) -o -path './a/b' -prune
findutils
► xargs 的作用也很大
► 目录下文件太多,无法用 rm * 删除时
可以用 ls | xargs rm 一次删除
► 对文本上下两行合并
$ cat file | xargs –d’n’ –n2
procps
► hongjiang@whj ~ % dpkg -L procps | grep bin/ | xargs -i basename {}
| xargs -n5 | column -t
kill ps sysctl free pgrep
pmap pwdx skill slabtop tload
top uptime vmstat w.procps watch
pkill snice
► 需要掌握: ps kill free pgrep pmap pwdx top uptime vmstat pkill
procps
► ps 查看线程
$ ps –eL | grep java
$ ps axms (BSD风格?,不好看)
若只是查看某个进程的线程数:
$ ps –onlwp –C java
$ ps –moTHREAD –C java
procps
►以CPU占用率为序显示进程
$ ps -eo pcpu,cmd | sort -nk1
►以MEM占用率为序显示进程
$ ps -eo pmem,cmd | sort -nk1
或:
$ ps –eo rss,cmd | sort –nk1
procps
► $ top -b -d 1 -n 3600 | grep java
一秒钟取一次取3600次 (1个小时)后结束
► $ top -b -d 1 –n 100 -p pid
单显示指定的进程
procps
► 定位哪些java线程 的使用率超过 50%
$ top -H -b -p 3260 | awk '/java/ && $9>50‘
► 有个线程cpu 100%,找出来
$ top -H -b -p 3260 | grep 100
3352 hongjian 20 0 1242m 37m 11m R 100 1.0 9:18.03 java
3352 hongjian 20 0 1242m 37m 11m R 100 1.0 9:21.02 java
3352 hongjian 20 0 1242m 37m 11m R 100 1.0 9:24.02 java
procps
► $ vmstat -S m 1 100
每秒钟刷新一次,执行100次。用m作为单位。
$ pgrep java 获取所有java进程的pid
注,sysvinit-utils下有个pidof命令,两者相同
$ pwdx $$ 当前进程的pwd环境变量(启动时所在的
路径)
procps
► $ pmap pid 查看进程的地址空间分布
coreutils
$ dpkg -L coreutils | grep bin/ | xargs -i basename {} | xargs -n6 | column -t
cat chgrp chmod chown cp date
dd df dir echo false ln
ls mkdir mknod mv pwd readlink
rm rmdir vdir sleep stty sync
touch true uname mktemp install hostid
nice who users pinky stdbuf [
base64 basename chcon cksum comm csplit
cut dircolors dirname du env expand
expr factor fmt fold groups head
id join link logname md5sum mkfifo
nl nproc nohup od paste pathchk
pr printenv printf ptx runcon seq
sha1sum sha224sum sha256sum sha384sum sha512sum shred
shuf sort split stat sum tac
tail tee test timeout tr truncate
tsort tty unexpand uniq unlink wc
whoami yes arch chroot touch md5sum.textutils
coreutils
► Coreutils 是linux下命令最多的一个包,也是
最基础的一个工具包。大部分都需要了解。
► $ printf %0x 1234 打印16进制
► $ seq 0 5 100 步长为5打印0-100之间
► $ seq –s, 0 10 产生 0,1,2,3…10
coreutils
► $ echo “hello” | base64
►$ base64 –d <<< “aGVsbG8K”
► $ basename /a/b/c/name
► $ dirname /a/b/c/name
coreutils
► 每行不超过100个字符
$ cat file | cut –b1-100
► 统计当前目录下每个子目录下有多少个文件:
$ ls -F | grep '/' | xargs -i find {} -type f | cut -d'/' -f2 |
uniq -c
coreutils
► 统计代码行数(不精准)
$ find –name “*.java” | xargs wc –l | tail -1
一些不常用的:
$ find . –type f | shuf 洗牌
$ factor 12345 可用来判断是不是质数
util-linux
► $ dpkg -L util-linux| grep bin/ | xargs -i basename {} | xargs -n6 |
column –t
lsblk tailf dmesg more mkfs mkswap
fsck.minix mkfs.minix mkfs.bfs blockdev swaplabel raw
fsck.cramfs mkfs.cramfs fdisk sfdisk cfdisk agetty
blkid findfs wipefs ctrlaltdel fsfreeze fstrim
pivot_root switch_root hwclock isosize getty fsck
getopt addpart ddate delpart mcookie namei
partx whereis rename.ul setterm chkdupexe chrt
ionice taskset flock ipcrm ipcs ipcmk
setsid setarch lscpu fallocate unshare rev
line pg fdformat cytune readprofile ldattach
tunelp rtcwake x86_64 i386 linux64 linux32
util-linux
► kill apache进程时别忘了清除共享内存
在我们的服务器启动脚本中都有的:
$ ipcs -m | grep admin | awk '{print $2}' | xargs ipcrm shm
$ ipcs -s | grep admin | awk '{print $2}' | xargs ipcrm sem
util-linux
► $ rev 命令 与 tac 命令 对比
► $ echo "hello" | rev
► $ echo "hellonworld" | tac

More Related Content

What's hot

Mac os Terminal 常用指令與小技巧
Mac os Terminal 常用指令與小技巧Mac os Terminal 常用指令與小技巧
Mac os Terminal 常用指令與小技巧Chen Liwei
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享Chong-Kuan Chen
 
BASH 漏洞深入探討
BASH 漏洞深入探討BASH 漏洞深入探討
BASH 漏洞深入探討Tim Hsu
 
常用Mac/Linux命令分享
常用Mac/Linux命令分享常用Mac/Linux命令分享
常用Mac/Linux命令分享Yihua Huang
 
Linux基础
Linux基础Linux基础
Linux基础zhuqling
 
DNS协议与应用简介
DNS协议与应用简介DNS协议与应用简介
DNS协议与应用简介琛琳 饶
 
Monitor is all for ops
Monitor is all for opsMonitor is all for ops
Monitor is all for ops琛琳 饶
 
线上问题排查交流
线上问题排查交流线上问题排查交流
线上问题排查交流Edward Lee
 
Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Kris Mok
 
Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)fisher zheng
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern理 傅
 
Introduction to FreeBSD commands
Introduction to FreeBSD commandsIntroduction to FreeBSD commands
Introduction to FreeBSD commands郁凱 黃
 
Golang advance
Golang advanceGolang advance
Golang advancerfyiamcool
 
分布式系统缓存设计
分布式系统缓存设计分布式系统缓存设计
分布式系统缓存设计zhujiadun
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceHo Kim
 
分布式系统缓存设计
分布式系统缓存设计分布式系统缓存设计
分布式系统缓存设计aleafs
 
Mysql展示功能与源码对应
Mysql展示功能与源码对应Mysql展示功能与源码对应
Mysql展示功能与源码对应zhaolinjnu
 

What's hot (19)

Mac os Terminal 常用指令與小技巧
Mac os Terminal 常用指令與小技巧Mac os Terminal 常用指令與小技巧
Mac os Terminal 常用指令與小技巧
 
Windbg入门
Windbg入门Windbg入门
Windbg入门
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 
BASH 漏洞深入探討
BASH 漏洞深入探討BASH 漏洞深入探討
BASH 漏洞深入探討
 
常用Mac/Linux命令分享
常用Mac/Linux命令分享常用Mac/Linux命令分享
常用Mac/Linux命令分享
 
Linux基础
Linux基础Linux基础
Linux基础
 
DNS协议与应用简介
DNS协议与应用简介DNS协议与应用简介
DNS协议与应用简介
 
Monitor is all for ops
Monitor is all for opsMonitor is all for ops
Monitor is all for ops
 
线上问题排查交流
线上问题排查交流线上问题排查交流
线上问题排查交流
 
Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)
 
Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern
 
Introduction to FreeBSD commands
Introduction to FreeBSD commandsIntroduction to FreeBSD commands
Introduction to FreeBSD commands
 
Golang advance
Golang advanceGolang advance
Golang advance
 
Zsh
ZshZsh
Zsh
 
分布式系统缓存设计
分布式系统缓存设计分布式系统缓存设计
分布式系统缓存设计
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical Experience
 
分布式系统缓存设计
分布式系统缓存设计分布式系统缓存设计
分布式系统缓存设计
 
Mysql展示功能与源码对应
Mysql展示功能与源码对应Mysql展示功能与源码对应
Mysql展示功能与源码对应
 

Viewers also liked

Hash map导致cpu100% 的分析
Hash map导致cpu100% 的分析Hash map导致cpu100% 的分析
Hash map导致cpu100% 的分析wang hongjiang
 
Exodus重构和向apollo迁移
Exodus重构和向apollo迁移Exodus重构和向apollo迁移
Exodus重构和向apollo迁移wang hongjiang
 
深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)wang hongjiang
 
深入剖析Concurrent hashmap中的同步机制(下)
深入剖析Concurrent hashmap中的同步机制(下)深入剖析Concurrent hashmap中的同步机制(下)
深入剖析Concurrent hashmap中的同步机制(下)wang hongjiang
 
Effective linux.3.(diagnosis)
Effective linux.3.(diagnosis)Effective linux.3.(diagnosis)
Effective linux.3.(diagnosis)wang hongjiang
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3shinolajla
 
中等创业公司后端技术选型
中等创业公司后端技术选型中等创业公司后端技术选型
中等创业公司后端技术选型wang hongjiang
 
Automatic Scaling Iterative Computations
Automatic Scaling Iterative ComputationsAutomatic Scaling Iterative Computations
Automatic Scaling Iterative ComputationsGuozhang Wang
 
Behavioral Simulations in MapReduce
Behavioral Simulations in MapReduceBehavioral Simulations in MapReduce
Behavioral Simulations in MapReduceGuozhang Wang
 
Apache Kafka at LinkedIn
Apache Kafka at LinkedInApache Kafka at LinkedIn
Apache Kafka at LinkedInGuozhang Wang
 
Apache Kafka, and the Rise of Stream Processing
Apache Kafka, and the Rise of Stream ProcessingApache Kafka, and the Rise of Stream Processing
Apache Kafka, and the Rise of Stream ProcessingGuozhang Wang
 
Building Realtim Data Pipelines with Kafka Connect and Spark Streaming
Building Realtim Data Pipelines with Kafka Connect and Spark StreamingBuilding Realtim Data Pipelines with Kafka Connect and Spark Streaming
Building Realtim Data Pipelines with Kafka Connect and Spark StreamingGuozhang Wang
 
Building a Replicated Logging System with Apache Kafka
Building a Replicated Logging System with Apache KafkaBuilding a Replicated Logging System with Apache Kafka
Building a Replicated Logging System with Apache KafkaGuozhang Wang
 
Building Stream Infrastructure across Multiple Data Centers with Apache Kafka
Building Stream Infrastructure across Multiple Data Centers with Apache KafkaBuilding Stream Infrastructure across Multiple Data Centers with Apache Kafka
Building Stream Infrastructure across Multiple Data Centers with Apache KafkaGuozhang Wang
 

Viewers also liked (20)

聊一些电影
聊一些电影聊一些电影
聊一些电影
 
Exodus2 大局观
Exodus2 大局观Exodus2 大局观
Exodus2 大局观
 
Hash map导致cpu100% 的分析
Hash map导致cpu100% 的分析Hash map导致cpu100% 的分析
Hash map导致cpu100% 的分析
 
Exodus重构和向apollo迁移
Exodus重构和向apollo迁移Exodus重构和向apollo迁移
Exodus重构和向apollo迁移
 
深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)
 
深入剖析Concurrent hashmap中的同步机制(下)
深入剖析Concurrent hashmap中的同步机制(下)深入剖析Concurrent hashmap中的同步机制(下)
深入剖析Concurrent hashmap中的同步机制(下)
 
Aswan&hump
Aswan&humpAswan&hump
Aswan&hump
 
Effective linux.3.(diagnosis)
Effective linux.3.(diagnosis)Effective linux.3.(diagnosis)
Effective linux.3.(diagnosis)
 
Enum开锁
Enum开锁Enum开锁
Enum开锁
 
Jvm内存管理基础
Jvm内存管理基础Jvm内存管理基础
Jvm内存管理基础
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3
 
Ali-tomcat
Ali-tomcatAli-tomcat
Ali-tomcat
 
中等创业公司后端技术选型
中等创业公司后端技术选型中等创业公司后端技术选型
中等创业公司后端技术选型
 
Automatic Scaling Iterative Computations
Automatic Scaling Iterative ComputationsAutomatic Scaling Iterative Computations
Automatic Scaling Iterative Computations
 
Behavioral Simulations in MapReduce
Behavioral Simulations in MapReduceBehavioral Simulations in MapReduce
Behavioral Simulations in MapReduce
 
Apache Kafka at LinkedIn
Apache Kafka at LinkedInApache Kafka at LinkedIn
Apache Kafka at LinkedIn
 
Apache Kafka, and the Rise of Stream Processing
Apache Kafka, and the Rise of Stream ProcessingApache Kafka, and the Rise of Stream Processing
Apache Kafka, and the Rise of Stream Processing
 
Building Realtim Data Pipelines with Kafka Connect and Spark Streaming
Building Realtim Data Pipelines with Kafka Connect and Spark StreamingBuilding Realtim Data Pipelines with Kafka Connect and Spark Streaming
Building Realtim Data Pipelines with Kafka Connect and Spark Streaming
 
Building a Replicated Logging System with Apache Kafka
Building a Replicated Logging System with Apache KafkaBuilding a Replicated Logging System with Apache Kafka
Building a Replicated Logging System with Apache Kafka
 
Building Stream Infrastructure across Multiple Data Centers with Apache Kafka
Building Stream Infrastructure across Multiple Data Centers with Apache KafkaBuilding Stream Infrastructure across Multiple Data Centers with Apache Kafka
Building Stream Infrastructure across Multiple Data Centers with Apache Kafka
 

Similar to Effective linux.2.(tools)

Shell奇技淫巧
Shell奇技淫巧Shell奇技淫巧
Shell奇技淫巧David Xie
 
Linux基础
Linux基础Linux基础
Linux基础Eric Lo
 
網路組-Ubuntu介紹
網路組-Ubuntu介紹網路組-Ubuntu介紹
網路組-Ubuntu介紹maryqute520
 
Command line 初級寶典
Command line 初級寶典Command line 初級寶典
Command line 初級寶典Tom Chen
 
Linux常用命令与工具简介
Linux常用命令与工具简介Linux常用命令与工具简介
Linux常用命令与工具简介weihe
 
4, files & folders
4, files & folders4, files & folders
4, files & foldersted-xu
 
Unix常用命令 1
Unix常用命令 1Unix常用命令 1
Unix常用命令 1tony2yy_fish
 
新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹fweng322
 
Linux command tutorial
Linux command tutorialLinux command tutorial
Linux command tutorial朋 陈
 
利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版xingsu1021
 
Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版redhat9
 
5, system admin
5, system admin5, system admin
5, system adminted-xu
 
CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)
CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)
CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)Jian-Kai Wang
 
開發流程與工具介紹
開發流程與工具介紹開發流程與工具介紹
開發流程與工具介紹Shengyou Fan
 
1, shell intro
1, shell intro1, shell intro
1, shell introted-xu
 
Bigdata 大資料分析實務 (進階上機課程)
Bigdata 大資料分析實務 (進階上機課程)Bigdata 大資料分析實務 (進階上機課程)
Bigdata 大資料分析實務 (進階上機課程)家雋 莊
 

Similar to Effective linux.2.(tools) (20)

Shell奇技淫巧
Shell奇技淫巧Shell奇技淫巧
Shell奇技淫巧
 
Linux基础
Linux基础Linux基础
Linux基础
 
網路組-Ubuntu介紹
網路組-Ubuntu介紹網路組-Ubuntu介紹
網路組-Ubuntu介紹
 
Rootkit 101
Rootkit 101Rootkit 101
Rootkit 101
 
Command line 初級寶典
Command line 初級寶典Command line 初級寶典
Command line 初級寶典
 
Linux学习
Linux学习Linux学习
Linux学习
 
Linux常用命令与工具简介
Linux常用命令与工具简介Linux常用命令与工具简介
Linux常用命令与工具简介
 
Linuxcommand
LinuxcommandLinuxcommand
Linuxcommand
 
4, files & folders
4, files & folders4, files & folders
4, files & folders
 
Unix常用命令 1
Unix常用命令 1Unix常用命令 1
Unix常用命令 1
 
新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹
 
Linux command tutorial
Linux command tutorialLinux command tutorial
Linux command tutorial
 
利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版
 
Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版Bypat博客出品-利用cent os快速构建自己的发行版
Bypat博客出品-利用cent os快速构建自己的发行版
 
Ch7
Ch7Ch7
Ch7
 
5, system admin
5, system admin5, system admin
5, system admin
 
CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)
CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)
CKAN : 資料開放平台技術介紹 (CAKN : Technical Introduction to Open Data Portal)
 
開發流程與工具介紹
開發流程與工具介紹開發流程與工具介紹
開發流程與工具介紹
 
1, shell intro
1, shell intro1, shell intro
1, shell intro
 
Bigdata 大資料分析實務 (進階上機課程)
Bigdata 大資料分析實務 (進階上機課程)Bigdata 大資料分析實務 (進階上機課程)
Bigdata 大資料分析實務 (進階上機課程)
 

Effective linux.2.(tools)

  • 2. tools ► findutils 包 ► procps 包 ► coreutils 包 ► util-linux 包 ► ssh
  • 3. findutils ► $ dpkg -L findutils | grep bin/ /usr/bin/find /usr/bin/oldfind /usr/bin/xargs find + xargs 威力非常强大
  • 4. findutils ► 查找所有mp3/MP3文件 $ find –iname “*.mp3” ► 查找当前目录下所有后缀是.c或者.h的文件,但是 忽略.svn目录 $ find . -type f -name '*.[ch]' -not -regex './.svn.*' ► 找出所有 .svn 目录下的文件: $ find . -type f -regex '.*/.svn.*‘
  • 5. findutils ► 删除当前目录下的空目录 $ find -type d -empty | xargs rmdir ►当前目录下文件长度大于1 M字节的文件: $ find . -size +1M -print $ find . –size -10M 小于10M的 单位:k, M, G, c(bytes)
  • 6. findutils ►find 的时间参数 -mtime 修改时间 -atime 访 问时间 $find / -amin -10 # 查找系统中最后10分钟访问的文件 $find / -atime -2 # 查找系统中最后48小时访问的文件 $find / -mmin -5 # 查找系统中最后5分钟里修改过的文件 $find / -mtime -1 #查找系统中最后24小时里修改过的文件 $find / -mtime +7 #查找7天前的文件
  • 7. findutils ► $ find -iregex ".*.(xml|java|vm)" | grep -v "test/" | xargs grep ldap ► $ find -type f | sed -n '/test//d;/(.java|.vm|.xml)$/p' | xargs grep ldap 第二个在一些低版本的linux (redhat as3) 上更快一些, 在高版本上两者已经接近。(可能首次执行时 iregex要慢一些,之后再执行已经很接近了)
  • 8. findutils ► find 避开某个子目录的问题 $ find . -path "./a/b" -prune -o -type f # 虽然不会列出./a/b目录下的文件,但这样会把 ./a/b 这个文件也打印 出来。上面的命令和 find . ( -path './a/b' -prune -o -type f ) -print 效 果是一样的。find 不指定-print参数,默认会对满足条件的进行print操作 $ find . -path "./a/b" -prune -o -type f -print # 这样没有问题。 它和 $ find . -path "./a/b" -prune -o ( -type f -a -print ) 效果一样。 把逻辑顺序交换一下,find -type f -print -o -path ‘./a/b’ -prune 也能得 到一样的效果,相当于: $ find ( -type f -print ) -o -path './a/b' -prune
  • 9. findutils ► xargs 的作用也很大 ► 目录下文件太多,无法用 rm * 删除时 可以用 ls | xargs rm 一次删除 ► 对文本上下两行合并 $ cat file | xargs –d’n’ –n2
  • 10. procps ► hongjiang@whj ~ % dpkg -L procps | grep bin/ | xargs -i basename {} | xargs -n5 | column -t kill ps sysctl free pgrep pmap pwdx skill slabtop tload top uptime vmstat w.procps watch pkill snice ► 需要掌握: ps kill free pgrep pmap pwdx top uptime vmstat pkill
  • 11. procps ► ps 查看线程 $ ps –eL | grep java $ ps axms (BSD风格?,不好看) 若只是查看某个进程的线程数: $ ps –onlwp –C java $ ps –moTHREAD –C java
  • 12. procps ►以CPU占用率为序显示进程 $ ps -eo pcpu,cmd | sort -nk1 ►以MEM占用率为序显示进程 $ ps -eo pmem,cmd | sort -nk1 或: $ ps –eo rss,cmd | sort –nk1
  • 13. procps ► $ top -b -d 1 -n 3600 | grep java 一秒钟取一次取3600次 (1个小时)后结束 ► $ top -b -d 1 –n 100 -p pid 单显示指定的进程
  • 14. procps ► 定位哪些java线程 的使用率超过 50% $ top -H -b -p 3260 | awk '/java/ && $9>50‘ ► 有个线程cpu 100%,找出来 $ top -H -b -p 3260 | grep 100 3352 hongjian 20 0 1242m 37m 11m R 100 1.0 9:18.03 java 3352 hongjian 20 0 1242m 37m 11m R 100 1.0 9:21.02 java 3352 hongjian 20 0 1242m 37m 11m R 100 1.0 9:24.02 java
  • 15. procps ► $ vmstat -S m 1 100 每秒钟刷新一次,执行100次。用m作为单位。 $ pgrep java 获取所有java进程的pid 注,sysvinit-utils下有个pidof命令,两者相同 $ pwdx $$ 当前进程的pwd环境变量(启动时所在的 路径)
  • 16. procps ► $ pmap pid 查看进程的地址空间分布
  • 17. coreutils $ dpkg -L coreutils | grep bin/ | xargs -i basename {} | xargs -n6 | column -t cat chgrp chmod chown cp date dd df dir echo false ln ls mkdir mknod mv pwd readlink rm rmdir vdir sleep stty sync touch true uname mktemp install hostid nice who users pinky stdbuf [ base64 basename chcon cksum comm csplit cut dircolors dirname du env expand expr factor fmt fold groups head id join link logname md5sum mkfifo nl nproc nohup od paste pathchk pr printenv printf ptx runcon seq sha1sum sha224sum sha256sum sha384sum sha512sum shred shuf sort split stat sum tac tail tee test timeout tr truncate tsort tty unexpand uniq unlink wc whoami yes arch chroot touch md5sum.textutils
  • 18. coreutils ► Coreutils 是linux下命令最多的一个包,也是 最基础的一个工具包。大部分都需要了解。 ► $ printf %0x 1234 打印16进制 ► $ seq 0 5 100 步长为5打印0-100之间 ► $ seq –s, 0 10 产生 0,1,2,3…10
  • 19. coreutils ► $ echo “hello” | base64 ►$ base64 –d <<< “aGVsbG8K” ► $ basename /a/b/c/name ► $ dirname /a/b/c/name
  • 20. coreutils ► 每行不超过100个字符 $ cat file | cut –b1-100 ► 统计当前目录下每个子目录下有多少个文件: $ ls -F | grep '/' | xargs -i find {} -type f | cut -d'/' -f2 | uniq -c
  • 21. coreutils ► 统计代码行数(不精准) $ find –name “*.java” | xargs wc –l | tail -1 一些不常用的: $ find . –type f | shuf 洗牌 $ factor 12345 可用来判断是不是质数
  • 22. util-linux ► $ dpkg -L util-linux| grep bin/ | xargs -i basename {} | xargs -n6 | column –t lsblk tailf dmesg more mkfs mkswap fsck.minix mkfs.minix mkfs.bfs blockdev swaplabel raw fsck.cramfs mkfs.cramfs fdisk sfdisk cfdisk agetty blkid findfs wipefs ctrlaltdel fsfreeze fstrim pivot_root switch_root hwclock isosize getty fsck getopt addpart ddate delpart mcookie namei partx whereis rename.ul setterm chkdupexe chrt ionice taskset flock ipcrm ipcs ipcmk setsid setarch lscpu fallocate unshare rev line pg fdformat cytune readprofile ldattach tunelp rtcwake x86_64 i386 linux64 linux32
  • 23. util-linux ► kill apache进程时别忘了清除共享内存 在我们的服务器启动脚本中都有的: $ ipcs -m | grep admin | awk '{print $2}' | xargs ipcrm shm $ ipcs -s | grep admin | awk '{print $2}' | xargs ipcrm sem
  • 24. util-linux ► $ rev 命令 与 tac 命令 对比 ► $ echo "hello" | rev ► $ echo "hellonworld" | tac