第三章面试题

第三章-面试练习题参考答案.md

第三章 面试练习题

解答论述题

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 ~]#

  1. 请在 /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 ~]#

  2. 在 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 ~]#

  3. 请写出创建文件夹权限为 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 ~]#

  4. 使用 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 ~]#

  5. 请说出 cd ~ , cd - , cd 三个命令的作用和区别?

    cd 命令用于切换当前工作目录。cd 后跟 ~ 符号代表切换到家目录;cd 后跟 - 符号代表切换到上一次切换的当前路径;cd 后不添加任何路径,
    功能和 ~ 一样,用于切换到当前用户的家目录。

  6. 如何使用文件的 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 ~]#

  7. 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 ~]#

  8. 使用 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 ~]#

  9. 软连接和硬链接的区别,使用 cp 命令复制文件和创建硬链接文件有什么区别?

    符号 (或软) 链接是 Linux 的一种文件类型,它的 inode 指向的数据块中保存的是一个要指向文件的文件系统路径。
    硬链接是目录文件中的一条记录,他的 inode 编号和对应文件的 inode 文件相同,每个文件有多少个硬链接,他的
    inode 引用记录次数就会增加多少。正式因为硬链接是基于文件的 inode 编号的,所以他不能跨磁盘分区。当删除硬
    链接文件时,除了删除他在指定文件夹中的记录信息,同时也要减少该文件 inode 引用计数,直到该值为 0 时才删除
    该文件。不能给文件夹创建硬链接文件。cp 命令进行复制文件时,会在磁盘上重新分配一个 inode,同时这个 inode
    也会指向一个新的磁盘数据区域用来存储文件中的数据,和硬链接有根本的区别。

  10. 描述 /etc/rc.local 和 /var/log/messages 两文件内容是做什么的?

    /etc/rc.local 文件是由 initscripts 软件包提供,在 Linux 系统启动流程后期被运行的 shell 脚本文件,我们可以在该文件中添加一些功能和
    逻辑,以此来实现开机后自动运行程序的目的。/var/log/messages 文件用于存储 Linux 系统启动日志,mail,cron,daemon,kern,auth 等内
    容。

  11. /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 ~]#

  12. 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 ~]#

  13. 某个文件占用了过多磁盘空间,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 ~]#

  14. 如何分别查看到 /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 ~]#

  15. 显示/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 ~]#

  16. 显示 /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 ~]#

  17. 显示/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 ~]#

  18. 显示/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 ~]#

  19. 显示/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 ~]#

  20. 显示 /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 ~]#

  21. 每天将 /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 ~]#

  22. 如何使用一条命令创建以下文件夹,/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 ~]#

  23. 如何使用一条命令创建 /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 ~]#


  1. :digit: ↩︎

  2. :alpha: ↩︎

posted @ 2021-01-07 20:44  Zhangshiqian  阅读(127)  评论(0)    收藏  举报