1、显示 /etc 目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录
[root@centos8 ~]# ls /etc/[^[:alpha:]][:alpha:]*
2、复制 /etc 目录下所有以 p 开头,以非数字结尾的文件或目录到 /tmp/mytest1 目录中。
[root@centos8 ~]# cp -r /etc/{p,P}*[^[:digit:]] /tmp/mytest1
3、将 /etc/issue 文件中的内容转化为大写后保存至 /tmp/issue.out 文件中
[root@centos8 ~]# cat /etc/issue | tr [a-z] [A-Z] > /tmp/issue.out
4、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组 distro,其 GID 为 2019;
[root@centos8 ~]# groupadd distro -g 2019
(2)、创建用户 mandriva,其 ID 号为 1005;基本组为 distro;
[root@centos8 ~]# useradd mandriva -u 1005 -g distro
(3)、创建用户 mageia,其 ID 号为 1100,家目录为 /home/linux;
[root@centos8 ~]# useradd -u 1100 -d /home/linux mageia
(4)、给用户 mageia 添加密码,密码为 mageedu,并设置用户密码 7 天后过期
[root@centos8 ~]# echo “mageedu” | passwd -x 7 mageia
(5)、删除 mandriva,但保留其家目录;
[root@centos8 ~]# userdel mandriva
(6)、创建用户 slackware,其 ID 号为 2002,基本组为 distro,附加组 peguin;
[root@centos8 ~]# groupadd peguin
[root@centos8 ~]# useradd -u 2002 slackware -g distro -G peguin
(7)、修改 slackware 的默认 shell 为 /bin/tcsh;
[root@centos8 ~]# usermod -s /bin/tcsh slackware
(8)、为用户 slackware 新增附加组 admins;
[root@centos8 ~]# groupadd admins
[root@centos8 ~]# usermod -G admins slackware
标准 I/O 重定向和管道
内容概述
1 标准输入和输出
内存空间:kernel+APP1(指令+data)+APP2(指令+data)+...
打开的文件都有一个 fd:file descriptor
三种 I/O 设备:标准输入、标准输出、标准错误
文件描述符
1
2
[root@centos8 ~]# tail -f anaconda-ks.cfg
[root@centos8 ~]# ps -ef | grep -i tail
root 5283 1630 0 14:28 pts/0 00:00:00 tail -f anaconda-ks.cfg
root 5348 5293 0 14:30 pts/1 00:00:00 grep --color=auto -i tail
[root@centos8 ~]# ll /proc | grep 5283
dr-xr-xr-x. 9 root root 0 Oct 17 14:28 5283
[root@centos8 ~]# ls /proc
1 115 130 16 22 372 476 5318 742 824 935 dma kpageflags self
10 116 131 1610 23 373 477 5319 743 825 936 driver loadavg slabinfo
101 117 132 1615 26 374 478 5343 759 826 939 execdomains locks softirqs
102 118 133 1618 27 375 479 5356 760 827 943 fb mdstat stat
103 119 134 1622 28 38 480 5359 782 828 957 filesystems meminfo swaps
104 12 135 1628 29 381 481 583 786 830 961 fs misc sys
105 120 136 1629 3 4 482 6 788 831 acpi interrupts modules sysrq-trigger
106 121 137 1630 30 413 50 614 793 832 asound iomem mounts sysvipc
107 122 1378 1636 31 416 5168 665 794 833 buddyinfo ioports mpt thread-self
108 123 138 1661 32 433 5179 667 8 848 bus irq mtrr timer_list
109 124 1387 1662 33 434 5222 69 815 851 cgroups kallsyms net tty
11 125 14 1663 34 4356 5233 736 817 869 cmdline kcore pagetypeinfo uptime
110 126 1403 17 35 439 5283 737 818 877 consoles keys partitions version
111 127 15 1762 36 452 5285 738 819 880 cpuinfo key-users pressure vmallocinfo
112 128 1535 19 37 471 5290 739 820 9 crypto kmsg sched_debug vmstat
113 129 1591 2 370 472 5292 740 821 915 devices kpagecgroup schedstat zoneinfo
114 13 1592 21 371 475 5293 741 822 924 diskstats kpagecount scsi
[root@centos8 ~]# ps aux | grep -i tail
root 5283 0.0 0.0 7316 808 pts/0 S+ 14:28 0:00 tail -f anaconda-ks.cfg
root 5400 0.0 0.0 12108 968 pts/1 R+ 14:34 0:00 grep --color=auto -i tail
[root@centos8 ~]# cd /proc/5283
[root@centos8 5283]# ls
attr coredump_filter gid_map mountinfo oom_score sched stat uid_map
autogroup cpuset io mounts oom_score_adj schedstat statm wchan
auxv cwd limits mountstats pagemap sessionid status
cgroup environ loginuid net patch_state setgroups syscall
clear_refs exe map_files ns personality smaps task
cmdline fd maps numa_maps projid_map smaps_rollup timers
comm fdinfo mem oom_adj root stack timerslack_ns
[root@centos8 5283]# ll
total 0
dr-xr-xr-x. 2 root root 0 Oct 17 14:35 attr
-rw-r--r--. 1 root root 0 Oct 17 14:35 autogroup
-r--------. 1 root root 0 Oct 17 14:35 auxv
-r--r--r--. 1 root root 0 Oct 17 14:35 cgroup
--w-------. 1 root root 0 Oct 17 14:35 clear_refs
-r--r--r--. 1 root root 0 Oct 17 14:28 cmdline
-rw-r--r--. 1 root root 0 Oct 17 14:35 comm
-rw-r--r--. 1 root root 0 Oct 17 14:35 coredump_filter
-r--r--r--. 1 root root 0 Oct 17 14:35 cpuset
lrwxrwxrwx. 1 root root 0 Oct 17 14:35 cwd -> /root
-r--------. 1 root root 0 Oct 17 14:35 environ
lrwxrwxrwx. 1 root root 0 Oct 17 14:28 exe -> /usr/bin/tail(查找问题程序全路径,一个面试点)
dr-x------. 2 root root 0 Oct 17 14:35 fd
dr-x------. 2 root root 0 Oct 17 14:35 fdinfo
-rw-r--r--. 1 root root 0 Oct 17 14:35 gid_map
-r--------. 1 root root 0 Oct 17 14:35 io
-r--r--r--. 1 root root 0 Oct 17 14:35 limits
-rw-r--r--. 1 root root 0 Oct 17 14:35 loginuid
dr-x------. 2 root root 0 Oct 17 14:35 map_files
-r--r--r--. 1 root root 0 Oct 17 14:35 maps
-rw-------. 1 root root 0 Oct 17 14:35 mem
-r--r--r--. 1 root root 0 Oct 17 14:35 mountinfo
-r--r--r--. 1 root root 0 Oct 17 14:35 mounts
-r--------. 1 root root 0 Oct 17 14:35 mountstats
dr-xr-xr-x. 6 root root 0 Oct 17 14:35 net
dr-x--x--x. 2 root root 0 Oct 17 14:35 ns
-r--r--r--. 1 root root 0 Oct 17 14:35 numa_maps
-rw-r--r--. 1 root root 0 Oct 17 14:35 oom_adj
-r--r--r--. 1 root root 0 Oct 17 14:35 oom_score
-rw-r--r--. 1 root root 0 Oct 17 14:35 oom_score_adj
-r--------. 1 root root 0 Oct 17 14:35 pagemap
-r--------. 1 root root 0 Oct 17 14:35 patch_state
-r--------. 1 root root 0 Oct 17 14:35 personality
-rw-r--r--. 1 root root 0 Oct 17 14:35 projid_map
lrwxrwxrwx. 1 root root 0 Oct 17 14:35 root -> /
-rw-r--r--. 1 root root 0 Oct 17 14:35 sched
-r--r--r--. 1 root root 0 Oct 17 14:35 schedstat
-r--r--r--. 1 root root 0 Oct 17 14:35 sessionid
-rw-r--r--. 1 root root 0 Oct 17 14:35 setgroups
-r--r--r--. 1 root root 0 Oct 17 14:35 smaps
-r--r--r--. 1 root root 0 Oct 17 14:35 smaps_rollup
-r--------. 1 root root 0 Oct 17 14:35 stack
-r--r--r--. 1 root root 0 Oct 17 14:28 stat
-r--r--r--. 1 root root 0 Oct 17 14:35 statm
-r--r--r--. 1 root root 0 Oct 17 14:29 status
-r--------. 1 root root 0 Oct 17 14:35 syscall
dr-xr-xr-x. 3 root root 0 Oct 17 14:35 task
-r--r--r--. 1 root root 0 Oct 17 14:35 timers
-rw-rw-rw-. 1 root root 0 Oct 17 14:35 timerslack_ns
-rw-r--r--. 1 root root 0 Oct 17 14:35 uid_map
-r--r--r--. 1 root root 0 Oct 17 14:35 wchan
[root@centos8 5283]# cd fd
[root@centos8 fd]# ll
total 0
lrwx------. 1 root root 64 Oct 17 14:39 0 -> /dev/pts/0
lrwx------. 1 root root 64 Oct 17 14:39 1 -> /dev/pts/0
lrwx------. 1 root root 64 Oct 17 14:39 2 -> /dev/pts/0
lr-x------. 1 root root 64 Oct 17 14:39 3 -> /root/anaconda-ks.cfg(知道程序打开的文件)
lr-x------. 1 root root 64 Oct 17 14:39 4 -> anon_inode:inotify
[root@centos8 ~]# echo $SHELL
/bin/bash
[root@centos8 ~]# ps aux | grep bash
root 848 0.0 0.1 25380 2400 ? S 09:09 0:00 /bin/bash /usr/sbin/ksmtuned
root 1630 0.0 0.2 26696 5456 pts/0 Ss 09:09 0:00 -bash
root 5293 0.0 0.2 26564 5164 pts/1 Ss 14:28 0:00 -bash
root 5484 0.0 0.0 12108 1068 pts/1 S+ 14:42 0:00 grep --color=auto bash
[root@centos8 ~]# echo $$
5293
[root@centos8 ~]# cd /proc/$$/fd
[root@centos8 fd]# ll
total 0
lrwx------. 1 root root 64 Oct 17 14:28 0 -> /dev/pts/1
lrwx------. 1 root root 64 Oct 17 14:28 1 -> /dev/pts/1
lrwx------. 1 root root 64 Oct 17 14:28 2 -> /dev/pts/1
lrwx------. 1 root root 64 Oct 17 14:44 255 -> /dev/pts/1
lr-x------. 1 root root 64 Oct 17 14:28 3 -> /var/lib/sss/mc/passwd
lrwx------. 1 root root 64 Oct 17 14:28 4 -> 'socket:[71572]'
[root@centos8 fd]# tty
/dev/pts/1
[root@centos8 fd]# ll /dev/st*
lrwxrwxrwx. 1 root root 15 Oct 17 09:08 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 Oct 17 09:08 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 Oct 17 09:08 /dev/stdout -> /proc/self/fd/1
2 I/O 重定向 redirect
命令 操作符号 文件名
1> 或 > 把 STDOUT 重定向到文件
2> 把 STDERR 重定向到文件
$> 把所有输出重定向到文件
2>> 追加标准错误重定向至文件
&>>
(CMD1;CMD2...)或者{CMD1;CMD2;...;}
[root@centos8 ~]# tty
/dev/pts/0
[root@centos8 ~]# hostname 1> /dev/pts/1
[root@centos8 ~]# tty
/dev/pts/1
[root@centos8 ~]# centos8.magedu.org
[root@centos8 ~]# w
14:58:03 up 5:49, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 09:09 1.00s 0.04s 0.00s w
root pts/1 10.0.0.1 14:28 1:58 0.05s 0.05s -bash
[root@centos8 ~]# ls
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@centos8 ~]# ls 1> /dev/pts/1
[root@centos8 ~]# centos8.magedu.org
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
touch 创建文件时,文件之前已存在,不会覆盖。> 直接就清零了。
[root@centos8 ~]# cat /dev/null > /data/stdout.log
[root@centos8 ~]# echo abc > /data/stdout.log
[root@centos8 ~]# echo > /data/stdout.log
[root@centos8 ~]# ll /data/stdout.log
-rw-r--r--. 1 root root 1 Oct 17 15:11 /data/stdout.log
[root@centos8 ~]# hexdump -C /data/stdout.log
00000000 0a |.|(0a 换行)
00000001
[root@centos8 ~]# echo -n > /data/stdout.log
[root@centos8 ~]# ll /data/stdout.log
-rw-r--r--. 1 root root 0 Oct 17 15:11 /data/stdout.log
[root@centos8 ~]# xxx 2> /data/stdout.log
[root@centos8 ~]# cat /data/stdout.log
bash: xxx: command not found...
[root@centos8 ~]# alias rm
alias rm='rm -i'
[root@centos8 ~]# rm /data/stdout.log 2> /data/stderr.log
[root@centos8 ~]# tty
/dev/pts/1
[root@centos8 ~]# cat /data/stderr.log
rm: remove regular file '/data/stdout.log'? [root@centos8 ~]#
[root@centos8 ~]# ls /data /err
ls: cannot access '/err': No such file or directory
/data:
[root@centos8 ~]# ls /data /err > /data/stdout.log 2> /data/stderr.log
[root@centos8 ~]# cat /data/stdout.log
/data:
stderr.log
stdout.log
[root@centos8 ~]# cat /data/stderr.log
ls: cannot access '/err': No such file or directory
[root@centos8 ~]# ls /data /err &> /data/all.log
[root@centos8 ~]# cat /data/all.log
ls: cannot access '/err': No such file or directory
/data:
all.log
stderr.log
stdout.log
[root@centos8 ~]# (ls;date) > /data/all.log
[root@centos8 ~]# ls /data /err > /data/all.log 2>&1
[root@centos8 ~]# ls /data /err 1> /data/all.log 2>&1(把 2 也写入 1)
[root@centos8 ~]# ls /data /err 2> /data/all.log 1>&2
[root@centos8 ~]# ls /data /err 2>&1 > /data/all.log(执行过程从左到右,一个面试题)
ls: cannot access '/err': No such file or directory
[root@centos8 ~]# cat /data/all.log
/data:
all.log
stderr.log
stdout.log
2.1 标准输出和错误重新定向
2.2 标准输入重定向
[root@centos8 data]# cat bc.log
2*3
4+5
[root@centos8 data]# bc < bc.log
6
9
[root@centos8 data]# vi f1.txt
[root@centos8 data]# cat f1.txt
y
[root@centos8 data]# rm bc.log < f1.txt
rm: remove regular file 'bc.log'? [root@centos8 data]# ll
total 4
-rw-r--r--. 1 root root 2 Oct 18 08:20 f1.txt
[root@centos8 data]# seq -s+ 1 10 > bc.log(一个程序生成数据,另一个程序处理)
[root@centos8 data]# cat bc.log
1+2+3+4+5+6+7+8+9+10
[root@centos8 data]# bc < bc.log
55
[root@centos8 data]# seq -s+ 1 10 | bc(| 前面一定有标准输出,| 后面的待支持标准输入)
55
2.2.1 tr命令
-c
-t
-d
-s
-dc
[root@centos8 data]# tr '123' 'abc'
12345abcd12345
abc45abcdabc45
[root@centos8 data]# tr '1234' 'abc'
12345abcd12345
abcc5abcdabcc5
[root@centos8 data]# tr -t '1234' 'abc'
12345abcd12345
abc45abcdabc45
[root@centos8 data]# tr '[:lower:]' '[:upper:]'
zbcd
ZBCD
[root@centos8 data]# tr -s abc
aabbccddaacbb
abcddacb
[root@centos8 data]# echo {1..10} | tr ' ' + | bc
55
[root@centos8 data]# tr -d ' 15' < win.txt > linux.txt
2.2.2 标准输入重定向
[root@centos8 data]# cat > cat.log
line1
[root@centos8 data]# cat cat.log
[root@centos8 data]# cat > cat.log
line1
(单行重定向)
[root@centos8 data]# cat cat.log
line1
[root@centos8 data]# cat > cat.log
line1
line2
[root@centos8 data]# cat cat.log
line1
[root@centos8 data]# cat > cat.log
line1
line2
(单行重定向)
[root@centos8 data]# cat cat.log
line1
line2
2.2.3 把多行重定向
就地文本 here documents
[root@centos8 data]# cat > cat2.log <
line1
EOF tab
line3
EOF
[root@centos8 data]# ll
total 8
-rw-r--r--. 1 root root 20 Oct 18 10:31 cat2.log
-rw-r--r--. 1 root root 12 Oct 18 10:14 cat.log
[root@centos8 data]# cat cat2.log
line1
EOF tab
line3
[root@centos8 data]# yum -y install postfix
CentOS-8 - AppStream 5.7 kB/s | 4.3 kB 00:00
CentOS-8 - Base 5.1 kB/s | 3.9 kB 00:00
CentOS-8 - Extras 2.3 kB/s | 1.5 kB 00:00
Dependencies resolved.
Package Architecture Version Repository Size
Installing:
postfix x86_64 2:3.3.1-12.el8 BaseOS 1.4 M
Transaction Summary
Install 1 Package
Total download size: 1.4 M
Installed size: 4.1 M
Downloading Packages:
postfix-3.3.1-12.el8.x86_64.rpm 4.1 MB/s | 1.4 MB 00:00
Total 1.3 MB/s | 1.4 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Running scriptlet: postfix-2:3.3.1-12.el8.x86_64 1/1
Installing : postfix-2:3.3.1-12.el8.x86_64 1/1
Running scriptlet: postfix-2:3.3.1-12.el8.x86_64 1/1
Verifying : postfix-2:3.3.1-12.el8.x86_64 1/1
Installed:
postfix-2:3.3.1-12.el8.x86_64
Complete!
[root@centos8 data]# systemctl start postfix
[root@centos8 ~]# ss -ntl | grep 25
LISTEN 0 100 127.0.0.1:25 0.0.0.0:
LISTEN 0 100 [::1]:25 [::]:
[root@centos8 data]# mail -s test wang
hello
.
EOT
[root@centos8 ~]# su - wang
[wang@centos8 ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/wang": 2 messages 2 new
N 1 root Sun Oct 18 10:47 18/581 "test"
N 2 root Sun Oct 18 10:47 18/581 "test"
& 1
Message 1:
From root@centos8.magedu.org Sun Oct 18 10:47:02 2020
Return-Path:
X-Original-To: wang
Delivered-To: wang@centos8.magedu.org
Date: Sun, 18 Oct 2020 10:44:07 +0800
To: wang@centos8.magedu.org
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root
Status: R
hello
& 2
Message 2:
From root@centos8.magedu.org Sun Oct 18 10:47:12 2020
Return-Path:
X-Original-To: wang
Delivered-To: wang@centos8.magedu.org
Date: Sun, 18 Oct 2020 10:47:12 +0800
To: wang@centos8.magedu.org
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root
Status: R
hello
&
[root@centos8 data]# mail -s hello wang < cat.log
3 管道
3.1 管道
所有命令会在当前 shell 进程的子 shell 进程中执行
STDERR默认不能通过管道转发,可以利用 2>&1 或 |& 实现
[root@centos8 data]# ls /data /err | tr 'a-z' 'A-Z'
ls: cannot access '/err': No such file or directory(只接收标准输出)
/DATA:
CAT2.LOG
CAT.LOG
[root@centos8 data]# ls /data /err 2>&1 | tr 'a-z' 'A-Z'
LS: CANNOT ACCESS '/ERR': NO SUCH FILE OR DIRECTORY
/DATA:
CAT2.LOG
CAT.LOG、
[root@centos8 data]# ls /data /err |& tr 'a-z' 'A-Z'
[root@centos8 data]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=7.05 ms
QQ邮箱
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
开启“IMAP/SMTP服务”--“生成授权码”--“.mailrc”(放在家目录)
cat .mailrc
set from=xxx@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=xxx@qq.com
set smtp-auth-password=
set smtp-auth=login
set ssl-verify=ignore
[root@centos8 data]# yum -y install mailx
[root@centos8 data]# which mail
/usr/bin/mail
[root@centos8 data]# rpm -qf /usr/bin/mail
mailx-12.5-29.el8.x86_64
[root@centos8 data]# echo warnings | mail -s hello wang@qq.com
[root@centos8 data]# echo warnings | rev
sgninraw
3.2 管道中的-符号
3.3 tee命令
[root@centos8 data]# echo warnings
warnings
[root@centos8 data]# echo warnings | tee -a /data/test.log
warnings
[root@centos8 data]# cat test.log
warnings
[root@centos8 data]# echo warnings1 | tee -a /data/test.log
warnings1
[root@centos8 data]# cat test.log
warnings
warnings1
[root@centos8 data]# cat <
4 练习