Linux Commands

几个比较有用的Linux命令:

  • xsel(pbcopy,pbpaste): 复制粘贴
  • sort: 排序
  • uniq: 去重
  • nc: netcat
  • fold:
  • jq:
  • open: 使用某应用大开文件
  • system: MySQL命令行环境下使用系统命令
  • paste: 合并文件
  • split: 分割文件
  • readlink: 读取文件全路径

参考文档:

GNU Plot
操作图片


1. xsel : manipulate the X selection.

The X server maintains three selections, called PRIMARY, SECONDARY and CLIPBOARD.

1
2
3
4
5
# 输出复制到剪切板
ls | xsel -bi

# write selection to stanrd output输出到标准输出
xsel -bo


2. sort : sort lines of text files

举例说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#源文件
$ cat file
12
23
11
1243
99
43
889
322112

# text sort
$ sort file
11
12
1243
23
322112
43
889
99


# numeric sort
$ sort -n file
11
12
23
43
99
889
1243
322112


3. uniq : report or omit repeated lines

Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).
过滤相邻的重复行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#源文件
$ cat file
12
12
12
23
23
23
11
1243
22
43
1234
6
2
90
6
99
11
12
99
43


# 去除连续重复的行
$ uniq file
12
23
11
1243
22
43
1234
6
2
90
6
99
11
12
99
43


#打印每一行重复数
$ uniq -c file
3 12
3 23
1 11
1 1243
1 22
1 43
1 1234
1 6
1 2
1 90
1 6
1 99
1 11
1 12
1 99
1 43

# uniq -d file
12
23

对上述文件按数值排序,去除重复

1
2
3
4
5
6
7
8
9
10
11
12
$ sort -n file | uniq
2
6
11
12
22
23
43
90
99
1234
1243


4. nc : netcat

1
2
3
4
5
#监听某个端口
nc -l 12345

#向某个端口发送数据
$ cat argv.py | nc localhost 12345

5. fold

fold long lines for finite width output device

The fold utility is a filter which folds the contents of the specified files, or the standard input if no files are specified, breaking the lines to have a maximum of 80 columns.

6. jq

Command-line JSON processor

jq can transform JSON in various ways, by selecting, iterating, reducing and otherwise mangling JSON documents.

Mac系统默认是没有jq命令的, 需要先安装

1
$ brew install jq

使用示例, 将不规范的Json数据格式化显示

1
2
3
4
5
6
# 查询consul服务器得到不规范的数据
$ curl host:8500/v1/catalog/service/my-app


#使用jq命令得到格式化输出
$ curl host:8500/v1/catalog/service/my-app |jq


7. open

open files and directories with specified application

The open command opens a file(or a directory or URL), just as if you had double-clicked the file’s icon.

  • 例如, 在shell终端使用idea打开某个Java工程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #首先找到idea启动文件所在的文件夹
    #注意因为路径中有一个空格,因此直接在shell中作为路径使用时, shell无法识别这个路径, 需要加上转义字符
    $ pwd
    /Applications/IntelliJ IDEA.app/Contents/MacOS

    #直接启动idea
    $ /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea

    #或者使用open命令打开某个工程文件
    $ oepen MyProject -a /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea
  • 为IntelliJ Idea配置快捷命令
    在.bashrc中配置alias, 然后可以直接使用idea

    1
    2
    3
    4
    5
    #配置.bashrc
    alias idea='open -a /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea'

    #直接使用idea命令打开demo工程
    $ idea ~/workspace/demo

8. system

pass a command to the shell

The system(command) function hands the argument command to the command interpreter sh. The calling process waits for the shell to finish executing the command, ignoring SIGINT and SIGQUIT, and blocking SIGHLD.

1
2
#但是这个命令无法再shell中执行
$ man system

mysql system

在mysql下运行shell命令

1
2
3
4
5
MySQL> system ls
Applications Go MySQL logs solarized
Downloads Music compare opt workspace

MySQL> system pwd


当一个软链接指向另外一个软链接, 而另外这个软链接又指向其他目标. 这时可以使用-f选项直接获取最终的非软链接的目标. 也可以通过这个命令直接打印某个非链接文件的全路径.
`bash
$ readlink -f part-0000
/home/quantux/workspace/spark/output.txt/part-0000