Linux常用操作练习题:

1.练习题

0.用cat显示/etc/passwd,并显示行号

[root@rhcsa001 ~]# cat -n /etc/passwd

1. 统计/etc/passwd文件的行数

[root@rhcsa001 ~]# wc -l /etc/passwd

2.将/etc/passwd文件中的前20行重定向保存到/hoem下改名20_pass.txt

[root@rhcsa001 ~]# head -n 20 /etc/passwd >> /home/20_pass.txt

3.在当前目录下创建空文件夹/web/test1

[root@rhcsa001 ~]# mkdir -p ./web/test1
[root@rhcsa001 ~]# tree
.
└── web
    └── test1

2 directories, 0 files

4.查看当前工作目录的命令

[root@rhcsa001 ~]# pwd
[root@rhcsa001 ~]# echo $PWD

5.将/tmp下的文件file1复制到当前目录下,文件名仍为file1

[root@rhcsa001 ~]# cp -r /tmp/file1 ./

6. 将文件file复制成file1

[root@rhcsa001 ~]# cp file ./file1
[root@rhcsa001 ~]# ls
 file  file1

7. 显示环境变量path,将/root加入到$PATH中

[root@rhcsa001 ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin
[root@rhcsa001 ~]# export PATH=$PATH:/root
[root@rhcsa001 ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/root

8. 拷贝文件/etc/passwd到/tmp目录下

[root@rhcsa001 ~]# cp /etc/passwd /tmp
[root@rhcsa001 ~]# cd /
[root@rhcsa001 /]# cd tmp/
[root@rhcsa001 tmp]# ls
 passwd

9. 查看/tmp/目录的使用空间

[root@rhcsa001 ~]# du -h /tmp

10. 删除空目录dir1

[root@rhcsa001 ~]# rm -rfv dir1/

11. 将/root下的所有文件删除

[root@rhcsa001 ~]# rm -rfv *

12. 删除当前目录下子目录dir中的文件file

[root@rhcsa001 ~]# tree
.
└── dir
    └── file

1 directory, 1 file

[root@rhcsa001 ~]# rm -rfv ./dir/file 

13. 将目录dir1设定成任何人皆有读取及执行的权利,但只有拥有者可作写修改

14. 在/下建立目录test,在test建立文件1.txt和2.txt,分别在文件1.txt和2.txt中输入 :                             “I am chinese”,“are you ok?”

[root@rhcsa001 ~]# cd /
[root@rhcsa001 /]# mkdir test

[root@rhcsa001 /]# cd ./test/
[root@rhcsa001 test]# echo "I am Chinese" >> 1.txt
[root@rhcsa001 test]# echo "Are you OK?" >> 2.txt
[root@rhcsa001 test]# ls
1.txt  2.txt
[root@rhcsa001 test]# cat 1.txt 
I am Chinese
[root@rhcsa001 test]# cat 2.txt 
Are you OK?

 15. 在当前目录下新建一个名称为a.txt的文件,并在文件里面输入如下内容:
Hello Linux!  
Jishou university!  

[root@rhcsa001 ~]# touch a.txt
[root@rhcsa001 ~]# vim a.txt 
进入命令模式
按i键,进入插入模式进行文本编辑
:wq!进入底行模式并进行保存退出

[root@rhcsa001 ~]# cat a.txt 
Hello Linux!
Jishou university!

16. 在/etc/passwd文件中查找所有以“root”开头的文件

[root@rhcsa001 ~]# vim /etc/passwd
进入文件后,进入末行模式输入 :/root

17. 在dir1目录中建立一个空文件file1

[root@rhcsa001 dir1]# touch file1
[root@rhcsa001 ~]# tree
.
└── dir1
    └── file1

1 directory, 1 file

18. 创建用户win1,UID、GID等均按默认

19. 创建用户win2,默认主目录为/think,其余默认

20. 用cat命令将file1、file2、file3合并为文件filenew

[root@rhcsa001 ~]# cat file1 file2 file3
 hello_word
 welcome_come_to_linux
 this_is_a_new_word

[root@rhcsa001 ~]# cat file1 file2 file3 >>filenew
[root@rhcsa001 ~]# cat filenew 
 hello_word
 welcome_come_to_linux
 this_is_a_new_word

21. 将目录dir1改名为dir2

[root@rhcsa001 ~]# mkdir dir1
[root@rhcsa001 ~]# ls
dir1
[root@rhcsa001 ~]# mv dir1 ./dir2
[root@rhcsa001 ~]# ls
dir2

22. 将文件file1改名为file2

[root@rhcsa001 ~]# touch file1
[root@rhcsa001 ~]# ls
 file1
[root@rhcsa001 ~]# mv file1 ./file2
[root@rhcsa001 ~]# ls
 file2

23. 建立source文件的符号链接,命名为source.soft

[root@rhcsa001 ~]# touch source
[root@rhcsa001 ~]# ln -s source source.soft
[root@rhcsa001 ~]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 30 19:00 source
lrwxrwxrwx. 1 root root 6 Sep 30 19:00 source.soft -> source

24. 创建install.login文件的软链接文件install.soft,硬链接文件install.hard

[root@rhcsa001 ~]# touch install.lodin
[root@rhcsa001 ~]# ln -s install.lodin install.soft
[root@rhcsa001 ~]# ln  install.lodin install.hard
[root@rhcsa001 ~]# ll
total 0
-rw-r--r--. 2 root root  0 Sep 30 19:04 install.hard
-rw-r--r--. 2 root root  0 Sep 30 19:04 install.lodin
lrwxrwxrwx. 1 root root 13 Sep 30 19:04 install.soft -> install.lodin

25. 查看/root目录下有哪些文件和目录

[root@rhcsa001 ~]# ls /root

[root@rhcsa001 ~]# ll /root

[root@rhcsa001 ~]# tree /root

26. 查看/etc/passwd文件的前10行

[root@rhcsa001 ~]# head -10 /etc/passwd | cat -n
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin

cat -n 用于显示行号

27.查看/etc/passwd文件的后2行

[root@rhcsa001 ~]# tail -2 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash

28. 查看当前目录中file的文件内容

[root@rhcsa001 ~]# cat file 
[root@rhcsa001 ~]# head file 
[root@rhcsa001 ~]# tail file 
[root@rhcsa001 ~]# less file 
[root@rhcsa001 ~]# vim file 
[root@rhcsa001 ~]# vi file

29. 在/root目录下创建1/2/3/4,在用户目录中创建目录/a/b/c/d ,

1.在超级用户/root文件下创建
[root@rhcsa001 ~]# mkdir -p 1/2/3/4
[root@rhcsa001 ~]# tree


2.在普通用户文件下创建
[root@rhcsa001 ~]# su -l redhat
[redhat@rhcsa001 ~]$ mkdir -p a/b/c/d
[redhat@rhcsa001 ~]$ tree
.
├── a
│   └── b
│       └── c
│           └── d

30. 把/root目录下所有文件和子目录拷贝到/a/b/c/d目录下

超级用户和普通用户之间不是割裂的,超级用户可以管理普通用户
/目录下的/root为超级用户文件;/home为普通用户文件;
[root@rhcsa001 ~]# cd /
[root@rhcsa001 /]# ls
 root  home 
[root@rhcsa001 /]# ls /home/
 redhat

[root@rhcsa001 ~]# cp -r ./1  /home/redhat/a/b/c/d
[root@rhcsa001 ~]# su -l redhat
[redhat@rhcsa001 ~]$ tree ./a/b/c/d
./a/b/c/d
└── 1
    └── 2
        └── 3
            └── 4

31. 把/root目录下的install.log拷贝成1.txt,2.txt,3.txt,a.txt,a.doc

[root@rhcsa001 ~]# cp install.log 1.txt
[root@rhcsa001 ~]# cp install.log 2.txt
[root@rhcsa001 ~]# cp install.log 3.txt
[root@rhcsa001 ~]# cp install.log a.txt
[root@rhcsa001 ~]# cp install.log b.doc
[root@rhcsa001 ~]# ls
1.txt  2.txt  3.txt  a.txt  b.doc  install.log
  后面学到脚本,一个循环就可以解决!

32. 把/root下的以txt结尾的文件拷贝到/a目录下

[root@rhcsa001 ~]# cp -r *.txt ./a

33. 把/a目录下1.txt改名为2.txt

[root@rhcsa001 ~]# mv ./a/1.txt ./a/2.txt
[root@rhcsa001 ~]# tree ./a
./a
└── 2.txt

34. 把/root目录下install.log的前10行内容输出到文件/a/b/a.log中

[root@rhcsa001 ~]# head install.log >> ./a/b/a.log

35. 删除/a/b目录下的所有文件和子目录

[root@rhcsa001 ~]# rm -rfv ./a/b/*
[root@rhcsa001 ~]# tree
.
└── a
    ├── 2.txt
    └── b
这里注意/b代表删除包含b目录在内的文件,/b/代表删除b目录以下的文件

36. 测试本机与域名为www.baidu.com的连通性

[root@rhcsa001 ~]# ping www.baidu.com
PING www.a.shifen.com (183.2.172.42) 56(84) bytes of data.
64 bytes from 183.2.172.42 (183.2.172.42): icmp_seq=1 ttl=128 time=47.3 ms
64 bytes from 183.2.172.42 (183.2.172.42): icmp_seq=2 ttl=128 time=50.5 ms

37. 查阅passwd命令的使用手册

[root@rhcsa001 ~]# man passwd
  按q键退出手册

38. 将/etc/alsa目录下的所有文件复制到当前目录下的dir目录中

1.查看/etc/alsa目录
[root@rhcsa001 ~]# ls /etc/alsa
alsactl.conf  conf.d  state-daemon.conf
2.将/etc/alsa目录下所有文件复制到当前目录的dir目录下
[root@rhcsa001 ~]# cp -r /etc/alsa/* ./dir/
3.查看dir目录tree结构
[root@rhcsa001 ~]# tree dir

39. 搜索/etc/passwd中包含root的行,并显示出来

1.进入文本编译器
[root@rhcsa001 ~]# vim /etc/passwd
进入底行模式输入以下指令
:/root   

2.使用grep命令
[root@rhcsa001 ~]# grep -n root /etc/passwd 

40. 修改当前用户的密码

[root@rhcsa001 ~]# passwd
New password: 
Retype new password: 

41. 显示当前登录系统的用户

[root@rhcsa001 ~]# pwd
/root
[root@rhcsa001 ~]# who
root     seat0        2024-10-09 10:59 (login screen)
root     tty2         2024-10-09 10:59 (tty2)
[root@rhcsa001 ~]# whoami
root
[root@rhcsa001 ~]# pwd $USER
/root

42. 查看/etc目录占用的磁盘空间

[root@rhcsa001 ~]# du -h /etc

43. 显示环境变量USER的值

[root@rhcsa001 ~]# pwd $USER
/root

44. 利用重定向将create.c 的数据输出到 output.c

[root@rhcsa001 ~]# echo create.c >>output.c

45. 在你的主目录下建立目录树
          mydir
     shell     program
linux dos   standart

[root@rhcsa001 ~]# mkdir -p mydir/shell
[root@rhcsa001 ~]# mkdir -p mydir/program
[root@rhcsa001 ~]# mkdir -p mydir/shell/linux
[root@rhcsa001 ~]# mkdir -p mydir/shell/dos
[root@rhcsa001 ~]# mkdir -p mydir/program/standart
[root@rhcsa001 ~]# tree mydir
mydir
├── program
│   └── standart
└── shell
    ├── dos
    └── linux

2.选择题

1.在大多数Linux发行版本中,以下哪个属于块设备 (block devices)       B
A. 串行口  B. 硬盘  C. 虚拟终端  D. 打印机  

串行口是接口、硬盘是块设备、虚拟终端是终端设备、打印机是外围设备


2.Linux终端中root用户使用( )作为默认的提示符       B
A. $  B. #  C. ?  D. !         

#是超级用户默认提示符、$是普通用户默认提示符


3. shell命令( )可以创建目录        A
A. mkdir  B. mv  C. diff  D. ls


4.使用命令ls显示文件hello的描述如下所示        D
lrwxr--r-- 1 root root 214 Dec 10 17:12 hello
由此可知文件hello的类型为( )。
A. 普通文件  B. 硬链接  C. 目录  D. 符号链接


5.shell命令( )可以从底部往上显示指定的行数   D
A. more  B. tac  C. nl  D. tail


6. 用来显示/home及其子目录下的文件名的shell命令是( )     B  
A. ls –R /home  B. ls –d /home  C. ls –a /home  D. ls –l /home

A. ls -R 以递归形式显示
[root@rhcsa001 ~]# ls -R /home
/home:
redhat
/home/redhat:
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

B. ls -d 只显示目录自身
[root@rhcsa001 ~]# ls -d  /home
/home

C. ls -a 显示目录下所有文件,包括隐藏文件
[root@rhcsa001 ~]# ls -a /home
.  ..  redhat

D. ls -l = ll 以长列表方式显示目录文件
[root@rhcsa001 ~]# ls -l
drwxr-xr-x. 2 root root   6 Oct 18  2023 Desktop
drwxr-xr-x. 2 root root   6 Oct 18  2023 Documents


7. 目录( )通常用来存放Linux的源代码     C
A. /etc  B. /home  C. /usr/src  D. /usr

/etc:用于存放Linux的配置文件
/conf:用于存放整个系统的配置文件
/home:用于存放普通目录的家目录,是普通用户的根
/usr/src:用于存放Linux的源代码
/usr:用于安装第三方目录


8.在 bash 中, 在一条命令后加入"1>&2" 意味着      
A. 标准错误输出重定向到标准输入  B. 标准输入重定向到标准错误输出
C. 标准输出重定向到标准错误输出  D. 标准输出重定向到标准输入

重定向:
0:标准输入  1:标准输出  2:标准错误输出


9.在使用mkdir命令创建新的目录时,在其父目录不存在时先创建父目录的选项是   D
A. –m  B. –d  C. –f  D. –p


10.使用ln命令将生成了一个指向文件old的符号链接new,如果你将文件old删除,是否还能够访问文件中的数据      A
A. 不可能再访问  B. 仍然可以访问
C. 能否访问取决于文件的所有者  D. 能否访问取决于文件的权限


11.下面哪种写法表示如果cmd1成功执行,则执行cmd2命令      
A. cmd1 && cmd2   B. cmd1 | cmd2
C. cmd1 ; cmd2       D. cmd1 || cmd2

A:cmd1 && cmd2  
B:cmd1 | cmd2   管道符,cmd1执行的结果作为cmd2的输入
C:cmd1 ;cmd2   cmd1和cmd2是不同的程序,cmd1执行与否于cmd2无关
D:cmd1 || cmd2  


12.下面哪个命令可以压缩部分文件    C
A. tar -dzvf filename.tgz *   B. tar -tzvf filename.tgz *
C. tar -czvf filename.tgz *   D. tar -xzvf filename.tgz *


13. 下面( )命令可以跨文件系统      A
A. ln    B. mv   C. cp   D. touch


14.Shell的自动补齐功能按( )键会在相关目录下自动查找匹配项     A
A. Tab   B. SHIFT   C. ^D   D. ALT


15.Linux文件系统的文件都按其作用分门别类地放在相关的目录中,对于外部设备文件,一般应将其放在( )目录中        C
A. /bin   B. /etc   C. /dev   D. /lib

/bin:用于存放普通用户可执行的脚本文件
/etc:存放系统的配置文件
/dev:存放外部设备文件
/lib:存放系统运行所需要的库文件


16.系统正常运行时,要改变的数据存放在( )中      D
A. /   B. /prc   C. /dev   D. /var

/:根目录   /prc:进程所在目录
/dev:外部设备存放目录    /var:运行时变量所在目录eg:日志文件


17.将xx.tar.gz的备份文件还原并解压缩的命令是( )        B
A. tar cxvf xx.tar.gz   B. tar zxvf xx.tar.gz    
C. tar tzvf xx.tar.gz    D. gzip –dtv xx.tar.gz


18.建立一个新文件可以使用的命令为( )        D
A. chmod   B. more   C. cp   D. touch


19.vi中删除所在整行的命令是()     C
A. yy   B. y1   C. dd   D. ss


20.假设当前工作目录为“/root”,请按顺序写出下面操作步骤中所用到的shell命令(包括参数)
  1.进入用户root主目录下的“Music”目录;
  2.复制文件“/root/file1”到“/root/Music”目录下并同时重命名为“file1.txt”;
  3.把“/root/temp/file2”文件移动到“/root/Music”目录下;
  4.删除非空目录“/root/temp”,要求不出现“是否删除文件”的提示。

0.先做准备工作:创建/root目录下file文件、/root目录下temp目录和/root/temp目录下file2文件
[root@rhcsa001 ~]# echo hello_word >> file1
[root@rhcsa001 ~]# mkdir ./temp
[root@rhcsa001 ~]# echo hello_word >>./temp/file2

1.进入root主目录下的"music"目录
[root@rhcsa001 ~]# cd ./Music/
[root@rhcsa001 Music]# 

2.复制文件/root/file1到/root/Music目录下并重命名为:file1.txt
[root@rhcsa001 Music]# cp  /root/file1 ./file1.txt

3.把/root/temp/file2文件移动到/root/Music目录下
[root@rhcsa001 Music]# mv /root/temp/file2 ./

4.删除非空文件/root/temp且不提示
[root@rhcsa001 ~]# rm -rf ./temp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值