一.第一部分
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)