1. 管道符的使用 |
利用Linux提供的管道符 | 将两个命令隔开,管道符左边命令的输出 就会作为管道符右边命令的输入。连续使用管道意味着第一个命令的输出会作为第二个命令的输入,第二个命令的输出又会作为第三个命令的输入,以此类推。
[root@linux-66283 ~]# ll /usr | grep lib
dr-xr-xr-x 1 root root 275 Jul 9 2022 lib
dr-xr-xr-x 1 root root 12288 Jul 9 2022 lib64
drwxr-xr-x 1 root root 147 Jul 9 2022 libexec
[root@linux-66283 ~]# ll /usr | awk '{print $9}'
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
[root@linux-66283 ~]# ll /usr|awk '{print $9}'|grep share
share
2. 输出重定向
对于任何一条linux命令执行,会有这样一个过程:
如果想把命令的结果输出到文件,则需要输出重定向符号:
> 输出重定向到文件,覆盖写入文件
>> 输出重定向到文件,追加写入文件
三种输入输出的代码分别是:
① 标准输入(stdin);代码为0;
② 标准输出(stdout);代码为1,不写默认标准输出
③ 错误输出(stderr);代码为2;
# 查看usr目录和roo目录并将结果覆盖写入到demo.log(默认是标准输出写入到文件)
[root@linux-66283 ~]# ls /usr /roo > demo.log
ls: cannot access /roo: No such file or directory
[root@linux-66283 ~]# cat demo.log
/usr:
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
# 标准错误输出重定向到demo.log文件中
[root@linux-66283 ~]# ls /usr /roo 2> demo.log
/usr:
bin etc games include lib lib64 libexec local sbin share src tmp
[root@linux-66283 ~]# cat demo.log
ls: cannot access /roo: No such file or directory
#标准错误输出重定向追加到demo.log文件中
[root@linux-66283 ~]# ls /usr /roo 2>> demo.log
/usr:
bin etc games include lib lib64 libexec local sbin share src tmp
[root@linux-66283 ~]# cat demo.log
ls: cannot access /roo: No such file or directory
ls: cannot access /roo: No such file or directory
# 标准输出和标准错误输出全部覆盖写入到demo.log中
[root@linux-66283 ~]# ls /usr /roo >demo.log 2>&1
[root@linux-66283 ~]# cat demo.log
ls: cannot access /roo: No such file or directory
/usr:
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
# 标准输出和标准错误输出全部追加写入到demo.log中
[root@linux-66283 ~]# ls /usr /roo >>demo.log 2>&1
[root@linux-66283 ~]# cat demo.log
ls: cannot access /roo: No such file or directory
/usr:
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
ls: cannot access /roo: No such file or directory
/usr:
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
# /dev/null是无底洞(⊙_⊙;),数据放里面就消失了
[root@linux-66283 ~]# ls /usr /roo >>/dev/null 2>&1
[root@linux-66283 ~]# cat /dev/null
3. 输入重定向
cat命令用于连接文件或标准输入并打印。这个命令常用来显示文件内容,从标准输入读取内容并显示,常与重定向符号配合使用。
常用使用方式:
① cat << EOF,以EOF输入字符串为标准输入结束;
② cat > filename,创建新文件,并把标准输入输出到filename文件中,以ctrl+d作为输入结束
③ cat > filename << EOF,以EOF作为输入结束,其中EOF可以是任何字符串,和ctrl+d作用一样
[root@linux-66283 ~]# cat << EOF
> nihao hello
> hello linux
> EOF
nihao hello
hello linux
# 写入到test.log
[root@linux-66283 ~]# cat > test.log << yx
> 123
> yae
> yx
[root@linux-66283 ~]# cat test.log
123
yae
# 切换到目标用户后执行相应命令并退出,一般用在shell编程中
su - tom << aaa
mkdir www
touch wwwf
aaa
4. find 命令 查找文件或目录
在目录中递归查找文件或目录
命令: find [目录] -name 文件或目录名
# 在根路径下按 名字 查找usr
[root@linux-66283 ~]# find / -name usr
/usr
/usr/share/gdb/auto-load/usr
/usr/lib/debug/usr
# 在opt目录下查找 类型 为普通文件的
[root@linux-66283 ~]# find /opt -type f
/opt/.prometheus/init_service
/opt/.prometheus/init_host
/opt/.prometheus/init_prometheus
# 在opt目录下查找 类型 为目录的
[root@linux-66283 ~]# find /opt -type d
/opt
/opt/.prometheus
# 在opt目录下按照 文件所属用户 查找root
[root@linux-66283 ~]# find /opt -user root
/opt
/opt/.prometheus
/opt/.prometheus/init_service
/opt/.prometheus/init_host
/opt/.prometheus/init_prometheus
# 链接目录找不到
[root@linux-66283 /]# ll
total 0
lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x 2 root root 10 May 29 2023 data
drwxr-xr-x 5 root root 360 Dec 6 21:32 dev
drwxr-xr-x 1 root root 26 Dec 6 21:32 etc
drwxr-xr-x 1 root root 27 Nov 2 10:34 home
lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x 2 root root 10 Apr 11 2018 media
drwxr-xr-x 2 root root 10 Apr 11 2018 mnt
drwxr-xr-x 3 root root 33 Sep 18 2022 opt
dr-xr-xr-x 1079 root root 0 Dec 6 21:32 proc
drwxr-xr-x 6 root root 80 Nov 17 2022 public
dr-xr-x--- 1 root root 66 Dec 6 22:12 root
drwxr-xr-x 1 root root 67 May 29 2023 run
lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 10 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Oct 14 09:14 sys
drwxrwxrwt 1 root root 82 Dec 6 22:27 tmp
drwxr-xr-x 1 root root 25 Nov 13 2020 usr
drwxr-xr-x 1 root root 42 Nov 13 2020 var
[root@linux-66283 /]# find /bin -name zmore
[root@linux-66283 /]#
注意:find要查找的目录不能是链接。