UID1
现金6831
在线时间561 小时
注册时间2016-12-27
成长值: 35920
黑狼创办人
- 积分
- 6831
|
(题图:毕业旅行之决战青海湖)
本文主要来源于小组内部的一个小的分享, 这里整理成一篇文章po出来. 题目叫 “Shell 助力开发效率提升”, 更切题的应该是叫 “命令行” 提升开发效率, 这里并没有讲到Shell编程, 而是主要介绍Linux 或者 Mac下常用的一些基本工具命令来帮助处理一些日常事务. (其实之前这篇文章Mac 软件推荐(续)之程序猿篇也大致提到了本文中的大部分内容)
通过本文的介绍, 你应该对相关命令有一个初步的了解, 知道比如用什么命令可以完成怎样的操作,
至于具体的参数, 你不用去刻意地记, 等到你用到的时候, 你再去 cmd --help 或者 man cmd去看, 用熟悉了, 常用的你也就记住了.
本文首先介绍了Linux/Mac下一些常用的命令行工具, 然后介绍了一些常用的命令, 最后通过一两个案例来说明这些工具的强大之处:
比如给定一个nginx日志文件, 能够找出HTTP 404 请求最多的top 10 是什么? 比如能找到请求耗时最多的top 10是什么? 再比如能够简单的得到每小时的”PV”是多少?
再比如拿到一篇文章, 能否简单统计一下这篇文章单次词频最高的10个词语是什么?
Mac 环境
- zsh
- on-my-zsh
- plugin
- git
- autojump
- osx(man-preview/quick-look/pfd(print Finder director)/cdf(cd Finder))
- 常用快捷键(bindkey)
- 演示: 高亮/git/智能补全/跳转(j,d)…
关于Mac相关的技巧, 更多的可以参考以下三篇文章: Mac 软件推荐, Mac 软件推荐(续) -- !程序猿篇, 和Mac 软件推荐(续)之程序猿篇
Shell 基础命令
- which/whereis, 常用 whatis, man, --help
.oh-my-zsh git:(master)$ whereis ls /bin/ls .oh-my-zsh git:(master)$ which ls ls: aliased to ls -G
- 基本文件目录操作
rm, mkdir, mv, cp, cd, ls, ln, file, stat, wc(-l/w/c), head, more, tail, cat...
- 利器 管道: |
Shell 文本处理
这里就是通过案例讲了一下12个命令的大致用法和参数, 可以通过点击右边的目录直达你想要了解的命令.
find, grep, xargs, cut, paste, commjoin, sort, uniq, tr, sed, awkfind
- 常用参数
- 文件名 -name, 文件类型-type, 查找最大深度-maxdepth
- 时间过滤(create/access/modify) -[cam]time
- 执行动作 -exec
- 示例
find ./ -name "*.json" find . -maxdepth 7 -name "*.json" -type f find . -name "*.log.gz" -ctime +7 -size +1M -delete (atime/ctime/mtime) find . -name "*.scala" -atime -7 -exec du -h {} \;
grep
- 常用参数
- -v(invert-match),
- -c(count),
- -n(line-number),
- -i(ignore-case),
- -l, -L, -R(-r, —recursive), -e
- 示例
grep 'partner' ./*.scala -l grep -e 'World' -e 'first' -i -R ./ (-e: or)
- 相关命令: grep -z / zgrep / zcat xx | grep
xargs
- 常用参数
- -n(每行列数),
- -I(变量替换)
- -d(分隔符), Mac 不支持, 注意与GNU版本的区别
- 示例
find . -type f -name "*.jpg" | xargs -n1 -I {} du -sh {}
cut
- 常用参数
- -b(字节)
- -c(字符)
- -f(第几列), -d(分隔符), f范围: n, n-, -m, n-m
- 示例
echo "helloworldhellp" | cut -c1-10 cut -d, -f2-8 csu.db.export.csv
paste
- 常用参数
- 示例
Documents$ cat file1 1 11 2 22 3 33 4 44 Documents$ cat file2 one 1 two 2 three 3 one1 4 Documents$ paste -d, file1 file2 1 11,one 1 2 22,two 2 3 33,three 3 4 44,one1 4 Documents$ paste -s -d: file1 file2 a 11:b bb:3 33:4 44 one 1:two 2:three 3:one1 4
join
类似sql中的 ...inner join ...on ..., -t 分隔符, 默认为空格或tab
Documents$ cat j11 112 223 334 445 55 Documents$ cat j2one 1 0one 2 1two 4 2three 5 3one1 5 4 Documents$ join -1 1 -2 3 j1 j21 11 one 22 22 two 43 33 three 54 44 one1 5comm
<ul style="margin: 1em 0px;padding-left: 2em;" class=" list-paddingleft-2">常用参数
- 用法 comm [-123i] file1 file2
- 字典序列, 3列: 只在file1/file2/both
- - 去掉某列, i 忽略大小写
示例 |
上一篇:用Python从头开发一个自己的Shell(上)下一篇:知道这些 Linux 命令,走上JAVA人生巅峰!
|