重定向
标准输入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 12712月 1618:41 rvsdrwxrwxrwx 16 root root 2858月 410:03 rvslocaldrwxrwxrwx 2 root root 65月 102021 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/sbingw1@gw1-PC:~$ echo$PATH | cut -d ":" -f 1/home/gw1/.local/bingw1@gw1-PC:~$ exportdeclare -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.txt2 this is a test3 Are you like awkThis's a test10 There are orange,apple,mongo[leadcom@localhost test]$ awk '{print$1,$4}' log.txt2 a3 likeThis's 10 orange,apple,mongo
用法二
awk -F #-F相当于内置变量FS, 指定分割字符
[leadcom@localhost test]$ awk -F, '{print $1,$4}' log.txt2 this is a test 3Are 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<script>][-f<script文件>][文本文件]
Linux sed 命令是利用脚本来处理文本文件。sed 可依照脚本的指令来处理、编辑文本文件。Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。
# 将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 # 或者使用&>
0
# 将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 # 或者使用&>
1
sort
sort [OPTION]... [FILE]...
Linux sort 命令用于将文本文件内容加以排序。sort 可针对文本文件的内容,以行为单位来排序。
# 将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 # 或者使用&>
2
wc
wc [OPTION]... [FILE]...
Linux wc命令用于计算字数。利用wc指令我们可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。
# 将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 # 或者使用&>
3
uniq
uniq [OPTION]... [INPUT [OUTPUT]]
Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。uniq 可检查文本文件中重复出现的行列。
# 将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 # 或者使用&>
4
tr
tr [OPTION]... SET1 [SET2]
Linux tr 命令用于转换或删除文件中的字符。tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。
# 将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 # 或者使用&>
5
(版权归原作者所有,侵删)
免责声明:本文内容来源于网络,所载内容仅供参考。转载仅为学习和交流之目的,如无意中侵犯您的合法权益,请及时联系Docker中文社区!
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……
还没有评论,来说两句吧...