3.檔案管理-案例分析
阿新 • • 發佈:2020-08-07
##1.顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的檔案或目錄
[root@localhost var]# mkdir l009z [root@localhost var]# touch l999a.txt [root@localhost var]# ll total 8 drwxr-xr-x. 2 root root 6 Apr 11 2018 adm drwxr-xr-x. 5 root root 44 Aug 3 17:03 cache drwxr-xr-x. 2 root root 6 Nov 5 2018 crash drwxr-xr-x. 3 root root 34 Aug 3 17:03 db drwxr-xr-x. 3 root root 18 Aug 3 17:03 empty drwxr-xr-x. 2 root root 6 Apr 11 2018 games drwxr-xr-x. 2 root root 6 Apr 11 2018 gopher drwxr-xr-x. 3 root root 18 Aug 3 17:02 kerberos drwxr-xr-x. 2 root root 6 Aug 7 14:28 l009z -rw-r--r--. 1 root root 0 Aug 7 14:29 l999a.txt drwxr-xr-x. 24 root root 4096 Aug 3 17:14 lib drwxr-xr-x. 2 root root 6 Apr 11 2018 local lrwxrwxrwx. 1 root root 11 Aug 3 17:02 lock -> ../run/lock drwxr-xr-x. 6 root root 4096 Aug 4 03:21 log lrwxrwxrwx. 1 root root 10 Aug 3 17:02 mail -> spool/mail drwxr-xr-x. 2 root root 6 Apr 11 2018 nis drwxr-xr-x. 2 root root 6 Apr 11 2018 opt drwxr-xr-x. 2 root root 6 Apr 11 2018 preserve lrwxrwxrwx. 1 root root 6 Aug 3 17:02 run -> ../run drwxr-xr-x. 8 root root 87 Aug 3 17:03 spool drwxrwxrwt. 3 root root 43 Aug 7 11:25 tmp drwxr-xr-x. 2 root root 6 Apr 11 2018 yp [root@localhost var]# ls -d /var/l*[[:digit:]]*[[:lower:]] /var/l009z /var/l999a.txt
##2.顯示/etc目錄下以任意一位數字開頭,且以非數字結尾的檔案或目錄
[root@localhost etc]# touch 999z.txt
[root@localhost etc]# mkdir 999z
[root@localhost etc]# ls [[:digit:]]*[^[:digit:]]
999z.txt
999z:
##3.顯示/etc/目錄下以非字母開頭,後面跟了一個字母及其它任意長度任意字元的檔案或目錄
[root@localhost etc]# touch 9a9b.txt [root@localhost etc]# mkdir 9a9b [root@localhost etc]# ls [^[:alpha:]][[:alpha:]]* 9a9b.txt 9a9b:
##4.顯示/etc/目錄下所有以rc開頭,並後面是0-6之間的數字,其它為任意字元的檔案或目錄
[root@localhost etc]# ls rc[0-6]* rc0.d: K50netconsole K90network rc1.d: K50netconsole K90network rc2.d: K50netconsole S10network rc3.d: K50netconsole S10network rc4.d: K50netconsole S10network rc5.d: K50netconsole S10network rc6.d: K50netconsole K90network
##5.顯示/etc目錄下,所有以.d結尾的檔案或目錄
[root@localhost etc]# ls *.d
bash_completion.d:
iprutils
binfmt.d:
chkconfig.d:
cron.d:
0hourly
depmod.d:
dist.conf
dracut.conf.d:
grub.d:
00_header 00_tuned 01_users 10_linux 20_linux_xen 20_ppc_terminfo 30_os-prober 40_custom 41_custom README
init.d:
functions netconsole network README
krb5.conf.d:
ld.so.conf.d:
kernel-3.10.0-957.el7.x86_64.conf mariadb-x86_64.conf
logrotate.d:
bootlog syslog wpa_supplicant yum
modprobe.d:
dccp-blacklist.conf firewalld-sysctls.conf tuned.conf
modules-load.d:
my.cnf.d:
mysql-clients.cnf
pam.d:
chfn crond login password-auth postlogin runuser smartcard-auth smtp.postfix sudo system-auth vlock
chsh fingerprint-auth other password-auth-ac postlogin-ac runuser-l smartcard-auth-ac sshd sudo-i system-auth-ac vmtoolsd
config-util fingerprint-auth-ac passwd polkit-1 remote screen smtp su su-l systemd-user
popt.d:
prelink.conf.d:
fipscheck.conf grub2.conf nss-softokn-prelink.conf
profile.d:
256term.csh colorgrep.csh colorls.csh csh.local lang.sh less.sh vim.csh which2.csh
256term.sh colorgrep.sh colorls.sh lang.csh less.csh sh.local vim.sh which2.sh
rc0.d:
K50netconsole K90network
rc1.d:
K50netconsole K90network
rc2.d:
K50netconsole S10network
rc3.d:
K50netconsole S10network
rc4.d:
K50netconsole S10network
rc5.d:
K50netconsole S10network
rc6.d:
K50netconsole K90network
rc.d:
init.d rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local
rsyslog.d:
listen.conf
rwtab.d:
logrotate
statetab.d:
sudoers.d:
sysctl.d:
99-sysctl.conf
tmpfiles.d:
xinetd.d:
yum.repos.d:
CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo
##6.顯示/etc目錄下,所有.conf結尾,且以m,n,r,p開頭的檔案或目錄
[root@localhost etc]# ls [m,n,r,p]*.conf
man_db.conf mke2fs.conf nsswitch.conf resolv.conf rsyslog.conf
##7.只顯示/root下的隱藏檔案和目錄
[root@localhost ~]# ls -d /root/.*
/root/. /root/.. /root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc /root/.cshrc /root/.lesshst /root/.tcshrc /root/.viminfo /root/.vimrc
##8.只顯示/etc下的非隱藏目錄
[root@localhost etc]# ls /etc/[^.]*/ -d
/etc/9a9b/ /etc/cron.weekly/ /etc/gnupg/ /etc/modprobe.d/ /etc/polkit-1/ /etc/rc4.d/ /etc/ssh/ /etc/vmware-tools/
/etc/alternatives/ /etc/dbus-1/ /etc/groff/ /etc/modules-load.d/ /etc/popt.d/ /etc/rc5.d/ /etc/ssl/ /etc/wpa_supplicant/
/etc/audisp/ /etc/default/ /etc/grub.d/ /etc/my.cnf.d/ /etc/postfix/ /etc/rc6.d/ /etc/statetab.d/ /etc/X11/
/etc/audit/ /etc/depmod.d/ /etc/gss/ /etc/NetworkManager/ /etc/ppp/ /etc/rc.d/ /etc/sudoers.d/ /etc/xdg/
/etc/bash_completion.d/ /etc/dhcp/ /etc/init.d/ /etc/openldap/ /etc/prelink.conf.d/ /etc/rpm/ /etc/sysconfig/ /etc/xinetd.d/
/etc/binfmt.d/ /etc/dracut.conf.d/ /etc/iproute2/ /etc/opt/ /etc/profile.d/ /etc/rsyslog.d/ /etc/sysctl.d/ /etc/yum/
/etc/chkconfig.d/ /etc/egl/ /etc/kernel/ /etc/pam.d/ /etc/python/ /etc/rwtab.d/ /etc/systemd/ /etc/yum.repos.d/
/etc/cron.d/ /etc/firewalld/ /etc/krb5.conf.d/ /etc/pkcs11/ /etc/rc0.d/ /etc/sasl2/ /etc/terminfo/
/etc/cron.daily/ /etc/fonts/ /etc/ld.so.conf.d/ /etc/pki/ /etc/rc1.d/ /etc/security/ /etc/tmpfiles.d/
/etc/cron.hourly/ /etc/gcrypt/ /etc/libnl/ /etc/plymouth/ /etc/rc2.d/ /etc/selinux/ /etc/tuned/
/etc/cron.monthly/ /etc/glvnd/ /etc/logrotate.d/ /etc/pm/ /etc/rc3.d/ /etc/skel/ /etc/udev/
##9.每天將/etc/目錄下所有檔案,備份到/data獨立的子目錄下,並要求子目錄格式為 backupYYYY-mm-dd,備份過程可見
[root@localhost ~]# cp -av /etc /data/backup`date +%F`
[root@localhost ~]# ll /data/
total 16
drwxr-xr-x. 78 root root 8192 Aug 7 14:42 backup2020-08-07
-rw-r--r--. 1 root root 20 Aug 7 11:41 f1.txt
##10.建立/data/rootdir目錄,並複製/root下所有檔案到該目錄內,要求保留原有許可權
[root@localhost ~]# ll /
total 16
lrwxrwxrwx. 1 root root 7 Aug 3 17:02 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Aug 3 17:13 boot
drwxr-xr-x. 4 root root 59 Aug 7 15:03 data
drwxr-xr-x. 19 root root 3220 Aug 3 17:14 dev
drwxr-xr-x. 78 root root 8192 Aug 7 14:42 etc
drwxr-xr-x. 22 root root 273 Aug 6 17:39 home
lrwxrwxrwx. 1 root root 7 Aug 3 17:02 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Aug 3 17:02 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 136 root root 0 Aug 3 17:14 proc
dr-xr-x---. 2 root root 196 Aug 7 14:22 root
drwxr-xr-x. 24 root root 660 Aug 7 11:30 run
lrwxrwxrwx. 1 root root 8 Aug 3 17:02 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Aug 3 17:14 sys
drwxrwxrwt. 8 root root 226 Aug 7 14:22 tmp
drwxr-xr-x. 13 root root 155 Aug 3 17:02 usr
drwxr-xr-x. 19 root root 267 Aug 7 14:38 var
[root@localhost ~]# ll -a
total 44
dr-xr-x---. 2 root root 196 Aug 7 14:22 .
dr-xr-xr-x. 18 root root 236 Aug 6 13:48 ..
-rw-r--r--. 1 root root 134 Aug 6 15:34 acl.txt
-rw-------. 1 root root 1689 Aug 3 17:13 anaconda-ks.cfg
-rw-------. 1 root root 1284 Aug 4 14:07 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-------. 1 root root 39 Aug 4 14:29 .lesshst
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-------. 1 root root 3996 Aug 7 14:22 .viminfo
-rw-r--r--. 1 root root 550 Aug 7 13:56 .vimrc
[root@localhost ~]# cp -ap /root/ /data/rootdir/
[root@localhost ~]# ll /data/rootdir/
total 0
dr-xr-x---. 2 root root 196 Aug 7 14:22 root
[root@localhost ~]# ll -a /data/rootdir/root/
total 44
dr-xr-x---. 2 root root 196 Aug 7 14:22 .
drwxr-xr-x. 3 root root 18 Aug 7 15:10 ..
-rw-r--r--. 1 root root 134 Aug 6 15:34 acl.txt
-rw-------. 1 root root 1689 Aug 3 17:13 anaconda-ks.cfg
-rw-------. 1 root root 1284 Aug 4 14:07 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-------. 1 root root 39 Aug 4 14:29 .lesshst
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-------. 1 root root 3996 Aug 7 14:22 .viminfo
-rw-r--r--. 1 root root 550 Aug 7 13:56 .vimrc
##11.如何建立/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a,/testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b
[root@localhost data]# mkdir -p /data/testdir/dir1/{x,y}/{a,b}
[root@localhost data]# tree testdir/
testdir/
└── dir1
├── x
│ ├── a
│ └── b
└── y
├── a
└── b
7 directories, 0 files
##12.如何建立/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a,/testdir/dir2/x/b
[root@localhost data]# mkdir -p /data/testdir/dir2/{x/{a,b},y}
[root@localhost data]# tree testdir/
testdir/
├── dir1
│ ├── x
│ │ ├── a
│ │ └── b
│ └── y
│ ├── a
│ └── b
└── dir2
├── x
│ ├── a
│ └── b
└── y
12 directories, 0 files
##13.如何建立/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6,/testdir/dir5/dir7
[root@localhost data]# mkdir -p /data/testdir/dir{3,4,5/{6,7}}
[root@localhost data]# tree testdir/
testdir/
├── dir1
│ ├── x
│ │ ├── a
│ │ └── b
│ └── y
│ ├── a
│ └── b
├── dir2
│ ├── x
│ │ ├── a
│ │ └── b
│ └── y
├── dir3
├── dir4
└── dir5
├── 6
└── 7
17 directories, 0 files