您的位置:首页 > 运维架构 > Linux

Linux下又用得命令行汇总

2016-05-30 08:31 549 查看
原文:https://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix

A few simple but useful examples:

Set intersection, union, and difference of text files via sort/uniq: Suppose a and b are text files that are already uniqued. Note this is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need
to use -T.) For comparison, see how many lines it is to write a disk-based merge sort in Java.
cat a b | sort | uniq > c   # c is a union b
cat a b | sort | uniq -d > c   # c is a intersect b
cat a b b | sort | uniq -u > c   # c is set difference a - b
Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python):
awk '{ x += $3 } END { print x }' myfile
If want to see sizes/dates on a tree of files, this is like a recursive "ls -l" but is easier to read than "ls -lR":
find . -type f -ls
Use xargs. It's very powerful. Note you can control how many items execute per line (-L) as well as parallelism (-P). If you're not sure if it'll do the right thing, use xargs echo first. Also, -I{} is handy. Examples:
find . -name \*.py | xargs grep some_function
cat hosts | xargs -I{} ssh root@{} hostname

Say you have a text file, like a web server log, and a certain value that appears on some lines, such as an acct_id parameter that is present in the URL. If you want a tally of how many requests for each acct_id:
cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux