倪的个人博客

  • 首页
  • Linux运维
  1. 首页
  2. Linux运维
  3. 正文

IO重定向进程管道

2024年3月16日 430点热度 1人点赞 0条评论

IO重定向进程管道

强引用弱引用

双引号是弱引用

单引号是强引用

\表转义符

[root@localhost ~]# x=1
[root@localhost ~]# y=2
[root@localhost ~]# echo "$x + $y"
1 + 2
[root@localhost ~]# echo "$x+$y"  
1+2
[root@localhost ~]# echo '$x+$y'
$x+$y
[root@localhost ~]# 
[root@localhost ~]# echo "\$x+\$y"
$x+$y
[root@localhost ~]#

标准输入与输出过程

[root@localhost ~]# tty
/dev/pts/32
[root@localhost ~]# tail
11^C
[root@localhost ~]# tail

通过进程id可以再proc目录下看到进程调用得文件 exe -> /usr/bin/tail

[root@localhost ~]# tty
/dev/pts/33
[root@localhost ~]# ps -ef|grep tail
root      22591  22444  0 01:01 pts/32   00:00:00 tail
root      22594  22495  0 01:02 pts/33   00:00:00 grep --color=auto tail
[root@localhost ~]# ll /proc/22591/
total 0
dr-xr-xr-x 2 root root 0 Nov 14 01:02 attr
-rw-r--r-- 1 root root 0 Nov 14 01:02 autogroup
-r-------- 1 root root 0 Nov 14 01:02 auxv
-r--r--r-- 1 root root 0 Nov 14 01:02 cgroup
--w------- 1 root root 0 Nov 14 01:02 clear_refs
-r--r--r-- 1 root root 0 Nov 14 01:02 cmdline
-rw-r--r-- 1 root root 0 Nov 14 01:02 comm
-rw-r--r-- 1 root root 0 Nov 14 01:02 coredump_filter
-r--r--r-- 1 root root 0 Nov 14 01:02 cpuset
lrwxrwxrwx 1 root root 0 Nov 14 01:02 cwd -> /root
-r-------- 1 root root 0 Nov 14 01:02 environ
lrwxrwxrwx 1 root root 0 Nov 14 01:02 exe -> /usr/bin/tail
dr-x------ 2 root root 0 Nov 14 01:02 fd
dr-x------ 2 root root 0 Nov 14 01:02 fdinfo
-rw-r--r-- 1 root root 0 Nov 14 01:02 gid_map
-r-------- 1 root root 0 Nov 14 01:02 io
-r--r--r-- 1 root root 0 Nov 14 01:02 limits
-rw-r--r-- 1 root root 0 Nov 14 01:02 loginuid
dr-x------ 2 root root 0 Nov 14 01:02 map_files
-r--r--r-- 1 root root 0 Nov 14 01:02 maps
-rw------- 1 root root 0 Nov 14 01:02 mem
-r--r--r-- 1 root root 0 Nov 14 01:02 mountinfo
-r--r--r-- 1 root root 0 Nov 14 01:02 mounts
-r-------- 1 root root 0 Nov 14 01:02 mountstats
dr-xr-xr-x 6 root root 0 Nov 14 01:02 net
dr-x--x--x 2 root root 0 Nov 14 01:02 ns
-r--r--r-- 1 root root 0 Nov 14 01:02 numa_maps
-rw-r--r-- 1 root root 0 Nov 14 01:02 oom_adj
-r--r--r-- 1 root root 0 Nov 14 01:02 oom_score
-rw-r--r-- 1 root root 0 Nov 14 01:02 oom_score_adj
-r--r--r-- 1 root root 0 Nov 14 01:02 pagemap
-r-------- 1 root root 0 Nov 14 01:02 patch_state
-r--r--r-- 1 root root 0 Nov 14 01:02 personality
-rw-r--r-- 1 root root 0 Nov 14 01:02 projid_map
lrwxrwxrwx 1 root root 0 Nov 14 01:02 root -> /
-rw-r--r-- 1 root root 0 Nov 14 01:02 sched
-r--r--r-- 1 root root 0 Nov 14 01:02 schedstat
-r--r--r-- 1 root root 0 Nov 14 01:02 sessionid
-rw-r--r-- 1 root root 0 Nov 14 01:02 setgroups
-r--r--r-- 1 root root 0 Nov 14 01:02 smaps
-r--r--r-- 1 root root 0 Nov 14 01:02 stack
-r--r--r-- 1 root root 0 Nov 14 01:02 stat
-r--r--r-- 1 root root 0 Nov 14 01:02 statm
-r--r--r-- 1 root root 0 Nov 14 01:02 status
-r--r--r-- 1 root root 0 Nov 14 01:02 syscall
dr-xr-xr-x 3 root root 0 Nov 14 01:02 task
-r--r--r-- 1 root root 0 Nov 14 01:02 timers
-rw-r--r-- 1 root root 0 Nov 14 01:02 uid_map
-r--r--r-- 1 root root 0 Nov 14 01:02 wchan
[root@localhost ~]# ll /proc/22591/fd
total 0
lrwx------ 1 root root 64 Nov 14 01:02 0 -> /dev/pts/32
lrwx------ 1 root root 64 Nov 14 01:02 1 -> /dev/pts/32
lrwx------ 1 root root 64 Nov 14 01:02 2 -> /dev/pts/32
[root@localhost ~]# 

通过fd目录下三个文件,可以看到标准输入、输出对应到哪个终端。

[root@localhost ~]# tail
^C
[root@localhost ~]# 

结束tail,可以看到打不开了

[root@localhost ~]# ll /proc/22591/fd
ls: cannot access /proc/22591/fd: No such file or directory
[root@localhost ~]#

做个测试

[root@localhost ~]# tty
/dev/pts/32
#开了两个tty
[root@localhost ~]# tty
/dev/pts/33
[root@localhost ~]# 

在32上面打内容,输出到33

[root@localhost ~]# tty
/dev/pts/33
[root@localhost ~]# echo "你好" > /dev/pts/3
3   30  31  32  33  
[root@localhost ~]# echo "你好" > /dev/pts/33
#33上面就出现了
[root@localhost ~]# tty
/dev/pts/33
[root@localhost ~]# 你好

这儿需要优化

image-20221114182010898

输出重定向案例

1> = > 1>> = >>

[root@localhost tmp]# echo "123456" > aaa.txt
[root@localhost tmp]# cat aaa.txt 
123456
[root@localhost tmp]# echo "654987" 1> aaa.txt      
[root@localhost tmp]# cat aaa.txt             
654987
[root@localhost tmp]# echo "123" >> aaa.txt
[root@localhost tmp]# echo "456" >> aaa.txt   
[root@localhost tmp]# cat aaa.txt          
654987
123
456
[root@localhost tmp]# echo "789" 1>> aaa.txt    
[root@localhost tmp]# cat aaa.txt           
654987
123
456
789
[root@localhost tmp]# 
[root@localhost tmp]# lls "abc" 2> bbb.txt
[root@localhost tmp]# cat bbb.txt 
bash: lls: command not found...
Similar command is: 'ls'
[root@localhost tmp]# ggg "sss" 2>> bbb.txt     
[root@localhost tmp]# cat bbb.txt          
bash: lls: command not found...
Similar command is: 'ls'
bash: ggg: command not found...
[root@localhost tmp]# 

案例1-标准输出重定向

image-20221114184520140

[root@localhost tmp]# > edu.txt
[root@localhost tmp]# ll edu.txt 
-rw-r--r-- 1 root root 0 Nov 14 02:45 edu.txt
[root@localhost tmp]# cat edu.txt 
[root@localhost tmp]# ifconfig > edu.txt 
[root@localhost tmp]# cat edu.txt        
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.50  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::20c:29ff:fe8e:fb6b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8e:fb:6b  txqueuelen 1000  (Ethernet)
        RX packets 1397491  bytes 349226050 (333.0 MiB)
        RX errors 0  dropped 3  overruns 0  frame 0
        TX packets 137962  bytes 11727670 (11.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 50  bytes 4400 (4.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 4400 (4.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:4a:d8:20  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost tmp]# 

案例2-标准追加输出重定向

image-20221114184910795

[root@localhost tmp]# echo "你好,深圳" >> lao6
[root@localhost tmp]# echo "下午好" >> lao6           
[root@localhost tmp]# cat lao6 
你好,深圳
下午好
[root@localhost tmp]#

案例3-错误输出重定向

image-20221114185115540

[bob@localhost ~]$ find /etc -name "*.conf" 1>ok 2>ok
[bob@localhost ~]$ 
[bob@localhost ~]$ 
[bob@localhost ~]$ cat ok 
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/dhcp’: Permission denied
......
find: ‘/etc/cups/ssl’: Permission denied
/etc/fonts/conf.d/50-user.conf
/etc/fonts/conf.d/51-local.conf
/etc/fonts/conf.d/60-latin.conf

案例4-混合和输出重定向

image-20221119160513373

[bob@localhost ~]$ find /etc &>> etc_ww.txt                 
[bob@localhost ~]$ ll
total 136
-rw-r--r-- 1 bob dev 58833 Nov 19 00:17 etc_qq.txt
-rw-r--r-- 1 bob dev 58833 Nov 19 00:18 etc_ww.txt
-rw-r--r-- 1 bob dev 15220 Nov 14 02:53 ok
[bob@localhost ~]$ find /etc 2>> etc_qq.txt 1>> etc_qq.txt

上面这两种写法都可以,&>>正确输出和错误输出一同文件

image-20221119171823649

[root@localhost ~]# ls /root /error >ab 2>&1
[root@localhost ~]# cat ab 
ls: cannot access /error: No such file or directory
/root:
20220409
ab
anaconda-ks.cfg
cpptools-linux.vsix
create.sql.gz
dbdump_20220218.db
dbdump_20220301.db
dbdump_20220303_2.db
dbdump_20220303_3.db
dbdump_20220303.db
dbdump_20220304.db
dbdump_20220405_1.db
Desktop
Documents
Downloads
initial-setup-ks.cfg
mono-3.10.0.tar.bz2
Music
mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
Mysql.tar
nijunheng.tar
original-ks.cfg
Pictures
Public
Templates
tmp
touch
user.txt
Videos
zabbix-agent-5.0.18-1.el7.x86_64.rpm
[root@localhost ~]# ls /root /error &>ab  
[root@localhost ~]# cat ab               
ls: cannot access /error: No such file or directory
/root:
20220409
ab
anaconda-ks.cfg
cpptools-linux.vsix
create.sql.gz
dbdump_20220218.db
dbdump_20220301.db
dbdump_20220303_2.db
dbdump_20220303_3.db
dbdump_20220303.db
dbdump_20220304.db
dbdump_20220405_1.db
Desktop
Documents
Downloads
initial-setup-ks.cfg
mono-3.10.0.tar.bz2
Music
mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
Mysql.tar
nijunheng.tar
original-ks.cfg
Pictures
Public
Templates
tmp
touch
user.txt
Videos
zabbix-agent-5.0.18-1.el7.x86_64.rpm
[root@localhost ~]# 

ls /root /error >ab 2>&1和ls /root /error &>ab 输出结果一样

案例5-将内容输出至黑洞

image-20221119172714103

[root@localhost ~]# ls /root /error >ab 2>/dev/null 
[root@localhost ~]# cat ab 
/root:
20220409
ab
anaconda-ks.cfg
cpptools-linux.vsix
create.sql.gz
dbdump_20220218.db
dbdump_20220301.db
dbdump_20220303_2.db
dbdump_20220303_3.db
dbdump_20220303.db
dbdump_20220304.db
dbdump_20220405_1.db
Desktop
Documents
Downloads
initial-setup-ks.cfg
mono-3.10.0.tar.bz2
Music
mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
Mysql.tar
nijunheng.tar
original-ks.cfg
Pictures
Public
Templates
tmp
touch
user.txt
Videos
zabbix-agent-5.0.18-1.el7.x86_64.rpm
[root@localhost ~]# ls /root /error >ab &>/dev/null
[root@localhost ~]# cat ab 
[root@localhost ~]#

管道案例使用

案例1:将 /etc/passwd 中的用户按 UID 大小排序

sort -t参数表设定分隔符,-k3表选第三列,-n表按数字排序 -r降序

[root@localhost ~]# sort -t ":" -k3  -n /etc/passwd |head -5
root:x:0:0:root:/root:/bin/bash
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
[root@localhost ~]# sort -t ":" -k3  -n -r /etc/passwd |head -5
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
alice:x:1008:10001::/home/alice:/bin/bash
bob:x:1007:10000::/home/bob:/bin/bash
test3:x:1006:1007::/home/test3:/bin/bash
test2:x:1005:1006::/home/test2:/bin/bash
[root@localhost ~]# 

案例2: 统计当前 /etc/passwd 中用户使用的 shell 类型

awk -F指定分隔符 sort作用是排序,这儿是为了把同样的数据聚在一起,方便后面用uniq

[root@localhost ~]# awk -F: '{print$7}' /etc/passwd|sort|uniq
/bin/bash
/bin/csh
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown
[root@localhost ~]# awk -F: '{print$7}' /etc/passwd|sort|uniq -c
      8 /bin/bash
      1 /bin/csh
      1 /bin/sync
      1 /sbin/halt
     42 /sbin/nologin
      1 /sbin/shutdown
[root@localhost ~]# awk -F: '{print$7}' /etc/passwd|sort|uniq -c |sort -n
      1 /bin/csh
      1 /bin/sync
      1 /sbin/halt
      1 /sbin/shutdown
      8 /bin/bash
     42 /sbin/nologin
[root@localhost ~]# awk -F: '{print$7}' /etc/passwd|sort|uniq -c |sort -n -r
     42 /sbin/nologin
      8 /bin/bash
      1 /sbin/shutdown
      1 /sbin/halt
      1 /bin/sync
      1 /bin/csh
[root@localhost ~]# 

案例3: 打印当前所有 IP

[root@localhost ~]# ip addr|grep 'inet '|tee ip.txt|awk -F"/" '{print $1}'|awk '{print $2}'
127.0.0.1
192.168.0.50
192.168.122.1
[root@localhost ~]# cat ip.txt 
    inet 127.0.0.1/8 scope host lo
    inet 192.168.0.50/24 brd 192.168.0.255 scope global eth0
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
[root@localhost ~]# 

管道中使用xargs

xargs参数传递,主要让一些不支持管道的命令可以使用管道技术

[root@localhost ~]# which cat|xargs ls -l
-rwsr-xr-x 1 root root 54080 Nov 16  2020 /usr/bin/cat
[root@localhost ~]# which cat|ls -l      
total 724288
drwxr-xr-x  2 root root       113 Apr  9  2022 20220409
-rw-r--r--  1 root root         0 Nov 19 01:30 ab
-rw-------. 1 root root      4687 Aug 12  2021 anaconda-ks.cfg
-rw-r--r--  1 root root  70421306 Mar 25  2022 cpptools-linux.vsix
-rw-r--r--  1 root root   2087874 Dec 21  2021 create.sql.gz

linux文件查找

image-20221120201356984

find使用场景

#基于名称查找
[root@localhost ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}
[root@localhost ~]# ll /etc/sysconfig/network-scripts/
total 264
-rw-r--r-- 1 root root   449 Sep 25  2021 eth0_bak
-rw-r--r-- 1 root root   415 Dec  6  2021 ifcfg-eth0
-rw-r--r-- 1 root root     0 Apr 24 03:50 ifcfg-eth1
-rw-r--r-- 1 root root     0 Apr 24 03:50 IFCFG-ETH1
[root@localhost ~]# find /etc -name "ifcfg-eth1"
/etc/sysconfig/network-scripts/ifcfg-eth1
[root@localhost ~]# find /etc -iname "ifcfg-eth1"
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1
[root@localhost ~]# 
#基于大小查找
[root@localhost ~]# find /etc -size +5M
/etc/udev/hwdb.bin
[root@localhost ~]# ll -h /etc/udev/hwdb.bin
-r--r--r-- 1 root root 8.6M May 29  2022 /etc/udev/hwdb.bin
[root@localhost ~]# find /etc -size 5M
[root@localhost ~]# find /etc -size -5M
/etc
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/resolv.conf
/etc/fonts
/etc/fonts/conf.d
/etc/fonts/conf.d/65-0-lohit-marathi.conf
/etc/fonts/conf.d/59-liberation-mono.conf
。。。。。。

#基于类型查找
f文件、d目录、l链接、b块设备、c字符设备、s套接字、p管道文件
[root@localhost ~]# find /dev/ -type f
[root@localhost ~]# find /dev/ -type d
/dev/
/dev/snd
/dev/snd/by-path
。。。。。
/dev/raw
/dev/cpu
/dev/cpu/0
[root@localhost ~]# find /dev/ -type l
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
。。。。。。
/dev/char/4:43
/dev/char/4:44
/dev/char/4:41
/dev/char/4:42
/dev/char/4:4
/dev/char/4:40
/dev/char/4:38
/dev/char/4:39

#基于时间查找
[root@localhost findstest]# for i in {01..10};do date -s 2021/04/$i;touch file-$i; done
Thu Apr  1 00:00:00 PDT 2021
Fri Apr  2 00:00:00 PDT 2021
Sat Apr  3 00:00:00 PDT 2021
Sun Apr  4 00:00:00 PDT 2021
Mon Apr  5 00:00:00 PDT 2021
Tue Apr  6 00:00:00 PDT 2021
Wed Apr  7 00:00:00 PDT 2021
Thu Apr  8 00:00:00 PDT 2021
Fri Apr  9 00:00:00 PDT 2021
Sat Apr 10 00:00:00 PDT 2021
[root@localhost findstest]# ls
file-01  file-02  file-03  file-04  file-05  file-06  file-07  file-08  file-09  file-10
[root@localhost findstest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr  1 00:00 file-01
-rw-r--r-- 1 root root 0 Apr  2 00:00 file-02
-rw-r--r-- 1 root root 0 Apr  3 00:00 file-03
-rw-r--r-- 1 root root 0 Apr  4 00:00 file-04
-rw-r--r-- 1 root root 0 Apr  5 00:00 file-05
-rw-r--r-- 1 root root 0 Apr  6 00:00 file-06
-rw-r--r-- 1 root root 0 Apr  7 00:00 file-07
-rw-r--r-- 1 root root 0 Apr  8 00:00 file-08
-rw-r--r-- 1 root root 0 Apr  9 00:00 file-09
-rw-r--r-- 1 root root 0 Apr 10 00:00 file-10
[root@localhost findstest]# date
Sat Apr 10 00:00:29 PDT 2021
[root@localhost findstest]# find ./ -iname 'file*' -mtime +7
./file-01
./file-02
[root@localhost findstest]# find ./ -iname 'file*' -mtime 7 
./file-03
[root@localhost findstest]# find ./ -iname 'file*' -mtime -7
./file-04
./file-05
./file-06
./file-07
./file-08
./file-09
./file-10
[root@localhost findstest]# 

#面试题: 查找/var/Log 下所有以.Log结尾的文件,并保留最近7天的Log文件。
[root@localhost ~]# find /var/log/ -name '*\.log' -type f -mtime +7 -delete

#find基于用户查找
[root@localhost ~]# ls /home/
alice  bob  ftpuser01  laotie666  mysql  nijunheng  NoSuchUser  test  test1  test2  test3
#查找属主是bob
[root@localhost ~]# find /home/ -user bob 
/home/laotie666
。。。。。。
/home/bob/etc_ww.txt
#查找属组是mysql
[root@localhost ~]# find /home/ -group mysql
/home/mysql
。。。。。。
/home/mysql/.mysql_history
##查找属主是alice 属组是ops
[root@localhost ~]# usermod -g ops test
[root@localhost ~]# id alice
uid=1008(alice) gid=10001(ops) groups=10001(ops)
[root@localhost ~]# id test
uid=1003(test) gid=10001(ops) groups=10001(ops),1008(sharing)
[root@localhost ~]# find /home -user alice -group ops
/home/alice
。。。。。。
/home/alice/.bashrc
[root@localhost ~]# find /home -group ops -user alice
/home/alice
。。。。。。
/home/alice/.bashrc
[root@localhost ~]# find /home -group ops
/home/test
。。。。。。
/home/alice/.bashrc
[root@localhost ~]# find /home -user alicessss -group ops
find: ‘alicessss’ is not the name of a known user
[root@localhost ~]# find /home -group ops -user alicessss
find: ‘alicessss’ is not the name of a known user
[root@localhost ~]# find /home -group opsddd -user alice
find: ‘opsddd’ is not the name of an existing group
[root@localhost ~]# usermod -u alice test
usermod: invalid user ID 'alice'
[root@localhost ~]# 
#查找属主是jack,并且属组是admin
[root@localhost ~]# find /home -group ops -a -user alice
/home/alice
。。。。。。
/home/alice/.bashrc

#查找属主是jack,或者属组是admin
[root@localhost ~]# find /home -group ops -o -user alice 
/home/test
。。。。。。
/home/test/.bashrc
/home/alice
。。。。。。
/home/alice/.bashrc
[root@localhost ~]# 

#查找没有属主
[test20221206@localhost ~]$ ll
total 0
-rw-rw-r-- 1 test20221206 test20221206 0 Apr 10 03:56 laotie666.txt
[test20221206@localhost ~]$ touch /tmp/666.txt
[test20221206@localhost ~]$ ll /tmp/666.txt 
-rw-rw-r-- 1 test20221206 test20221206 0 Apr 10 04:23 /tmp/666.txt
[test20221206@localhost ~]$ exit
logout
[root@localhost ~]# userdel test20221206
[root@localhost ~]# ll /home/test20221206/
total 0
-rw-rw-r-- 1 1009 1009 0 Apr 10 03:56 laotie666.txt
[root@localhost ~]# find /home/ -nouser
/home/test20221206
。。。。。。
/home/test20221206/laotie666.txt
/home/test20221206/.bash_history
#查找没有属组
[root@localhost ~]# find /tmp/ -nogroup 
/tmp/666.txt
[root@localhost ~]# 

#基于权限查找
#精确
[root@localhost ~]# find /root/ -type f -perm 644 |exec ls
#包含
[root@localhost ~]# find /root/ -type f -perm -644 |exec ls
#特殊权限
[root@localhost ~]# find /root/ -type f -perm -4000 |exec ls
[root@localhost ~]# find /root/ -type f -perm -2000 |exec ls
[root@localhost ~]# find /root/ -type f -perm -1000 |exec ls 

#1.查找当前目录下,属主不是root的所有文件
[root@localhost ~]# find . -not -user root
[root@localhost ~]# chown bob:bob ip.txt 
chown: invalid group: ‘bob:bob’
[root@localhost ~]# id bob
uid=1007(bob) gid=10000(dev) groups=10000(dev),10001(ops)
[root@localhost ~]# chown bob:ops ip.txt 
[root@localhost ~]# find . -not -user root
./ip.txt
[root@localhost ~]# 

linux文件压缩

image-20221206125750075

#gzip
[root@localhost tmp]# ll findstest/
total 0
-rw-r--r-- 1 root root 0 Apr  1 00:00 file-01
-rw-r--r-- 1 root root 0 Apr  2 00:00 file-02
-rw-r--r-- 1 root root 0 Apr  3 00:00 file-03
-rw-r--r-- 1 root root 0 Apr  4 00:00 file-04
-rw-r--r-- 1 root root 0 Apr  5 00:00 file-05
-rw-r--r-- 1 root root 0 Apr  6 00:00 file-06
-rw-r--r-- 1 root root 0 Apr  7 00:00 file-07
-rw-r--r-- 1 root root 0 Apr  8 00:00 file-08
-rw-r--r-- 1 root root 0 Apr  9 00:00 file-09
-rw-r--r-- 1 root root 0 Apr 10 00:00 file-10
[root@localhost tmp]# gzip findstest
gzip: findstest is a directory -- ignored
[root@localhost tmp]# ls
data  findstest  test

[root@localhost tmp]# gzip -r findstest
[root@localhost tmp]# ls
data  findstest  test
[root@localhost tmp]# ls
data  findstest  test
[root@localhost tmp]# ll findstest/
total 40
-rw-r--r-- 1 root root 28 Apr  1 00:00 file-01.gz
-rw-r--r-- 1 root root 28 Apr  2 00:00 file-02.gz
-rw-r--r-- 1 root root 28 Apr  3 00:00 file-03.gz
-rw-r--r-- 1 root root 28 Apr  4 00:00 file-04.gz
-rw-r--r-- 1 root root 28 Apr  5 00:00 file-05.gz
-rw-r--r-- 1 root root 28 Apr  6 00:00 file-06.gz
-rw-r--r-- 1 root root 28 Apr  7 00:00 file-07.gz
-rw-r--r-- 1 root root 28 Apr  8 00:00 file-08.gz
-rw-r--r-- 1 root root 28 Apr  9 00:00 file-09.gz
-rw-r--r-- 1 root root 28 Apr 10 00:00 file-10.gz
[root@localhost tmp]# 

[root@localhost tmp]# cd test/
[root@localhost test]# ls
[root@localhost test]# touch 1.txt 2.txt
[root@localhost test]# ls
1.txt  2.txt
[root@localhost test]# gzip 1.txt 
[root@localhost test]# ls
1.txt.gz  2.txt
[root@localhost test]# zcat 1.txt.gz 
[root@localhost test]# echo "blue fly" >> 2.txt 
[root@localhost test]# gzip 2.txt 
[root@localhost test]# zcat 2.txt.gz 
blue fly
[root@localhost test]#

#zip
[root@localhost tmp]# ls findstest/
file-01  file-02  file-03  file-04  file-05  file-06  file-07  file-08  file-09  file-10
[root@localhost tmp]# zip fi.zip findstest/
  adding: findstest/ (stored 0%)
[root@localhost tmp]# zipinfo fi.zip 
Archive:  fi.zip
Zip file size: 170 bytes, number of entries: 1
drwxr-xr-x  3.0 unx        0 bx stor 21-Apr-10 07:11 findstest/#不加参数r 有问题
1 file, 0 bytes uncompressed, 0 bytes compressed:  0.0%
#查看zip压缩包是否是完整的
[root@localhost tmp]# zip -T fi
findstest/ fi.zip     
[root@localhost tmp]# zip -T fi.zip 
test of fi.zip OK
[root@localhost tmp]# ls findstest/
file-01  file-02  file-03  file-04  file-05  file-06  file-07  file-08  file-09  file-10
#压缩目录为zip包
[root@localhost tmp]# zip -r fi2.zip findstest
  adding: findstest/ (stored 0%)
  adding: findstest/file-01 (stored 0%)
  adding: findstest/file-02 (stored 0%)
  adding: findstest/file-03 (stored 0%)
  adding: findstest/file-04 (stored 0%)
  adding: findstest/file-05 (stored 0%)
  adding: findstest/file-06 (stored 0%)
  adding: findstest/file-07 (stored 0%)
  adding: findstest/file-08 (stored 0%)
  adding: findstest/file-09 (stored 0%)
  adding: findstest/file-10 (stored 0%)
#不解压压缩查看压缩包中的内容
[root@localhost tmp]# zipinfo fi2.zip 
Archive:  fi2.zip
Zip file size: 1790 bytes, number of entries: 11
drwxr-xr-x  3.0 unx        0 bx stor 21-Apr-10 07:11 findstest/
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-01 00:00 findstest/file-01
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-02 00:00 findstest/file-02
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-03 00:00 findstest/file-03
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-04 00:00 findstest/file-04
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-05 00:00 findstest/file-05
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-06 00:00 findstest/file-06
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-07 00:00 findstest/file-07
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-08 00:00 findstest/file-08
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-09 00:00 findstest/file-09
-rw-r--r--  3.0 unx        0 bx stor 21-Apr-10 00:00 findstest/file-10
11 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%
[root@localhost tmp]# 
[root@localhost tmp]# unzip -l fi2.zip 
Archive:  fi2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-10-2021 07:11   findstest/
        0  04-01-2021 00:00   findstest/file-01
        0  04-02-2021 00:00   findstest/file-02
        0  04-03-2021 00:00   findstest/file-03
        0  04-04-2021 00:00   findstest/file-04
        0  04-05-2021 00:00   findstest/file-05
        0  04-06-2021 00:00   findstest/file-06
        0  04-07-2021 00:00   findstest/file-07
        0  04-08-2021 00:00   findstest/file-08
        0  04-09-2021 00:00   findstest/file-09
        0  04-10-2021 00:00   findstest/file-10
---------                     -------
        0                     11 files
[root@localhost tmp]# 
[root@localhost tmp]# mv findstest findstest2
[root@localhost tmp]# ls
data  fi2.zip  findstest2  fi.zip  test
[root@localhost tmp]# unzip fi.zip 
Archive:  fi.zip
   creating: findstest/
[root@localhost tmp]# ls
data  fi2.zip  findstest  findstest2  fi.zip  test
[root@localhost tmp]# ll findstest
total 0
[root@localhost tmp]#
[root@localhost tmp]# unzip fi2.zip 
Archive:  fi2.zip
   creating: findstest/
 extracting: findstest/file-01       
 extracting: findstest/file-02       
 extracting: findstest/file-03       
 extracting: findstest/file-04       
 extracting: findstest/file-05       
 extracting: findstest/file-06       
 extracting: findstest/file-07       
 extracting: findstest/file-08       
 extracting: findstest/file-09       
 extracting: findstest/file-10       
[root@localhost tmp]# ls
data  fi2.zip  findstest  fi.zip  test
[root@localhost tmp]# ls findstest/
file-01  file-02  file-03  file-04  file-05  file-06  file-07  file-08  file-09  file-10
[root@localhost tmp]# rm -rf findstest
[root@localhost tmp]# unzip fi2.zip ./opt
Archive:  fi2.zip
caution: filename not matched:  ./opt
[root@localhost tmp]# ls
data  fi2.zip  fi.zip  test
[root@localhost tmp]# unzip fi2.zip -d ./opt
Archive:  fi2.zip
   creating: ./opt/findstest/
 extracting: ./opt/findstest/file-01  
 extracting: ./opt/findstest/file-02  
 extracting: ./opt/findstest/file-03  
 extracting: ./opt/findstest/file-04  
 extracting: ./opt/findstest/file-05  
 extracting: ./opt/findstest/file-06  
 extracting: ./opt/findstest/file-07  
 extracting: ./opt/findstest/file-08  
 extracting: ./opt/findstest/file-09  
 extracting: ./opt/findstest/file-10  
[root@localhost tmp]# ls opt/
findstest
[root@localhost tmp]# ls opt/findstest/
file-01  file-02  file-03  file-04  file-05  file-06  file-07  file-08  file-09  file-10
[root@localhost tmp]# 

文件打包与压缩-tar

常用打包与压缩组合

czf #打包tar.gz格式
cjf #打包tar. bz格式
cJf #打包tar.xz格式
zxf #解压tar . gz格式

jxf #解压tar. bz格式
xf #自动选择解压模式
tf #查看压缩包内容

[root@localhost tmp]# ls
data  findstest  opt  test
[root@localhost tmp]# tar czf 1.tar.gz findstest/ data/
[root@localhost tmp]# ls
1.tar.gz  data  findstest  opt  test
[root@localhost tmp]# tar cjf 1.tar.bz2 findstest/ data/  
[root@localhost tmp]# ls
1.tar.bz2  1.tar.gz  data  findstest  opt  test
[root@localhost tmp]# tar cJf 1.tar.xz findstest/ data/    
[root@localhost tmp]# ls
1.tar.bz2  1.tar.gz  1.tar.xz  data  findstest  opt  test
[root@localhost tmp]# 
[root@localhost tmp]# tar tf 1.tar.bz2 
findstest/
findstest/file-01
findstest/file-02
。。。。。。
[root@localhost tmp]# tar tf 1.tar.gz 
findstest/
findstest/file-01
。。。。。。
[root@localhost tmp]# tar tf 1.tar.xz 
findstest/
findstest/file-01
。。。。。。

排除文件, 并打包压缩

[root@localhost tmp]# ls
1.tar.bz2  1.tar.gz  1.tar.xz  data  findstest  opt  test
[root@localhost tmp]# tar -czf 20221218.tar.gz --exclude=test/ data/ findstest
[root@localhost tmp]# ls
1.tar.bz2  1.tar.gz  1.tar.xz  20221218.tar.gz  data  findstest  opt  test
[root@localhost tmp]# tar tf 20221218.tar.gz 
data/
data/www_v1/
......
data/www_v2/index.html
data/www
findstest/
findstest/file-01
......
findstest/file-10
[root@localhost tmp]#

linux软件包管理

[root@localhost ~]# rpm -ivh /mnt/dvd/Packages/vsftpd-3.0.2-21.el7.x86_64.rpm 
warning: /mnt/dvd/Packages/vsftpd-3.0.2-21.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:vsftpd-3.0.2-21.el7              ################################# [100%]
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# rpm -e vsftpd-3.0.2-21.el7.x86_64.rpm
error: package vsftpd-3.0.2-21.el7.x86_64.rpm is not installed #删除rpm包时,不用带扩展名
[root@localhost ~]# rpm -e vsftpd-3.0.2-21.el7.x86_64
[root@localhost ~]# rpm -qa|grep vsftpd
[root@localhost ~]#

rpm软件包更新

通过-U参数即可升级

[root@localhost ~]# rpm -qa|grep zabbix
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.2/rhel/7/x86_64/zabbix-agent-3.2.0-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.590f34c4HSmsmw
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/3.2/rhel/7/x86_64/zabbix-agent-3.2.0-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.590f34c4HSmsmw
warning: /var/tmp/rpm-tmp.kpQMHn: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-3.2.0-1.el7         ################################# [100%]
[root@localhost ~]# rpm -qa|grep zabbix
zabbix-agent-3.2.0-1.el7.x86_64
[root@localhost ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.ea052e2fJNAe3S
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.ea052e2fJNAe3S
warning: /var/tmp/rpm-tmp.ln2CzA: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
        file /etc/zabbix/zabbix_agentd.conf from install of zabbix-agent-5.0.0-1.el7.x86_64 conflicts with file from package zabbix-agent-3.2.0-1.el7.x86_64
        file /usr/lib/systemd/system/zabbix-agent.service from install of zabbix-agent-5.0.0-1.el7.x86_64 conflicts with file from package zabbix-agent-3.2.0-1.el7.x86_64
        file /usr/lib/tmpfiles.d/zabbix-agent.conf from install of zabbix-agent-5.0.0-1.el7.x86_64 conflicts with file from package zabbix-agent-3.2.0-1.el7.x86_64
        file /usr/sbin/zabbix_agentd from install of zabbix-agent-5.0.0-1.el7.x86_64 conflicts with file from package zabbix-agent-3.2.0-1.el7.x86_64
        file /usr/share/man/man8/zabbix_agentd.8.gz from install of zabbix-agent-5.0.0-1.el7.x86_64 conflicts with file from package zabbix-agent-3.2.0-1.el7.x86_64
[root@localhost ~]# rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.ea052e2fJNAe3S 
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.ea052e2fJNAe3S
warning: /var/tmp/rpm-tmp.7CPb2v: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-5.0.0-1.el7         ################################# [ 50%]
Cleaning up / removing...
   2:zabbix-agent-3.2.0-1.el7         ################################# [100%]
[root@localhost ~]# rpm -qa|grep zabbix
zabbix-agent-5.0.0-1.el7.x86_64
[root@localhost ~]#
[root@localhost ~]# rpm -qa|grep zabbix
zabbix-agent-5.0.0-1.el7.x86_64
[root@localhost ~]# rpm -q zabbix-agent  #查看指定软件包是否安装
zabbix-agent-5.0.0-1.el7.x86_64
[root@localhost ~]# rpm -qa zabbix-agent #查看系统中已安装的所有RPM软件包列表
zabbix-agent-5.0.0-1.el7.x86_64
[root@localhost ~]# rpm -qi zabbix-agent # 查看指定软件的详细信息
Name        : zabbix-agent
Version     : 5.0.0
Release     : 1.el7
Architecture: x86_64
Install Date: Sat 31 Dec 2022 03:28:44 AM PST
Group       : Applications/Internet
Size        : 1838133
License     : GPLv2+
Signature   : RSA/SHA512, Mon 11 May 2020 05:06:59 AM PDT, Key ID 082ab56ba14fe591
Source RPM  : zabbix-5.0.0-1.el7.src.rpm
Build Date  : Mon 11 May 2020 04:30:16 AM PDT
Build Host  : builds.zabbix.lan
Relocations : (not relocatable)
URL         : http://www.zabbix.com/
Summary     : Old Zabbix Agent
Description :
Old implementation of zabbix agent.
To be installed on monitored systems.
[root@localhost ~]# rpm -ql zabbix-agent #查询指定软件包所安装的目录、文件列表
/etc/logrotate.d/zabbix-agent
/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.d
/usr/lib/systemd/system/zabbix-agent.service
/usr/lib/tmpfiles.d/zabbix-agent.conf
/usr/sbin/zabbix_agentd
/usr/share/doc/zabbix-agent-5.0.0
/usr/share/doc/zabbix-agent-5.0.0/AUTHORS
/usr/share/doc/zabbix-agent-5.0.0/COPYING
/usr/share/doc/zabbix-agent-5.0.0/ChangeLog
/usr/share/doc/zabbix-agent-5.0.0/NEWS
/usr/share/doc/zabbix-agent-5.0.0/README
/usr/share/doc/zabbix-agent-5.0.0/userparameter_mysql.conf
/usr/share/man/man8/zabbix_agentd.8.gz
/var/log/zabbix
/var/run/zabbix
[root@localhost ~]# rpm -qc zabbix-agent #查询指定软件包的配置文件
/etc/logrotate.d/zabbix-agent
/etc/zabbix/zabbix_agentd.conf
[root@localhost ~]# rpm -qf /etc/logrotate.d/zabbix-agent #查询文件或目录属于哪个RPM软件
zabbix-agent-5.0.0-1.el7.x86_64
[root@localhost ~]# 
标签: 暂无
最后更新:2024年3月16日

西伯利亚狼

这个人很懒,什么都没留下

点赞
< 上一篇

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复
分类

COPYRIGHT © 2024 大倪. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang