1.分别说明根目录下常见的子目录/root、/etc、/dev、/var、/home、/bin和/sbin的作用。
/root:超级用户root的家目录。
/etc:存放Linux系统和各种程序的配置文件,Linux中的很多操作和配置都是通过修改配置文件实现的。/etc目录的作用类似于Windows系统中的注册表。
/dev:存放Linux系统中的硬盘、光驱和鼠标等硬件设备文件。
/var:存放系统运行过程中经常变化的文件,如 /var/log 用于存放日志文件、/var/spool/mail 用于存放邮件等。
/home:普通用户家目录(也称为主目录)。例如,用户账号 “student” 对应的家目录位于 “/home/student”。
/bin:存放Linux系统中常用的基本命令,任何用户都有权限执行。
/sbin:存放Linux系统中基本的管理命令,只有管理员权限才可以执行。
2.切换工作目录到 /usr/src,并执行命令查看当前所在的目录。
[root@localhost ~]# cd /usr/src
[root@localhost src]# pwd
/usr/src
3.当前所在的工作目录是 /usr/src,切换工作目录到当前目录的上一级目录。
[root@localhost src]# pwd
/usr/src
[root@localhost src]# cd ..
[root@localhost usr]#
4.将工作目录切换到当前用户的家目录。
[root@localhost usr]# cd
[root@localhost ~]# pwd
/root
5.查看/dev目录中所有文件的详细信息(包含隐藏文件),在Linux系统中,隐藏文件的标识是什么?在显示的文件详细信息中,第一组数的第1个字符代表文件类别,“-” “d” “l” “c” “b”分别代表的是哪种类别的文件?
以“.”开头的就是隐藏文件或隐藏目录。
“-”代表普通文件,“d”代表目录,“”代表符号链接,“c”代表字符设备,“b”代表块设备。
6.以长格式显示/etc/inittab 文件的详细信息。
[root@localhost ~]# ls -l /etc/inittab
-rw-r--r--. 1 root root 490 9月 2 2020 /etc/inittab
7.在root用户的家目录中创建一个名为test1的目录。
[root@localhost ~]# mkdir /root/test1
[root@localhost ~]# ls -ld /root/test1
drwxr-xr-x. 2 root root 6 8月 29 23:56 /root/test1
8.在/root/test1目录中创建一个名为templ的空文件。
[root@localhost ~]# touch /root/test1/temp1
[root@localhost ~]# ls -l /root/test1/temp1
-rw-r--r--. 1 root root 0 8月 30 00:00 /root/test1/temp1
9.复制文件/root/test1/templ 进行备份,仍然保存在/root/test1/目录下,备份的文件名为temp1.bak。
[root@localhost ~]# cp -r /root/test1/temp1 /root/test1/temp1.bak
[root@localhost ~]# ls -l /root/test1
总用量 0
-rw-r--r--. 1 root root 0 8月 30 00:00 temp1
-rw-r--r--. 1 root root 0 8月 30 00:05 temp1.bak
10.将文件/root/test1/temp1.bak移动到/tmp/目录下,并改名为temp.bak。
[root@localhost ~]# ls -l /root/test1
总用量 0
-rw-r--r--. 1 root root 0 8月 30 00:00 temp1
-rw-r--r--. 1 root root 0 8月 30 00:05 temp1.bak
[root@localhost ~]# mv /root/test1/temp1.bak /tmp/temp.bak
[root@localhost ~]# ls -l /root/test1
总用量 0
-rw-r--r--. 1 root root 0 8月 30 00:00 temp1
11.将/root/test1/目录强制删除。
[root@localhost ~]# rm -rf /root/test1
[root@localhost ~]# ls -l /root/test1
ls: 无法访问'/root/test1': 没有那个文件或目录
12.以长格式显示/etc目录本身的详细信息。
[root@localhost ~]# ls -ld /etc
drwxr-xr-x. 139 root root 8192 8月 28 18:21 /etc
13.查看操作系统的版本信息。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 8.3.2011
14.将/etc目录中所有以“d”结尾的文件或目录复制到/tmp目录。
[root@localhost ~]# find /etc -name "*.d" -exec cp -r {} /tmp \;
15.将/etc目录中所有以.conf结尾并且以m、n、r或p开头的文件或目录复制到/tmp目录。
[root@localhost ~]# find /etc -name [mnrp]*.conf -exec cp -r {} /tmp \;
16.显示/etc目录中所有以“pa”开头的文件,如果它是目录,则只显示目录本身。
[root@localhost ~]# ls -ld /etc/pa*
17.显示/dev目录中所有以“d”“f”开头并且文件名为3个字符的文件。
[root@localhost ~]# ls -ld /dev/[df]??
18.查看文件/etc/passwd的内容,并显示行号。
[root@localhost ~]# cat -n /etc/passwd
19.分别用more、less命令分屏查看/etc/passwd文件的内容。
[root@localhost ~]# more /etc/passwd
[root@localhost ~]# less /etc/passwd
20、查看/etc/passwd文件的前10行内容,并将结果保存到/tmp/1.txt文件中。
[root@localhost ~]# head /etc/passwd > /tmp/1.txt
[root@localhost ~]# cat /tmp/1.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
21.查看/etc/passwd文件的后5行内容,并将结果保存到/tmp/2.txt文件中。
[root@localhost ~]# tail -5 /etc/passwd > /tmp/2.txt
[root@localhost ~]# cat /tmp/2.txt
super:x:1006:1006::/tmp/super:/bin/bash
user4:x:1007:1005::/home/user4:/bin/bash
user5:x:1008:1008::/home/user5:/bin/bash
temp01:x:1009:100::/home/temp01:/bin/bash
ftpuser:x:1010:1010::/home/ftpuser:/sbin/nologin
22、统计/etc目录中扩展名是“*.conf”的文件的个数。
[root@localhost ~]# find /etc -name "*.conf" | wc -l
428
23.查找/etc目录下以net 开头的文件,将结果保存到/tmp/net.file文件中。
[root@localhost ~]# find /etc -name "net*" > /tmp/net.file
24.在/boot目录中查找大小超过1024KB且文件名以“init”开头的文件。
[root@localhost ~]# find /boot -size +1024k -name "init*"
/boot/initramfs-4.18.0-240.el8.x86_64.img
/boot/initramfs-0-rescue-3cdf0a2a0a994c07910a3b6f53ed92e1.img
/boot/initramfs-4.18.0-240.el8.x86_64kdump.img
25.在/etc/passwd文件中查找包含“root”字符串的行。
[root@localhost ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
26.查找/etc/profile文件中所有不以“#”开头并且不是空白的行。
[root@localhost ~]# grep -v "^#" /etc/profile | grep -v "^$"
27.查看系统中已经设置的别名命令。
[root@localhost ~]# alias
28.创建名为wcl的命令别名,统计/etc/profile文件中不以#开头且不是空白行的行数。
[root@localhost ~]# alias wcl='grep -v "^#" /etc/profile | grep -v "^$"|wc -l'
[root@localhost ~]# wcl
61
29.找到find命令的命令文件路径。
[root@localhost ~]# which find
/usr/bin/find
30.查看find命令的简要帮助信息。
[root@localhost ~]# find --help
31.查看grep 命令的帮助手册。
[root@localhost ~]# man grep
32.查看历史命令。
[root@localhost ~]# history
33.查找/etc/passwd 文件中所有以“nologin”结尾的行。
[root@localhost ~]# grep "nologin$" /etc/passwd
34.假设存在3个目录:/tmp/a、/tmp/b 和/tmp/c,要求用一条命令强制删除这3个目录。
[root@localhost tmp]# rm -rf a b c
35.查找/etc目录下大于1MB且类型为普通文件的所有文件。
[root@localhost tmp]# find /etc -size +1M -type f
/etc/selinux/targeted/policy/policy.31
/etc/udev/hwdb.bin
36.在/tmp目录中创建一个名为hi.txt的文件,文件内容为“Hello World”。
[root@localhost tmp]# echo "Hello World" > /tmp/hi.txt
[root@localhost tmp]# cat hi.txt
Hello World
37.将执行“find / -user student”命令时产生的错误信息重定向到/dev/null文件中。
[root@localhost tmp]# find / -user student 2> /dev/null
38.如何查看系统时钟与硬件时钟?
[root@localhost tmp]# date
2025年 08月 30日 星期六 07:09:19 UTC
[root@localhost tmp]# hwclock
2025-08-30 19:08:53.956668+00:00
39.以“年/月/日时:分:秒”的形式显示当前日期和时间,如“2018/05/26 15:02:48”。
[root@localhost tmp]# date "+%Y/%m/%d %T"
2025/08/30 07:13:20
40.某台Linux服务器疑似正在遭受更改,作为管理员的你需要实时观察系统日志文件/var/log/messages 中的最新内容,该如何用命令实现?
[root@localhost tmp]# tail -f /var/log/messages
41、在/etc目录中查找最近1天之内被改动过的文件。
[root@localhost tmp]# find /etc -ctime -1 -type f
42.从/tmp目录及其子目录中找出扩展名为.txt的文件,并将之删除。
[root@localhost tmp]# find /tmp -name "*.txt" -exec rm -f {} \;