目录
查找文件 find
find 查找范围 -name “文件名”
例如:find /etc/ -name “passwd”
find 查找范围 -iname “文件名” 忽略大小写查找文件
注意:“”中的关键字是精确匹配,可用*实现模糊查询
find 查找范围 -size 文件大小的范围 根据文件大小查找(+ 大于,- 小于)
例如:查找大于100M的文件 find / -size +100M
例如:查找大于100M并小于200M的文件 find / -size +100M -size -200M
例如:查找大于100M并小于300M,且名字中含有rpm的文件
find / -size +100M -size -300M -iname “*rpm*”
查找范围大于100M小于200M的文件
[root@localhost ~]# find / -size +100M -size -200M
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/var/lib/rpm/Packages
查找范围大于100M小于300M且名字中含有rpm不区分大小写的文件
[root@localhost ~]# find / -size +100M -size -300M -iname "*rpm*"
查找种类类型为设备文件的文件
[root@localhost ~]# find / -type b
/dev/dm-1
/dev/dm-0
/dev/sr1
/dev/sr0
/dev/nvme0n1p2
/dev/nvme0n1p1
/dev/nvme0n1
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 876M 0 876M 0% /dev
tmpfs 896M 0 896M 0% /dev/shm
tmpfs 896M 18M 878M 2% /run
tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 4.7G 13G 28% /
/dev/nvme0n1p1 1014M 204M 811M 21% /boot
tmpfs 180M 5.7M 174M 4% /run/user/1001
find 查找范围 -type 类型 根据文件类型查找
F:普通文件 d:目录 b:设备文件 c:字符文件
例如:查找设备文件 find / -type b
find 查找范围 -user 用户名 根据文件的所有者查找
例如:find / -user test
find 查找范围 -选项 -exec 命令 \;
-exec参数后面跟的是命令,命令的终止是以\;作为命令的结束符。{} 代表前面find查找到的文件。
例如:查找/etc目录下,passwd文件,然后复制到当前用户家目录下。
find /etc -name “passwd” -exec cp {} ~ \;
例如:查找当前用户家目录下的passwd文件,并删除。
find /root -name "passwd" -exec rm -rf {} \;
例如:查找文件所有者为普通用户test的文件,并复制保存到/root/student目录中
find / -user test -exec cp {} /root/student \;
练习: 查找/etc目录下,大于1M的文件,并复制到/tmp下
//查找/etc目录下passwd文件,然后复制到当前用户家目录下
[root@localhost home]# cd ~
[root@localhost ~]# find /etc -iname "passwd"
/etc/pam.d/passwd
/etc/passwd
[root@localhost ~]# find /etc -iname "passwd" -exec cp {} /root \;
[root@localhost ~]# ll
总用量 32
-rw-r--r--. 1 root root 2631 3月 3 10:32 passwd
[root@localhost ~]# find /root -iname "passwd"
/root/passwd
//查找当前用户家目录下的passwd文件并删除
[root@localhost ~]# find /root -iname "passwd" -exec rm -rf {} \;
[root@localhost ~]# ll
总用量 28
-rw-r--r--. 1 root root 110 3月 1 11:23 123
-rw-r--r--. 1 root root 1525 3月 1 11:41 1.txt
-rw-r--r--. 1 root root 21 3月 1 11:30 6.txt
drwxr-xr-x. 3 root root 56 3月 1 10:49 A
-rw-------. 1 root root 2793 2月 24 12:13 anaconda-ks.cfg
drwxr-xr-x. 3 root root 15 3月 1 10:53 C
drwxr-xr-x. 2 root root 6 3月 1 11:17 E
-rw-r--r--. 1 root root 105 3月 1 11:27 e.gzip
drwxr-xr-x. 3 root root 15 3月 1 11:24 F
-rw-r--r--. 1 root root 131 3月 1 11:26 f.bz2
-rw-------. 1 root root 2057 2月 24 12:13 original-ks.cfg
//查找文件所有者为普通用户mika的文件,并复制保存到/root/student目录下
//复制完后所有者会变成root
[root@localhost ~]# find / -user mika -exec cp -r {} /root/student \;
//所有者是mika 就是我自己 o(* ̄︶ ̄*)o
[root@localhost test]# cd /home/mika
[root@localhost mika]# ll
总用量 0
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Desktop
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Documents
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Downloads
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Music
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Pictures
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Public
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Templates
drwxr-xr-x. 2 mika mika 6 2月 24 14:00 Videos
[root@localhost mika]# cd /
[root@localhost /]# ll
总用量 28
lrwxrwxrwx. 1 root root 7 4月 23 2020 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 2月 24 12:14 boot
drwxr-xr-x. 20 root root 3260 2月 24 12:14 dev
drwxr-xr-x. 150 root root 8192 3月 1 10:20 etc
drwxr-xr-x. 4 root root 30 2月 24 12:13 home
lrwxrwxrwx. 1 root root 7 4月 23 2020 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 4月 23 2020 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 4月 23 2020 media
drwxr-xr-x. 3 root root 18 2月 24 12:05 mnt
drwxr-xr-x. 2 root root 6 4月 23 2020 opt
dr-xr-xr-x. 337 root root 0 2月 24 12:14 proc
dr-xr-x---. 9 root root 4096 3月 3 10:44 root
drwxr-xr-x. 43 root root 1260 3月 3 10:17 run
lrwxrwxrwx. 1 root root 8 4月 23 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 4月 23 2020 srv
dr-xr-xr-x. 13 root root 0 2月 24 12:14 sys
drwxrwxrwt. 18 root root 4096 3月 3 11:06 tmp
drwxr-xr-x. 12 root root 144 2月 24 12:02 usr
drwxr-xr-x. 22 root root 4096 2月 24 12:14 var
[root@localhost /]# find /etc -size +1M
/etc/udev/hwdb.bin
/etc/selinux/targeted/policy/policy.31
[root@localhost /]# find /etc -size +1M -exec cp -r {} /tmp \;
[root@localhost /]# ll /tmp
总用量 17612
drwxr-xr-x. 2 root root 18 2月 24 12:06 hsperfdata_root
-r--r--r--. 1 root root 9389332 3月 3 11:08 hwdb.bin
-rwx------. 1 root root 1230 2月 24 12:13 ks-script-9zrz77z7
-rwx------. 1 root root 291 2月 24 12:13 ks-script-e_2_y_tv
-rwx------. 1 root root 701 2月 24 12:13 ks-script-g4a4itsr
-rw-r--r--. 1 root root 8628473 3月 3 11:08 policy.31
drwx------. 3 root root 17 2月 24 12:14 systemd-private-2e9f762d835b4351881cba92d3a663e6-bluetooth.service-nUK3Oi
drwx------. 3 root root 17 2月 24 12:14 systemd-private-2e9f762d835b4351881cba92d3a663e6-chronyd.service-vF4p7h
drwx------. 3 root root 17 2月 24 12:14 systemd-private-2e9f762d835b4351881cba92d3a663e6-colord.service-0kQvMi
drwx------. 3 root root 17 2月 24 12:14 systemd-private-2e9f762d835b4351881cba92d3a663e6-ModemManager.service-Ruosef
drwx------. 3 root root 17 2月 24 12:14 systemd-private-2e9f762d835b4351881cba92d3a663e6-rtkit-daemon.service-OEPtLh
drwx------. 2 mika mika 6 2月 24 14:01 tracker-extract-files.1001
drwx------. 2 root root 6 2月 24 12:14 vmware-root_1187-4022308825
文本过滤 grep
(如果找的是文件的路径,就是find;如果从一堆文本中过滤出需要的内容就是grep)
grep 【选项】“关键字” 要过滤的文本路径
例: 从/etc/passwd文件中,过滤出含有root字段的行
-i 忽略大小写
-n 显示行号
-v 反选(关键字以外内容)
//从/etc/passwd 文件中过滤出含有root字段的行
[root@localhost /]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
//过滤含有Root的行
[root@localhost /]# grep "Root" /etc/passwd
//忽略大小写
[root@localhost /]# grep -i "Root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
//判断是在文件的第几行
[root@localhost /]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
//反选(关键字以外的所有行)
[root@localhost /]# grep -v "root" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
gluster:x:996:992:GlusterFS daemons:/run/gluster:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pipewire:x:995:991:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
libstoragemgmt:x:994:988:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
clevis:x:993:987:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:992:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
setroubleshoot:x:991:984::/var/lib/setroubleshoot:/sbin/nologin
saslauth:x:990:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dnsmasq:x:982:982:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sssd:x:981:981:User for sssd:/:/sbin/nologin
cockpit-ws:x:980:979:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:979:978:User for cockpit-ws instances:/nonexisting:/sbin/nologin
chrony:x:978:977::/var/lib/chrony:/sbin/nologin
flatpak:x:977:976:User for flatpak system helper:/:/sbin/nologin
colord:x:976:975:User for colord:/var/lib/colord:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:975:974::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
test:x:1000:1000:test:/home/test:/bin/bash
mika:x:1001:1001:mika:/home/mika:/bin/bash
管道符 |
将前一个命令的正确输出,作为后一个命令的输入参数用(后一个命令通常是文件处理命令)
例: cat /etc/passwd | grep “root”
grep “root” /etc/passwd
例: ping 127.0.0.1 | grep “time=0.03”
利用管道符可以实现多重过滤
grep “bash” /etc/passwd | grep “root”
//查找含有root的行
[root@localhost /]# cat /etc/passwd | grep "root"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
//查找符合条件的行
[root@localhost /]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.056 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.126 ms
--- 127.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3080ms
rtt min/avg/max/mdev = 0.049/0.088/0.126/0.037 ms
[root@localhost /]# ping 127.0.0.1 | grep "time=0.01*"
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.055 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.043 ms
//多重过滤 在第一次结果的基础上再次筛选
[root@localhost /]# grep "bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
test:x:1000:1000:test:/home/test:/bin/bash
mika:x:1001:1001:mika:/home/mika:/bin/bash
[root@localhost /]# grep "bash" /etc/passwd |grep "root"
root:x:0:0:root:/root:/bin/bash
[root@localhost /]#
重定向符
输入重定向:将原本从键盘输入的内容,改为从文本或者脚本中读取,输入重定向的符号为”<”
输出重定向:将原本输出到终端上的内容,重新定向到其他地方(例如本地磁盘文件),输出重定向的符号为”>”或”>>”
>: 添加新内容,会覆盖源文件内容
例: echo “hello” >1.txt
>>: 新内容会添加到文件末尾
例:重定向到另一个终端
echo “hello” > /dev/pts/1(另一个终端地址:tty)g
echo “hello” 1> /dev/pts/1 执行正确后重定向
ech “hello” 2> /dev/pts/1 执行不正确后重定向
echo “hello” &> /dev/pts/1 执行正确/不正确后重定向
练习:查找/etc目录下含有conf的文件名,将文件名保存到当前用户家目录下的1.txt中
ls /etc | grep “conf” > /root/ 1.txt
//直接输出hello
[root@localhost /]# echo "hello"
hello
//覆盖1.txt中的内容为hello
[root@localhost /]# echo "hello" >1.txt
[root@localhost /]# cat 1.txt
hello
[root@localhost /]# echo "mika!" >1.txt
[root@localhost /]# cat 1.txt
mika!
//写入内容不覆盖
[root@localhost /]# echo "suki!" >>1.txt
[root@localhost /]# cat 1.txt
mika!
suki!
//略(各种意义)
[root@localhost /]# echo "hello" >/dev/pts/1
bash: /dev/pts/1: 权限不够
[root@localhost /]# cd ~
[root@localhost ~]# echo "hello" >/dev/pts/1
bash: /dev/pts/1: 权限不够
[root@localhost ~]# echo "hello" 2>/dev/pts/1
bash: /dev/pts/1: 权限不够
[root@localhost ~]# echo "hello" &>/dev/pts/1
bash: /dev/pts/1: 权限不够
[root@localhost ~]# ls /etc | grep "conf"
asound.conf
autofs.conf
...(略去)
updatedb.conf
vconsole.conf
xattr.conf
yum.conf
[root@localhost ~]# pwd
/root
[root@localhost ~]# cat 1.txt
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=22 ttl=64 time=0.055 ms
64 bytes from 127.0.0.1: icmp_seq=23 ttl=64 time=0.047 ms
--- 127.0.0.1 ping statistics ---
23 packets transmitted, 23 received, 0% packet loss, time 22549ms
rtt min/avg/max/mdev = 0.041/0.061/0.124/0.021 ms
[root@localhost ~]# ls /etc|grep "conf" >1.txt
[root@localhost ~]# cat 1.txt
asound.conf
autofs.conf
...(略去)
vconsole.conf
xattr.conf
yum.conf
[root@localhost ~]#
vim文本编辑器
我是みか,祝你开心。
- 一稿:2022/03/03
- 二稿:2022/06/14