Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

linux常用命令记录

常用命令记录

scp

远程复制到本地

1
2
scp -r lianhui.klh@30.37.92.42:/Users/kanlianhui/workspace/  /local/dir
sudo -u admin scp -r lianhui.klh@11.179.119.42:/home/admin/batch* /home/admin/

本地复制到远程

1
scp -r /local/dir lianhui.klh@30.37.92.42:/Users/kanlianhui/workspace/

pmap

report memory map of a process
查看进程的内存映射

1
pmap 1536 | grep spring | awk '{print $4}' | sort

awk

params

1
2
3
4
-F fs
--field-separator fs
Use fs for the input field separator (the value of the FS predefined variable).
指定分隔符

exp
删除大文件

1
ls -l | awk '{if($5>2000000000) print $9}' | xargs sudo -u admin  rm -f

uniq

uniq 检查重复行的时候,只会检查相邻的行.所以一般和sort一起使用

params

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
-c, --count
prefix lines by the number of occurrences
在每列旁边显示该行重复出现的次数
-d, --repeated
only print duplicate lines, one for each group
仅显示重复出现的行列
-f, --skip-fields=N
avoid comparing the first N fields
-f 忽略的段数,-f 1 忽略第一段
--group[=METHOD]
show all items, separating groups with an empty line;
METHOD={separate(default),prepend,append,both}
-i, --ignore-case
ignore differences in case when comparing
不区分大小写
-s, --skip-chars=N
avoid comparing the first N characters
根-f有点像,不过-s是忽略,后面多少个字符 -s 5就忽略后面5个字符
-u, --unique
only print unique lines
全部显示出来,根mysql的distinct功能上有点像
-z, --zero-terminated
line delimiter is NUL, not newline
-w, --check-chars=N
compare no more than N characters in lines
对每行第N 个字符以后的内容不作对照

exp

1
cat /home/admin/output/logs/usr/radiance/radiance.log | grep 'FUCK' | awk '{print $7}' | sort |uniq

几个命令组合使用

1
grep 'com.xxx.china.bolttask.common' xxx.log | awk -F '[' '{print $6}' | sort | uniq -c | sort -rn | head -n 5

tar

解压

1
sudo -u admin tar -zxvf taobao-hsf.tgz 

压缩

1
tar -zcvf xxx.tar.gz ./xxx.log

grep

params

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
-R, -r, --recursive
Read all files under each directory, recursively; this is equivalent to the -d recurse option.
遍历所有文件
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX .)
显示匹配的文件名称
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX .)
反转查找不匹配的行
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX .)
固定字符串列表查找
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines. (-c is specified by POSIX .)
显示匹配行数
-n, --line-number
Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX .)
显示匹配行数的行号
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
只输出匹配到的内容
-b, --byte-offset
Print the 0-based byte offset within the input file before each line of output. If -o (--only-matching) is specified, print the offset of the matching part itself.
输出从0开始的字节偏移地址
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX .)
忽略大小写
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
输出后x行
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
输出前x行
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
输出前后x行

exp
查找包含StringUtils的jar包

1
grep -rl 'StringUtils' ./libs/

查询不包含|00|

1
cat eagleeye.log* | grep Daoli | grep queryInvitationsInSeller | grep -vF '|00|' | more

匹配正则

1
2
cat /home/admin/logs/hsf/hsf.log  | grep -E 'TraceId=.*00\w{5},'
ps -ef | grep java | grep -Eo 'taskname=\w*\s'

颜色显示匹配

1
grep --color 'OfferQueryService' /home/admin/logs/xxx.log

排除grep自身

1
ps -ef| grep java | grep -v grep | awk '{print $2}'

curl

params

1
2
3
-I/--head   只显示响应头
-s/--silent 静默方式
-#/--progress-bar 只显示进度条

exp
只显示响应头

1
2
3
4
5
6
7
8
9
➜  ~ curl -I "https://res.xxx.com/fly/irecom.do?recid=77721&uid=b2b-3434172625c2082"
HTTP/1.1 302 Found
Date: Wed, 18 Apr 2018 02:36:10 GMT
Connection: keep-alive
Location: https://login.xxx.com/jump?target=https%3A%2F%2Fres.xxx.com%2Ffly%2Firecom.do%3Ftbpm%3D1%26recid%3D77721%26uid%3Db2b-3434172625c2082
Server: Tengine/Aserver
EagleEye-TraceId: 0bb6128415240189703251686e711a
Strict-Transport-Security: max-age=31536000
Timing-Allow-Origin: *

top

实时动态地查看系统的整体运行情况

doc

params

1
2
3
4
5
6
7
-a : Sort by memory usage
This switch makes top to sort the processes by allocated memory
-H : Threads toggle
Starts top with the last remembered 'H' state reversed. When this toggle is On, all individual threads will be displayed. Otherwise, top displays a summation of all threads in a process.
-p : Monitor PIDs as: -pN1 -pN2 ... or -pN1, N2 [,...]
Monitor only processes with specified process IDs. This option can be given up to 20 times, or you can provide a comma delimited list with up to 20 pids. Co-mingling both approaches is permitted.
This is a command-line option only. And should you wish to return to normal operation, it is not necessary to quit and and restart top -- just issue the '=' interactive command.

查看指定进程中的线程占用cpu的情况

1
top -Hp <pid>

du

du命令是对文件和目录磁盘使用的空间的查看,默认是查看的目录,可以用参数-a查看所有文件。

参数列表:

  • -a--all:列出所有文件和目录的磁盘使用情况,而不仅仅是目录。
  • -c--total:在最后输出所有选定文件的总计。
  • -h--human-readable:以可读性更高的格式(如KB、MB、GB)显示文件大小。
  • -s--summarize:只显示总计,即当前目录的总磁盘使用量而不显示其下各个子目录或文件的磁盘使用量。
  • -d--max-depth=N:查找到指定深度N的目录树磁盘使用情况,不会更深入地查找。
  • -x--one-file-system:只检查和输出当前文件系统的磁盘使用情况,忽略挂载的其他文件系统。
  • -L--dereference:跟随符号链接,显示符号链接所指向的实际文件的大小。
  • --exclude=PATTERN:排除匹配模式PATTERN的文件或目录。
  • -B--block-size=SIZE:以指定的块大小显示每个文件的磁盘使用量。
  • --time:显示每个文件或目录的最后修改时间。
  • --time=WORD--time-style=STYLE:显示时间,可以定义时间显示方式,例如full-isolong-isoiso等。

查看当前目录下一级子文件和子目录占用的磁盘容量。

1
du -ah --max-depth=1 /etc

可以直接用-d来限定层级(同--max-depth

1
du -ah -d 1 /etc

查看当前目录总共占的容量

1
du -sh

查看大文件(这里使用find -size +n更合适)

1
du -am | awk -F ' ' '{if($1 > 500) print $1 " ~ " $2}'

列出文件大小前5的目录

1
2
3
du --max-depth=1  | sort -nr | head -5

du -d 1 . | sort -rn | head -5

各个显示文件大小单位的参数

1
2
3
4
-B:--block-size=SIZE,指定单位大小。
-b:--bytes,以字节为单
-k:和--block-size=1k类似,以KB为单位
-m:和--block-size=1m类似,以MB为单位

uname

uname命令用于显示系统信息

params

1
2
3
4
5
6
7
8
-a或--all  显示全部的信息。
-m或--machine  显示电脑类型。
-n或-nodename  显示在网络上的主机名称。
-r或--release  显示操作系统的发行编号。
-s或--sysname  显示操作系统名称。
-v  显示操作系统的版本。
--help  显示帮助。
--version  显示版本信息。

exp

1
2
$uname -a
Linux wisp011010179090.pre.na62 4.9.79-009.ali3000.ecsvm.alios7.x86_64 #1 SMP Mon Mar 26 10:38:04 CST 2018 x86_64 x86_64 x86_64 GNU/Linux

sort

params

1
2
3
4
5
6
7
-u  输出行中去重
-r 倒序排列
-o 输出重定向
-n 以数字排序
-t/-k -t指定分隔符,-k选中列数(1开始)
-f 忽略大小写
-b 从第一个可见字符开始

exp
文件大小排序

1
2
3
du --max-depth=2 |sort  -rnk 1 -t' '  

du --max-depth=2 |sort -nr

find

find path -option [ -print ] [ -exec -ok command ] {} \;

params

1
2
3
4
5
6
7
8
9
10
-type 
文件类型(f 普通文件,l 符号连接,d 目录,c 字符设备,b 块设备,s 套接字,p Fifo)
-size
文件大小(b—bit,c—byte,w—word,k-kb,M-m,G-g), + 号代表大于,-代表小于,无符号代表等于 比如 +800M
-name
按文件名称
-print
问查找到的文件输出,默认
-exec
匹配的文件执行shell命令,格式 'command' { } \;,注意{ }和\;之间的空格

比如查找文件

1
find ~/ -name fuck.log*

查找大于300M的文件,并输出详细路径

1
find ~/ -type f -size +300M -exec ls -lh {} \;

nohup

1
nohup ./test > myout.txt 2>&1 &

& 让程序后台允许
nohup 退出终端不挂起
2>&1 错误重定向到标准输出

netstat

查看端口占用

1
netstat -anp | grep 8080。

ps. 讲一下 lsof:
lsof(list open files)是一个列出当前系统打开文件的工具。
lsof 查看端口占用语法格式: lsof -i:端口号