常用linux命令

描述

重定向

标准输入stdin: 代码为0, 使用< 或<<
标准输出stdout: 代码为1, 使用>或>>
标准错误输出stderr: 代码为2, 使用2>或2>>
特殊写法:将stdout和stderr同时写入一个文件,使用2>&1

 

# 将ll的结果重定向到out.txt文件中,如果文件中有内容则覆盖
ll /home > out.txt 

# 将ll的结果追加到out.txt文件中
ll /etc >> out.txt 

# stdout和stderr写入同一个文件
find /home -name .bashrc > out.txt 2>&1 # 注意2>&1写在最后
find /home -name .bashrc &> out.txt       # 或者使用&>

 

管道

使用command A | command B | command C命令,将A命令产生的标准输出作为B命令的标准输入(注意只能接收前一个命令的标准输出)。
每个管道后必须接指令,且指令必须可以接收stdin才可以。如less, more, head, tail 都可以,ls, cp, mv 则不行。
如果要接收前一个命令的stdout,则需要使用2>&1将stdout转换为stdin。

tee命令

tee [OPTION]... [FILE]...
将stdin读取,写入stdout和file。
结合上面的管道,:

 

# 将ll结果同时显示在屏幕和记录到文件中
ll /home | tee list_home.out

# 将find结果(正常和错误)同时显示在屏幕和记录到文件中
find /home -name .bashrc 2>&1 | tee find.out

 

xargs命令

xargs [options] [command [initial-arguments]]
xargs读取stdin,以空格或换行作为分隔符,将stdin分割为参数。

 

# 将find的结果作为参数,传给ls -lh命令
find /usr/sbin -perm /7000 | xargs ls -lh

# 将find结果作为参数,传给du命令
find /home -name "*.go" | xargs du -cb

 

文本处理 - vim, grep, awk, sed, sort, wc, uniq, cut, tr

grep

grep [OPTION...] PATTERNS [FILE...]
从文本中查找符合某个模式的文本。

 

# 查找list.out中包含rvs字符的行
[leadcom@localhost test]$ grep rvs  list.out 
drwx------   4 rvs     rvs          127 12月 16 18:41 rvs
drwxrwxrwx  16 root    root         285 8月   4 10:03 rvslocal
drwxrwxrwx   2 root    root           6 5月  10 2021 rvsremote

# 结合管道查找前一个命令中包含某个字符的行
ps -ef | grep postgres

 

cut

cut OPTION... [FILE]...
根据option将文件中的每行做处理,输出到到标准输出。
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
如果不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。

 

# 以:为分割符,取第一个元素
gw1@gw1-PC:~$ echo $PATH
/home/gw1/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin
gw1@gw1-PC:~$ echo $PATH | cut -d ":" -f 1
/home/gw1/.local/bin

gw1@gw1-PC:~$ export 
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
declare -x DISPLAY="localhost:10.0"
declare -x HOME="/home/gw1"
declare -x LANG="zh_CN.UTF-8"
declare -x LANGUAGE="zh_CN"
declare -x LOGNAME="gw1"
...

# 只取export每行的declare -x之后内容,即第12个字符后内容
gw1@gw1-PC:~$ export | cut -c 12-
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
DISPLAY="localhost:10.0"
HOME="/home/gw1"
LANG="zh_CN.UTF-8"
LANGUAGE="zh_CN"
LOGNAME="gw1"
...

 

awk

 

gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
gawk [ POSIX or GNU style options ] [ -- ] program-text file ...

 

用法一

 

awk '{[pattern] action}' {filenames}   # 行匹配语句 awk '' 只能用单引号
# 每行按空格或TAB分割,输出文本中的1、4项
[leadcom@localhost test]$ cat log.txt
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo

[leadcom@localhost test]$ awk '{print $1,$4}' log.txt
2 a
3 like
This's 
10 orange,apple,mongo

 

用法二 awk -F #-F相当于内置变量FS, 指定分割字符

 

[leadcom@localhost test]$ awk -F, '{print $1,$4}' log.txt
2 this is a test 
3 Are you like awk 
This's a test 
10 There are orange 

 

sed

sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed [-hnV][-e

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分