4. 第三章Linux文件管理和IO重定向
一.第一部分
1、显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录
[root@centos8 ~]# ls -d /etc/l*[[:digit]]*[[:lower]]
2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录
[root@centos8 ~]# ls -d /etc/[0-9]*[^0-9]
3、显示/etc目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录
[root@centos8 ~]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
4、显示/etc目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录
[root@centos8 ~]# ll /etc/rc[0-6]*
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 May 11 2019 /etc/rc6.d -> rc.d/rc6.d
5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录
[root@centos8 ~]# ll /etc/[mnrp]*.conf
-rw-r--r--. 1 root root 5165 May 11 2019 /etc/man_db.conf
-rw-r--r--. 1 root root 1108 Apr 24 2020 /etc/mke2fs.conf
-rw-r--r--. 1 root root 2620 May 11 2019 /etc/mtools.conf
-rw-r--r--. 1 root root 1042 Apr 24 2020 /etc/nfs.conf
-rw-r--r--. 1 root root 3606 Apr 24 2020 /etc/nfsmount.conf
lrwxrwxrwx. 1 root root 29 Nov 13 18:01 /etc/nsswitch.conf -> /etc/authselect/nsswitch.conf
-rw-r--r--. 1 root root 1362 May 14 2019 /etc/pbm2ppa.conf
-rw-r--r--. 1 root root 6300 May 14 2019 /etc/pnm2ppa.conf
-rw-r--r--. 1 root root 433 Apr 7 2020 /etc/radvd.conf
-rw-r--r--. 1 root root 1787 May 11 2019 /etc/request-key.conf
-rw-r--r--. 1 root root 82 Nov 19 14:19 /etc/resolv.conf
-rw-r--r--. 1 root root 3185 Apr 24 2020 /etc/rsyslog.conf
6、只显示/root下的隐藏文件和目录
[root@centos8 ~]# ls -d /root/.*
/root/. /root/.bash_logout /root/.cache /root/.dbus /root/.Xauthority
/root/.. /root/.bash_profile /root/.config /root/.esd_auth
/root/.bash_history /root/.bashrc /root/.cshrc /root/.tcshrc
7、只显示/etc下的非隐藏目录
[root@centos8 ~]# ls -d /etc/*/
/etc/alsa/ /etc/fonts/ /etc/ndctl/ /etc/rsyslog.d/
/etc/alternatives/ /etc/fwupd/ /etc/NetworkManager/ /etc/rwtab.d/
/etc/audit/ /etc/gconf/ /etc/nftables/ /etc/samba/
/etc/authselect/ /etc/gcrypt/ /etc/oddjob/ /etc/sane.d/
/etc/avahi/ /etc/gdm/ /etc/oddjobd.conf.d/ /etc/sasl2/
/etc/bash_completion.d/ /etc/geoclue/ /etc/openldap/ /etc/security/
/etc/binfmt.d/ /etc/glvnd/ /etc/opt/ /etc/selinux/
/etc/bluetooth/ /etc/gnupg/ /etc/ostree/ /etc/setroubleshoot/
/etc/brltty/ /etc/groff/ /etc/PackageKit/ /etc/sgml/
/etc/chkconfig.d/ /etc/grub.d/ /etc/pam.d/ /etc/skel/
/etc/chromium/ /etc/gss/ /etc/pipewire/ /etc/smartmontools/
/etc/cifs-utils/ /etc/gssproxy/ /etc/pkcs11/ /etc/speech-dispatcher/
/etc/cni/ /etc/hp/ /etc/pki/ /etc/ssh/
/etc/cockpit/ /etc/init.d/ /etc/plymouth/ /etc/ssl/
/etc/containers/ /etc/iproute2/ /etc/pm/ /etc/sssd/
/etc/cron.d/ /etc/iscsi/ /etc/polkit-1/ /etc/sudoers.d/
/etc/cron.daily/ /etc/issue.d/ /etc/popt.d/ /etc/sysconfig/
/etc/cron.hourly/ /etc/kernel/ /etc/postfix/ /etc/sysctl.d/
/etc/cron.monthly/ /etc/krb5.conf.d/ /etc/prelink.conf.d/ /etc/systemd/
/etc/cron.weekly/ /etc/ld.so.conf.d/ /etc/profile.d/ /etc/terminfo/
/etc/crypto-policies/ /etc/libblockdev/ /etc/pulse/ /etc/tmpfiles.d/
/etc/cups/ /etc/libibverbs.d/ /etc/qemu-ga/ /etc/tuned/
/etc/cupshelpers/ /etc/libnl/ /etc/qemu-kvm/ /etc/udev/
/etc/dbus-1/ /etc/libpaper.d/ /etc/ras/ /etc/udisks2/
/etc/dconf/ /etc/libreport/ /etc/rc0.d/ /etc/unbound/
/etc/default/ /etc/libssh/ /etc/rc1.d/ /etc/UPower/
/etc/depmod.d/ /etc/libvirt/ /etc/rc2.d/ /etc/vmware-tools/
/etc/dhcp/ /etc/logrotate.d/ /etc/rc3.d/ /etc/wpa_supplicant/
/etc/dnf/ /etc/lsm/ /etc/rc4.d/ /etc/X11/
/etc/dnsmasq.d/ /etc/lvm/ /etc/rc5.d/ /etc/xdg/
/etc/dracut.conf.d/ /etc/mcelog/ /etc/rc6.d/ /etc/xinetd.d/
/etc/egl/ /etc/microcode_ctl/ /etc/rc.d/ /etc/xml/
/etc/exports.d/ /etc/modprobe.d/ /etc/rdma/ /etc/yum/
/etc/firefox/ /etc/modules-load.d/ /etc/request-key.d/ /etc/yum.repos.d/
/etc/firewalld/ /etc/motd.d/ /etc/rhsm/
/etc/flatpak/ /etc/multipath/ /etc/rpm/
二.第二部分
1、每天将/etc目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为backupYYYY-mm-dd,备份过程可见
[root@centos8 ~]# cp -rv /etc /data/backup`date +%F`
...
[root@centos8 ~]# ll /data
total 12
drwxr-xr-x. 136 root root 8192 Nov 19 15:04 backup2020-11-19
2、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限
[root@centos8 ~]# mkdir /data/rootdir
[root@centos8 ~]# cp -a /root/ /data/rootdir/
[root@centos8 ~]# ll /data/rootdir/
total 4
dr-xr-x---. 5 root root 4096 Nov 19 14:41 root
三.第三部分
1、如果创建/testdir/dir1/x,/testdir/dir1/y,/testdir/dir1/x/a,/testdir/dir1/x/b,/testdir/dir1/y/a,/testdir/dir1/y/b
[root@centos8 ~]# mkdir -p testdir/dir1/{x,y}/{a,b}
[root@centos8 ~]# tree testdir/dir1
testdir/dir1
├── x
│ ├── a
│ └── b
└── y
├── a
└── b
6 directories, 0 files
2、如何创建/testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a,/testdir/dir2/x/b
[root@centos8 ~]# mkdir -pv testdir/dir2/{x/{a,b},y}
mkdir: created directory 'testdir'
mkdir: created directory 'testdir/dir2'
mkdir: created directory 'testdir/dir2/x'
mkdir: created directory 'testdir/dir2/x/a'
mkdir: created directory 'testdir/dir2/x/b'
mkdir: created directory 'testdir/dir2/y'
[root@centos8 ~]# tree testdir/dir2
testdir/dir2
├── x
│ ├── a
│ └── b
└── y
4 directories, 0 files
3、如何创建/testdir/dir3,/testdir/dir4,/testdir/dir5,/testdir/dir5/dir6,/testdir/dir5/dir7
[root@centos8 ~]# mkdir -pv testdir/dir{3,4,5/dir{6,7}}
mkdir: created directory 'testdir/dir3'
mkdir: created directory 'testdir/dir4'
mkdir: created directory 'testdir/dir5'
mkdir: created directory 'testdir/dir5/dir6'
mkdir: created directory 'testdir/dir5/dir7'
[root@centos8 ~]# tree testdir/
testdir/
├── dir1
│ ├── x
│ │ ├── a
│ │ └── b
│ └── y
│ ├── a
│ └── b
├── dir2
│ ├── x
│ │ ├── a
│ │ └── b
│ └── y
├── dir3
├── dir4
└── dir5
├── dir6
└── dir7
17 directories, 0 files
四.第四部分
1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@centos8 ~]# cat /etc/issue
Hostname is \n
Today is \d
Terminal is \l
==================
\S
Kernel \r on an \m
[root@centos8 ~]# tr 'a-z' 'A-Z' </etc/issue
HOSTNAME IS \N
TODAY IS \D
TERMINAL IS \L
==================
\S
KERNEL \R ON AN \M
[root@centos8 ~]# tr 'a-z' 'A-Z' </etc/issue >/tmp/issue.out
[root@centos8 ~]# cat /tmp/issue.out
HOSTNAME IS \N
TODAY IS \D
TERMINAL IS \L
==================
\S
KERNEL \R ON AN \M
2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@centos8 ~]# who
root pts/0 2020-11-19 15:02 (10.0.0.1)
[root@centos8 ~]# who |tr 'a-z' 'A-Z' >/tmp/who.out
[root@centos8 ~]# cat /tmp/who.out
ROOT PTS/0 2020-11-19 15:02 (10.0.0.1)
3、一个linux用户给root发邮件,需求邮件标题为“help”,邮件正文如下:
Hello,i am 用户名,The system version is here,please help me ro check it,thanks!
操作系统版本信息
[root@centos8 ~]# su neteagle
[neteagle@centos8 ~]$ mail -s help root <<EOF
\> Hello,i am `whoami`,The system version is here,please help me ro check it,thanks!
\> `cat /etc/redhat-release`
\> EOF
[neteagle@centos8 root]$ exit
exit
You have mail in /var/spool/mail/root
[root@centos8 ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
\>N 1 neteagle Thu Nov 19 16:09 19/730 "help"
& n
Message 1:
From neteagle@centos8.neteagles.cn Thu Nov 19 16:09:55 2020
Return-Path: <neteagle@centos8.neteagles.cn>
X-Original-To: root
Delivered-To: root@centos8.neteagles.cn
Date: Thu, 19 Nov 2020 16:09:55 +0800
To: root@centos8.neteagles.cn
Subject: help
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: neteagle <neteagle@centos8.neteagles.cn>
Status: R
Hello,i am neteagle,The system version is here,please help me ro check it,thanks!
CentOS Linux release 8.2.2004 (Core)
& q
Held 1 message in /var/spool/mail/root
4、将/root下文件列表,显示成一行,并文件名之间用空格隔开
[root@centos8 ~]# ls /root |tr '\t' ' '
112.txt
1.txt
a.log
anaconda-ks.cfg
a.sh
b.log
f1.txt
initial-setup-ks.cfg
ls.log
mail.txt
seq.log
testdir
win2.txt
win.txt
5、计算1+2+3+...+99+100的总和
[root@centos8 ~]#echo {1..100}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
[root@centos8 ~]#echo {1..100} | tr ' ' +
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[root@centos8 ~]#echo {1..100} | tr ' ' + | bc
5050
[root@centos8 ~]#seq -s + 1 100 | bc
5050
6、删除Windows文本文件中的回车字符,即“\r”
[root@centos8 ~]#cat windows.txt
a
b
c[root@centos8 ~]#file windows.txt
windows.txt: ASCII text, with CRLF line terminators
[root@centos8 ~]#hexdump -C windows.txt
00000000 61 0d 0a 62 0d 0a 63 |a..b..c|
00000007
[root@centos8 ~]#nano linux.txt
a
a
c
[root@centos8 ~]#hexdump -C linux.txt
00000000 61 0a 62 0a 63 0a |a.b.c.|
00000006
[root@centos8 ~]#tr -d '\r' < windows.txt >windows2.txt
[root@centos8 ~]#cat windows2.txt
a
b
c[root@centos8 ~]#file windows2.txt
windows2.txt: ASCII text
[root@centos8 ~]#hexdump -C windows2.txt
00000000 61 0a 62 0a 63 |a.b.c|
00000005
7、处理字符串“xt,.l 1 jr#!$mn 2c*/fe 3 uz 4”,只保留其中的数字和空格
[root@centos8 ~]# echo "xt,.l 1 jr#'a-j'mn 2c*/fe 3 uz 4" |tr -d '[[:alpha:]][[:punct:]]'
1 2 3 4
8、将PATH变量每个目录显示在独立的一行
[root@centos8 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos8 ~]# echo $PATH |tr -s ':' '\n'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
9、将指定文件中0-9分别替代称a-j
[root@centos8 ~]# echo 0123456789| tr '0-9' 'a-j'
abcdefghij
10、将文件/etc/centos-release中每个单词(由字母组成)显示在独立一行,并无空行
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@centos8 ~]# cat /etc/redhat-release | tr -s ' ' '\n'
CentOS
Linux
release
8.2.2004
(Core)