Shell基本命令练习
1、显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表(提前创建符合特征文件或目录)
[14:27:16 root@cocky86 etc]$ls -d /etc/l*[0-9]*[a-z]
/etc/l1bc /etc/l345abc /etc/labc123abc
2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
[14:39:26 root@cocky86 /]$ls -d /etc/[0-9]*[^0-9]
/etc/123ab /etc/123abc.t /etc/456abc1.t
3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表
[14:40:52 root@cocky86 /]$ls -d /etc/[^a-zA-Z]*[a-zA-Z]*
/etc/123ab /etc/123abc.t /etc/456abc1.t
4、显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表
[14:43:56 root@cocky86 /]$ls /etc/rc[0-6]*
/etc/rc0.d:
/etc/rc1.d:
/etc/rc2.d:
/etc/rc3.d:
/etc/rc4.d:
/etc/rc5.d:
/etc/rc6.d:
##
[14:44:42 root@cocky86 /]$ls /etc/rc[{0..6}]*
/etc/rc0.d:
/etc/rc1.d:
/etc/rc2.d:
/etc/rc3.d:
/etc/rc4.d:
/etc/rc5.d:
/etc/rc6.d:
##
[14:45:01 root@cocky86 /]$ls /etc/rc{0..6}*
/etc/rc0.d:
/etc/rc1.d:
/etc/rc2.d:
/etc/rc3.d:
/etc/rc4.d:
/etc/rc5.d:
/etc/rc6.d:
5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录列表
[14:45:18 root@cocky86 /]$ls /etc/[m,n,r,p]*.conf
/etc/man_db.conf /etc/mtools.conf /etc/nfsmount.conf /etc/pbm2ppa.conf /etc/request-key.conf /etc/rsyslog.conf
/etc/mke2fs.conf /etc/nfs.conf /etc/nsswitch.conf /etc/pnm2ppa.conf /etc/resolv.conf
6、只显示/root下的隐藏文件和目录列表
[14:51:41 root@cocky86 /]$ls -d root/.*
root/. root/.bash_logout root/.bashrc root/.certs root/.cshrc root/.lesshst root/.viminfo
root/.. root/.bash_profile root/.cache root/.config root/.dbus root/.tcshrc root/.Xauthority
7、只显示/etc下的非隐藏目录列表
[14:55:35 root@cocky86 /]$ls -d /etc/*/
/etc/123ab/ /etc/dnf/ /etc/l345abc/ /etc/pkcs11/ /etc/sgml/
/etc/accountsservice/ /etc/dnsmasq.d/ /etc/ld.so.conf.d/ /etc/pki/ /etc/skel/
/etc/alsa/ /etc/dracut.conf.d/ /etc/libblockdev/ /etc/plymouth/ /etc/smartmontools/
/etc/alternatives/ /etc/egl/ /etc/libibverbs.d/ /etc/pm/ /etc/sos/
/etc/anaconda/ /etc/exports.d/ /etc/libnl/ /etc/polkit-1/ /etc/speech-dispatcher/
/etc/audit/ /etc/firefox/ /etc/libpaper.d/ /etc/popt.d/ /etc/ssh/
/etc/authselect/ /etc/firewalld/ /etc/libreport/ /etc/postfix/ /etc/ssl/
/etc/avahi/ /etc/flatpak/ /etc/libssh/ /etc/profile.d/ /etc/sssd/
/etc/bash_completion.d/ /etc/fonts/ /etc/libvirt/ /etc/pulse/ /etc/sudoers.d/
/etc/binfmt.d/ /etc/fwupd/ /etc/logrotate.d/ /etc/qemu-ga/ /etc/sysconfig/
/etc/bluetooth/ /etc/gconf/ /etc/lsm/ /etc/qemu-kvm/ /etc/sysctl.d/
/etc/brltty/ /etc/gcrypt/ /etc/lvm/ /etc/ras/ /etc/systemd/
/etc/chkconfig.d/ /etc/gdm/ /etc/mcelog/ /etc/rc0.d/ /etc/terminfo/
/etc/chromium/ /etc/geoclue/ /etc/mdevctl.d/ /etc/rc1.d/ /etc/tmpfiles.d/
/etc/cifs-utils/ /etc/glvnd/ /etc/microcode_ctl/ /etc/rc2.d/ /etc/tuned/
/etc/cni/ /etc/gnupg/ /etc/modprobe.d/ /etc/rc3.d/ /etc/udev/
/etc/cockpit/ /etc/groff/ /etc/modules-load.d/ /etc/rc4.d/ /etc/udisks2/
/etc/containers/ /etc/grub.d/ /etc/motd.d/ /etc/rc5.d/ /etc/unbound/
/etc/cron.d/ /etc/gss/ /etc/multipath/ /etc/rc6.d/ /etc/UPower/
/etc/cron.daily/ /etc/gssproxy/ /etc/ndctl/ /etc/rc.d/ /etc/vmware-tools/
/etc/cron.hourly/ /etc/hp/ /etc/NetworkManager/ /etc/request-key.d/ /etc/wpa_supplicant/
/etc/cron.monthly/ /etc/init.d/ /etc/nftables/ /etc/rhsm/ /etc/X11/
/etc/cron.weekly/ /etc/initial-setup/ /etc/nvme/ /etc/rpm/ /etc/xdg/
/etc/crypto-policies/ /etc/iproute2/ /etc/oddjob/ /etc/rsyslog.d/ /etc/xinetd.d/
/etc/cups/ /etc/iscsi/ /etc/oddjobd.conf.d/ /etc/rwtab.d/ /etc/xml/
/etc/cupshelpers/ /etc/issue.d/ /etc/openldap/ /etc/samba/ /etc/yum/
/etc/dbus-1/ /etc/kdump/ /etc/opt/ /etc/sane.d/ /etc/yum.repos.d/
/etc/dconf/ /etc/kernel/ /etc/ostree/ /etc/sasl2/
/etc/default/ /etc/keyutils/ /etc/PackageKit/ /etc/security/
/etc/depmod.d/ /etc/krb5.conf.d/ /etc/pam.d/ /etc/selinux/
/etc/dhcp/ /etc/l1bc/ /etc/pipewire/ /etc/setroubleshoot/
8、将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为 backupYYYY-mm-dd,备份过程可见
[15:53:54 root@cocky86 data]$ mkdir -p "/data/backup$(date +%Y-%m-%d)"&& cp -Rv /etc/* "/data/backup$(date +%Y-%m-%d)"
'/etc/123ab' -> '/data/backup2023-07-29/123ab'
'/etc/123ab/123abcdefg.' -> '/data/backup2023-07-29/123ab/123abcdefg.'
'/etc/123abc.t' -> '/data/backup2023-07-29/123abc.t'
'/etc/456abc1.t' -> '/data/backup2023-07-29/456abc1.t'
'/etc/accountsservice' -> '/data/backup2023-07-29/accountsservice'
'/etc/accountsservice/user-templates' -> '/data/backup2023-07-29/accountsservice/user-templates'
'/etc/adjtime' -> '/data/backup2023-07-29/adjtime'
'/etc/aliases' -> '/data/backup2023-07-29/aliases'
'/etc/alsa' -> '/data/backup2023-07-29/alsa'
'/etc/alsa/state-daemon.conf' -> '/data/backup2023-07-29/alsa/state-daemon.conf'
'/etc/alsa/alsactl.conf' -> '/data/backup2023-07-29/alsa/alsactl.conf'
'/etc/alsa/conf.d' -> '/data/backup2023-07-29/alsa/conf.d'
'/etc/alsa/conf.d/50-pulseaudio.conf' -> '/data/backup2023-07-29/alsa/conf.d/50-pulseaudio.conf'
'/etc/alsa/conf.d/99-pulseaudio-default.conf' -> '/data/backup2023-07-29/alsa/conf.d/99-pulseaudio-default.conf'
'/etc/alternatives' -> '/data/backup2023-07-29/alternatives'
'/etc/alternatives/libnssckbi.so.x86_64' -> '/data/backup2023-07-29/alternatives/libnssckbi.so.x86_64'
9、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有属性
[15:59:04 root@cocky86 data]$mkdir -p /data/rootdir && cp -Rpv /root/* /data/rootdir
'/root/anaconda-ks.cfg' -> '/data/rootdir/anaconda-ks.cfg'
'/root/data' -> '/data/rootdir/data'
'/root/data/test.sh' -> '/data/rootdir/data/test.sh'
'/root/dead.letter' -> '/data/rootdir/dead.letter'
'/root/initial-setup-ks.cfg' -> '/data/rootdir/initial-setup-ks.cfg'
10、将 /etc/passwd 文件中的内容转换为大写后重定向保存至/tmp/passwd.out文件中
[16:27:36 root@cocky86 /]$cat /etc/passwd | tr [a-z] [A-Z] >/tmp/passwd.out
[16:27:41 root@cocky86 /]$cat /tmp/passwd.out
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
SYNC:X:5:0:SYNC:/SBIN:/BIN/SYNC
10、使用echo在m55.txt中输入自己名字的拼音,并使用cat查看
[16:29:54 root@cocky86 /]$echo "xushuang">m55.txt
[16:29:59 root@cocky86 /]$cat m55.txt
xushuang
11、在m55.txt中使用echo继续输入当前时间的数字且不覆盖之前内容,并使用cat查看
[16:30:06 root@cocky86 /]$echo $(date +%Y-%m-%d" "%H:%M:%S) >> m55.txt
[16:39:57 root@cocky86 /]$cat m55.txt
xushuang
2023-07-29 16:39:57