常用linux命令
文件管理 - cd, pwd, mkdir, rmdir, ls, cp, rm, mv, tar
文件检索 - cat, more, less, head, tail, file, find
输入输出控制 - 重定向, 管道, tee, xargs
重定向
标准输入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<script>][-f<script文件>][文本文件]
Linux sed 命令是利用脚本来处理文本文件。
sed 可依照脚本的指令来处理、编辑文本文件。
Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。
参数说明:
-e<sript>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
-f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
-n或--quiet或--silent 仅显示script处理后的结果。
动作说明:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
[leadcom@localhost test]$ cat testfile
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
# 标准输出中在第四行后增加一行
[leadcom@localhost test]$ sed -e 4a"\naaaa" testfile
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
naaaa
# 标准输出中删除2到5行
[leadcom@localhost test]$ nl testfile | sed '2,5d'
1 HELLO LINUX!
sort
sort [OPTION]... [FILE]...
Linux sort 命令用于将文本文件内容加以排序。
sort 可针对文本文件的内容,以行为单位来排序。
gw1@gw1-PC:~$ cat testfile
test 30
Hello 95
Linux 85
gw1@gw1-PC:~$ sort testfile
Hello 95
Linux 85
test 30
wc
wc [OPTION]... [FILE]...
Linux wc命令用于计算字数。
利用wc指令我们可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。
[leadcom@localhost test]$ ps -ef | grep postgres | wc -l
16
uniq
uniq [OPTION]... [INPUT [OUTPUT]]
Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。
uniq 可检查文本文件中重复出现的行列。
gw1@gw1-PC:~$ cat testfile
test 30
test 30
test 30
Hello 95
Hello 95
Hello 95
Hello 95
Linux 85
Linux 85
gw1@gw1-PC:~$ uniq testfile
test 30
Hello 95
Linux 85
Linux 85
tr
tr [OPTION]... SET1 [SET2]
Linux tr 命令用于转换或删除文件中的字符。
tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。
gw1@gw1-PC:~$ cat testfile
It uses a mix of theory and practicl techniques to
teach administrators how to install and
use security applications, as well as how the
applcations work and why they are necesary.
# 将小写转换为大写
gw1@gw1-PC:~$ cat testfile | tr a-z A-Z
IT USES A MIX OF THEORY AND PRACTICL TECHNIQUES TO
TEACH ADMINISTRATORS HOW TO INSTALL AND
USE SECURITY APPLICATIONS, AS WELL AS HOW THE
APPLCATIONS WORK AND WHY THEY ARE NECESARY.
gw1@gw1-PC:~$
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫