第三章面試題
第三章 面試練習題
解答論述題
1. Linux 系統中檔案型別一共有幾種,他們分別是什麼?使用哪個命令可以檢視檔案的型別?
Linux 系統中一共有 7 種檔案型別,分別是普通檔案,目錄,符號連結檔案,套接字檔案,管道檔案,塊裝置檔案,字元裝置檔案。
使用 ls -l 命令檢視指定檔案,顯示出檔案資訊的第一個字元代表檔案型別;使用 stat file 的方式檢視檔案的型別,如下:
[root@localhost ~]#
[root@localhost ~]# ls -al /root/anaconda-ks.cfg
-rw-------. 1 root root 902 Dec 21 19:51 /root/anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# stat /root/anaconda-ks.cfg
File: '/root/anaconda-ks.cfg'
Size: 902 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 531984 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-21 19:51:28.645999942 +0800
Modify: 2019-12-21 19:51:28.691999942 +0800
Change: 2019-12-21 19:51:31.573999940 +0800
[root@localhost ~]#
[root@localhost ~]#
-
請在 /root 路徑下建立一個指向 /etc/sysconfig/network-scripts 資料夾的軟連線檔案 netconf。
[root@localhost ~]#
[root@localhost ~]# pwd
/root
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ln -s /etc/sysconfig/network-scripts netconf
[root@localhost ~]#
[root@localhost ~]# ll
total 20
-rw-------. 1 root root 902 Dec 21 19:51 anaconda-ks.cfg
-rw-r--r--. 1 root root 8901 Dec 21 19:51 install.log
-rw-r--r--. 1 root root 3384 Dec 21 19:50 install.log.syslog
lrwxrwxrwx. 1 root root 30 Jan 3 00:25 netconf -> /etc/sysconfig/network-scripts
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
-
在 Linux 系統中使用 mkdir 命令的哪個選項可以在 /data 資料夾不存在的情況下成功建立 /data/mysql/back 資料夾?
使用 -p 選項可以自動建立不存在的父目錄,使用 -v 選項可以顯示建立過程,如下:
[root@localhost ~]#
[root@localhost ~]# ls -al /data
ls: cannot access /data: No such file or directory
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# mkdir -pv /data/mysql/back
mkdir: created directory '/data'
mkdir: created directory '/data/mysql'
mkdir: created directory '/data/mysql/back'
[root@localhost ~]#
[root@localhost ~]#
-
請寫出建立資料夾許可權為 777 的 /data/magedu 資料夾命令?
[root@localhost ~]#
[root@localhost ~]# mkdir -pv -m 777 /data/magedu
mkdir: created directory '/data/magedu'
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ls -adl /data/magedu
drwxrwxrwx. 2 root root 4096 Jan 3 00:29 /data/magedu
[root@localhost ~]#
[root@localhost ~]#
-
使用 cp 命令拷貝檔案時,使用哪個選項可以連原始檔的許可權資訊一起拷貝?
使用 cp 命令的 preserve 選項可以指定在複製過程中需要一起復制的元資料:
[root@localhost ~]# ll
total 20
-rw-------. 1 root root 902 Dec 21 19:51 anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# chmod 777 anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ll
total 20
-rwxrwxrwx. 1 root root 902 Dec 21 19:51 anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cp anaconda-ks.cfg file1
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ll
total 24
-rwxrwxrwx. 1 root root 902 Dec 21 19:51 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 902 Jan 3 00:33 file1
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cp --preserve=mode anaconda-ks.cfg file2
[root@localhost ~]#
[root@localhost ~]# ll
total 28
-rwxrwxrwx. 1 root root 902 Dec 21 19:51 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 902 Jan 3 00:33 file1
-rwxrwxrwx. 1 root root 902 Jan 3 00:34 file2
[root@localhost ~]#
[root@localhost ~]#
-
請說出 cd ~ , cd - , cd 三個命令的作用和區別?
cd 命令用於切換當前工作目錄。cd 後跟 ~ 符號代表切換到家目錄;cd 後跟 - 符號代表切換到上一次切換的當前路徑;cd 後不新增任何路徑,
功能和 ~ 一樣,用於切換到當前使用者的家目錄。
-
如何使用檔案的 inode 編號刪除該檔案?
[root@localhost ~]#
[root@localhost ~]# pwd
/root
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ls -i anaconda-ks.cfg
531984 anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# find /root -inum 531984
/root/anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# find /root -inum 531984 -delete
[root@localhost ~]#
[root@localhost ~]# ll
total 0
[root@localhost ~]#
-
Linux 系統中如何給一個檔案修改名稱,rename 命令的作用是什麼?
Linux 系統中使用 mv 命令可以給檔案修改名稱,如下:
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 4 Jan 3 00:55 anaconda-ks.cfg
[root@localhost ~]#
[root@localhost ~]# mv anaconda-ks.cfg newfile
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 4 Jan 3 00:55 newfile
[root@localhost ~]#
[root@localhost ~]#
rename 也用於檔名的修改,rename 後由三個欄位組成,第一個表示將哪些字元資訊內容進行修改,第二個
欄位表示將要改成的具體內容,第三個欄位表示要修改哪個檔案,演示如下:
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 4 Jan 3 00:55 myfile
[root@localhost ~]#
[root@localhost ~]# rename myfile file myfile
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 4 Jan 3 00:55 file
[root@localhost ~]#
-
使用 ls 命令檢視資料夾時,檢視到的結果為指定資料夾內包含的檔案資訊,請問如何檢視指定資料夾自身的屬性資訊?
使用 ls 命令時指定 -d 選項即可:
[root@localhost ~]#
[root@localhost ~]# ls -al /root
total 36
dr-xr-x---. 2 root root 4096 Jan 3 01:12 .
dr-xr-xr-x. 23 root root 4096 Jan 3 00:26 ..
-rw-------. 1 root root 12 Dec 21 19:53 .bash_history
-rw-r--r--. 1 root root 18 May 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 May 20 2009 .bash_profile
-rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc
-rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc
-rw-r--r--. 1 root root 4 Jan 3 00:55 file
-rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc
[root@localhost ~]#
[root@localhost ~]# ls -ald /root
dr-xr-x---. 2 root root 4096 Jan 3 01:12 /root
[root@localhost ~]#
-
軟連線和硬連結的區別,使用 cp 命令複製檔案和建立硬連結檔案有什麼區別?
符號 (或軟) 連結是 Linux 的一種檔案型別,它的 inode 指向的資料塊中儲存的是一個要指向檔案的檔案系統路徑。
硬連結是目錄檔案中的一條記錄,他的 inode 編號和對應檔案的 inode 檔案相同,每個檔案有多少個硬連結,他的
inode 引用記錄次數就會增加多少。正式因為硬連結是基於檔案的 inode 編號的,所以他不能跨磁碟分割槽。當刪除硬
連結檔案時,除了刪除他在指定資料夾中的記錄資訊,同時也要減少該檔案 inode 引用計數,直到該值為 0 時才刪除
該檔案。不能給資料夾建立硬連結檔案。cp 命令進行復制檔案時,會在磁碟上重新分配一個 inode,同時這個 inode
也會指向一個新的磁碟資料區域用來儲存檔案中的資料,和硬連結有根本的區別。
-
描述 /etc/rc.local 和 /var/log/messages 兩檔案內容是做什麼的?
/etc/rc.local 檔案是由 initscripts 軟體包提供,在 Linux 系統啟動流程後期被執行的 shell 指令碼檔案,我們可以在該檔案中新增一些功能和
邏輯,以此來實現開機後自動執行程式的目的。/var/log/messages 檔案用於儲存 Linux 系統啟動日誌,mail,cron,daemon,kern,auth 等內
容。
-
/root 有三個檔案,名稱分別為 ctestfile ctestdir ctestdisk ,請使用一條命令將三個檔名中的 test 字元替換成 good?
使用 rename 命令可以完成,如下:
[root@localhost ~]#
[root@localhost ~]# ll
total 12
-rw-r--r--. 1 root root 3 Jan 3 19:04 ctestdir
-rw-r--r--. 1 root root 3 Jan 3 19:04 ctestdisk
-rw-r--r--. 1 root root 3 Jan 3 19:04 ctestfile
[root@localhost ~]#
[root@localhost ~]# rename test good c*
[root@localhost ~]#
[root@localhost ~]# ll
total 12
-rw-r--r--. 1 root root 3 Jan 3 19:04 cgooddir
-rw-r--r--. 1 root root 3 Jan 3 19:04 cgooddisk
-rw-r--r--. 1 root root 3 Jan 3 19:04 cgoodfile
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
-
df du 命令的作用分別是什麼?
df 命令用於檢視磁碟使用資訊情況,du 命令用於檢視檔案的磁碟空間佔用情況:
[root@localhost ~]#
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 17413948 759268 15763444 5% /
tmpfs 501508 0 501508 0% /dev/shm
/dev/sda1 999320 27020 919872 3% /boot
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# du -h /etc/
4.0K /etc/sudoers.d
4.0K /etc/cron.weekly
4.0K /etc/gnupg
.
. 省略部分輸出
.
4.0K /etc/selinux/targeted/logins
8.1M /etc/selinux/targeted/policy
304K /etc/selinux/targeted/contexts/files
28K /etc/selinux/targeted/contexts/users
388K /etc/selinux/targeted/contexts
22M /etc/selinux/targeted
22M /etc/selinux
27M /etc/
[root@localhost ~]#
-
某個檔案佔用了過多磁碟空間,rm 刪掉之後發現空間並沒釋放,是什麼原因?如何解決?
當有程序佔用該檔案時,即使刪除該檔案,空間也不會釋放,這時可以考慮先將該檔案資料清空然後再刪除。
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 4 Jan 3 19:13 file
[root@localhost ~]#
[root@localhost ~]# > file
[root@localhost ~]#
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Jan 3 19:14 file
[root@localhost ~]#
[root@localhost ~]# rm -rf file
[root@localhost ~]#
[root@localhost ~]#
-
如何分別檢視到 /root 目錄下的隱藏檔案和非隱藏檔案
ls 命令預設情況下不會顯示要檢視目錄下的以 . 號開頭的檔案,我們習慣上將這種以 . 號開頭的檔案稱為
隱藏檔案。當使用 -a 選項後,ls 命令即可顯示所謂的隱藏檔案
[root@localhost ~]#
[root@localhost ~]# \ls
file
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# \ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cshrc file .tcshrc
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# find /root -name ".*" -exec ls -al {} ;
-rw-r--r--. 1 root root 129 Dec 4 2004 /root/.tcshrc
-rw-r--r--. 1 root root 176 May 20 2009 /root/.bash_profile
-rw-r--r--. 1 root root 18 May 20 2009 /root/.bash_logout
-rw-r--r--. 1 root root 100 Sep 23 2004 /root/.cshrc
-rw-------. 1 root root 12 Dec 21 19:53 /root/.bash_history
-rw-r--r--. 1 root root 176 Sep 23 2004 /root/.bashrc
[root@localhost ~]#
[root@localhost ~]#
-
顯示/etc目錄下,所有.conf結尾,且以m,n,r,p開頭的檔案或目錄
[root@localhost ~]#
[root@localhost ~]# ls -al /etc/[nmrp]*.conf
-rw-r--r--. 1 root root 827 Jun 19 2018 /etc/mke2fs.conf
-rw-r--r--. 1 root root 1688 May 5 2010 /etc/nsswitch.conf
-rw-r--r--. 1 root root 79 Jan 3 18:45 /etc/resolv.conf
-rw-r--r--. 1 root root 2875 Dec 1 2017 /etc/rsyslog.conf
[root@localhost ~]#
[root@localhost ~]#
-
顯示 /var 目錄下所有以 l 開頭,以一個小寫字母結尾,且中間出現至少一位數的檔案或目錄
[root@localhost ~]#
[root@localhost ~]# echo 111 > /var/l8ss
[root@localhost ~]#
[root@localhost ~]# echo 22 > /var/l22jui
[root@localhost ~]#
[root@localhost ~]# ls -d /var/l[0-9][[:lower:]]
/var/l22jui /var/l8ss
[root@localhost ~]#
[root@localhost ~]#
-
顯示/etc目錄下以任意一位數字開頭,且以非數字結尾的檔案或目錄
[root@localhost ~]#
[root@localhost ~]# echo 11 > /etc/89ll
[root@localhost ~]#
[root@localhost ~]# echo 12 > /etc/2hell
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ls -d /etc/[0-9]*[[1]]
/etc/2hell /etc/89ll
[root@localhost ~]#
-
顯示/etc/目錄下以非字母開頭,後面跟了一個字母及其它任意長度任意字元的檔案或目錄
[root@localhost ~]#
[root@localhost ~]# echo 12 > /etc/2hell
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# echo 13 > /etc/22lsflskj22
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ls -d /etc/[[2]][a-zA-Z]*
/etc/2hell
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
-
顯示/etc/目錄下所有檔名以rc開頭,並且後面是 0 到 6 中的數字,其它為任意字元的檔案或目錄
[root@localhost ~]#
[root@localhost ~]# ls -d /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
[root@localhost ~]#
[root@localhost ~]#
-
顯示 /etc 目錄下,所有以 .d 結尾的檔案或目錄
[root@localhost ~]#
[root@localhost ~]# ls -adl /etc/*.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/bash_completion.d
drwxr-xr-x. 2 root root 4096 May 11 2016 /etc/chkconfig.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/cron.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:49 /etc/depmod.d
drwxr-xr-x. 2 root root 4096 Jun 19 2018 /etc/dracut.conf.d
lrwxrwxrwx. 1 root root 11 Dec 21 19:49 /etc/init.d -> rc.d/init.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/ld.so.conf.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/logrotate.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:49 /etc/makedev.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/modprobe.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:51 /etc/pam.d
drwxr-xr-x. 2 root root 4096 Aug 21 2010 /etc/popt.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:49 /etc/prelink.conf.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/profile.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 Dec 21 19:50 /etc/rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root 4096 Dec 21 19:50 /etc/rc.d
drwxr-xr-x. 2 root root 4096 Jun 19 2018 /etc/rsyslog.d
drwxr-xr-x. 2 root root 4096 Jun 20 2018 /etc/rwtab.d
drwxr-xr-x. 2 root root 4096 Jun 20 2018 /etc/statetab.d
drwxr-x---. 2 root root 4096 Jun 23 2017 /etc/sudoers.d
drwxr-xr-x. 2 root root 4096 Jun 20 2018 /etc/sysctl.d
drwxr-xr-x. 2 root root 4096 Sep 23 2011 /etc/xinetd.d
drwxr-xr-x. 2 root root 4096 Dec 21 19:50 /etc/yum.repos.d
[root@localhost ~]#
[root@localhost ~]#
-
每天將 /etc/ 目錄下所有檔案,備份到 /data 目錄的獨立子目錄下,並要求子目錄名稱格式為 backupYYYY-mm-dd
[root@localhost ~]#
[root@localhost ~]# mkdir -pv /data/backupdate +%F
mkdir: created directory '/data'
mkdir: created directory '/data/backup2020-01-03'
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cp -a /etc/ /data/backupdate +%F
[root@localhost ~]#
[root@localhost ~]# ls -la /data/backup2020-01-03/
total 12
drwxr-xr-x. 3 root root 4096 Jan 3 19:46 .
drwxr-xr-x. 3 root root 4096 Jan 3 19:46 ..
drwxr-xr-x. 63 root root 4096 Jan 3 19:40 etc
[root@localhost ~]#
-
如何使用一條命令建立以下資料夾,/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b,
/testdir/dir1/y/a, /testdir/dir1/y/b
[root@localhost ~]#
[root@localhost ~]# mkdir -pv /testdir/dir1{x,y}/{a,b}
mkdir: created directory '/testdir'
mkdir: created directory '/testdir/dir1x'
mkdir: created directory '/testdir/dir1x/a'
mkdir: created directory '/testdir/dir1x/b'
mkdir: created directory '/testdir/dir1y'
mkdir: created directory '/testdir/dir1y/a'
mkdir: created directory '/testdir/dir1y/b'
[root@localhost ~]#
[root@localhost ~]#
-
如何使用一條命令建立 /root/testdir/dir3, /root/testdir/dir4, /root/testdir/dir5, /root/testdir/dir5/dir6, /root/testdir/dir5/dir7
[root@localhost ~]#
[root@localhost ~]# mkdir -pv /root/testdir/dir{3,4,5/dir{6,7}}
mkdir: created directory '/root/testdir'
mkdir: created directory '/root/testdir/dir3'
mkdir: created directory '/root/testdir/dir4'
mkdir: created directory '/root/testdir/dir5'
mkdir: created directory '/root/testdir/dir5/dir6'
mkdir: created directory '/root/testdir/dir5/dir7'
[root@localhost ~]#