一、实验目的

掌握Linux各类命令的使用方法。
熟悉Linux操作环境。

二、实验内容

1、文件和目录类命令

(1)启动计算机,利用root用户登录到系统,进入字符提示界面。

(2)用pwd命令查看当前所在的目录。

  1. [root@centos-linux ~]# pwd
  2. /root
  3. [root@centos-linux ~]#

(3)用ls命令列出此目录下的文件和目录。

  1. [root@centos-linux ~]# ls
  2. anaconda-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面

(4)用-a选项列出此目录下包括隐藏文件在内的所有文件和目录。

  1. [root@centos-linux ~]# ls -a
  2. . .bash_logout .config .lesshst 公共 文档
  3. .. .bash_profile .cshrc .local 模板 下载
  4. anaconda-ks.cfg .bashrc .esd_auth .tcshrc 视频 音乐
  5. .bash_history .cache .ICEauthority test 图片 桌面
  6. [root@centos-linux ~]#

(5)用man命令查看ls命令的使用手册。

  1. [root@centos-linux test]# man ls
  2. LS(1) User Commands LS(1)
  3. NAME
  4. ls - list directory contents
  5. SYNOPSIS
  6. ls [OPTION]... [FILE]...
  7. DESCRIPTION
  8. List information about the FILEs (the current directory by default).
  9. Sort entries alphabetically if none of -cftuvSUX nor --sort is speci
  10. fied.
  11. Mandatory arguments to long options are mandatory for short options
  12. too.
  13. -a, --all
  14. do not ignore entries starting with .
  15. -A, --almost-all
  16. do not list implied . and ..
  17. --author
  18. with -l, print the author of each file
  19. [root@centos-linux test]#

(6)在当前目录下,创建测试目录test。

(7)利用ls命令列出文件和目录,确认test目录创建成功。

  1. [root@centos-linux ~]# mkdir test
  2. [root@centos-linux ~]# ls
  3. anaconda-ks.cfg test 公共 模板 视频 图片 文档 下载 音乐 桌面

(8)进入test目录,利用pwd命令查看当前工作目录。

  1. [root@centos-linux ~]# cd test
  2. [root@centos-linux test]# pwd
  3. /root/test
  4. [root@centos-linux test]#

(9)利用touch命令,在当前目录创建一个新的空文件newfile。

  1. [root@centos-linux test]# touch newfile
  2. [root@centos-linux test]# ls
  3. newfile

(10)利用cp命令复制系统文件/etc/profile到当前目录下。

  1. [root@centos-linux test]# cp /etc/profile .
  2. [root@centos-linux test]# ls
  3. newfile profile

(11)复制文件profile到一个新文件profile.bak,作为备份。

  1. [root@centos-linux test]# cp profile profile.bak
  2. [root@centos-linux test]# ls
  3. newfile profile profile.bak

(12)用ll命令以长格式列出当前目录下的所有文件,注意比较每个文件的长度和创建时间的不同。

  1. [root@centos-linux test]# ll
  2. 总用量 4188
  3. -rw-r--r--. 1 root root 0 3 18 14:22 newfile
  4. -rw-r--r--. 1 root root 1750 3 18 14:58 profile
  5. -rw-r--r--. 1 root root 1750 3 18 14:58 profile.bak
  6. [root@centos-linux test]#

(13)用less命令分屏查看文件profile的内容,注意练习less命令的各个子命令.

  1. [root@centos-linux test]# less profile
  2. # /etc/profile
  3. # System wide environment and startup programs, for login setup
  4. # Functions and aliases go in /etc/bashrc
  5. # It's NOT a good idea to change this file unless you know what you
  6. # are doing. It's much better to create a custom.sh shell script in
  7. # /etc/profile.d/ to make custom changes to your environment, as this
  8. # will prevent the need for merging in future updates.
  9. pathmunge () {
  10. case ":${PATH}:" in
  11. *:"$1":*)
  12. ;;
  13. *)
  14. if [ "$2" = "after" ] ; then
  15. PATH=$PATH:$1
  16. else
  17. PATH=$1:$PATH
  18. fi
  19. esac
  20. }
  21. if [ -x /usr/bin/id ]; then
  22. if [ -z "$EUID" ]; then
  23. [root@centos-linux test]#

(14)用grep命令在profile文件中对关键字then进行查询,并与上面的结果比较。

  1. [root@centos-linux test]# less profile
  2. # /etc/profile
  3. # System wide environment and startup programs, for login setup
  4. # Functions and aliases go in /etc/bashrc
  5. # It's NOT a good idea to change this file unless you know what you
  6. # are doing. It's much better to create a custom.sh shell script in
  7. # /etc/profile.d/ to make custom changes to your environment, as this
  8. # will prevent the need for merging in future updates.
  9. pathmunge () {
  10. case ":${PATH}:" in
  11. *:"$1":*)
  12. ;;
  13. *)
  14. if [ "$2" = "after" ] ; then
  15. PATH=$PATH:$1
  16. else
  17. PATH=$1:$PATH
  18. fi
  19. esac
  20. }
  21. if [ -x /usr/bin/id ]; then
  22. if [ -z "$EUID" ]; then
  23. # ksh workaround
  24. EUID=`id -u`
  25. UID=`id -ru`
  26. fi
  27. USER="`id -un`"
  28. LOGNAME=$USER
  29. MAIL="/var/spool/mail/$USER"
  30. fi
  31. # Path manipulation
  32. if [ "$EUID" = "0" ]; then
  33. pathmunge /usr/sbin
  34. pathmunge /usr/local/sbin
  35. :/then

(15)删除rofile文件和profile.bak文件

  1. [root@centos-linux test]# rm -f profile
  2. [root@centos-linux test]# rm -f profile.bak

(16)用tar命令把目录test打包。

  1. [root@centos-linux ~]# tar cvf test.tar .
  2. [root@centos-linux ~]# ls
  3. anaconda-ks.cfg test test.tar 公共 模板 视频 图片 文档 下载 音乐 桌面
  4. [root@centos-linux ~]#

(17)在test下创建一个子目录test1

  1. [root@centos-linux test]# mkdir test1
  2. [root@centos-linux test]# ls
  3. newfile test1
  4. [root@centos-linux test]#

(18)把文件test.tar解包。

  1. [root@centos-linux test]# tar xvf test.tar ./test1

(19)复制test目录为testbak目录作为备份。

  1. [root@centos-linux ~]# cp -r /root/test /root/testbak
  2. [root@centos-linux ~]# ls
  3. anaconda-ks.cfg test test.tar 模板 图片 下载 桌面
  4. core.3751 testbak 公共 视频 文档 音乐
  5. [root@centos-linux ~]#

(20)查找root用户自己的主目录下的所有名为newfile的文件。

  1. [root@centos-linux ~]# find /root -name newfile
  2. /root/test/newfile
  3. /root/testbak/newfile
  4. [root@centos-linux ~]#

(21)删除test子目录下的所有文件。

  1. [root@centos-linux test]# rm -f newfile
  2. [root@centos-linux test]# ls
  3. test1
  4. [root@centos-linux test]#

(22)利用rmdir命令删除目录test。

  1. [root@centos-linux ~]# rm -rf test
  2. [root@centos-linux ~]# ls
  3. anaconda-ks.cfg testbak 公共 视频 文档 音乐
  4. core.3751 test.tar 模板 图片 下载 桌面
  5. [root@centos-linux ~]#

三、实验作业

1、在/root目录下新建目录test1/test2/test3

  1. [root@centos-linux ~]# mkdir -p test1/test2/test3
  2. [root@centos-linux ~]# ls
  3. anaconda-ks.cfg test1 模板 图片 下载 桌面
  4. core.3751 公共 视频 文档 音乐
  5. [root@centos-linux ~]#

2、在/home目录下新建目录TEST

  1. [root@centos-linux ~]# cd /home
  2. [root@centos-linux home]# mkdir TEST
  3. [root@centos-linux home]# ls
  4. cyy TEST
  5. [root@centos-linux home]#

3、在test2目录下新建文件file1.txt

  1. [root@centos-linux ~]# cd /root/test1/test2
  2. [root@centos-linux test2]# touch file1.txt

4、复制file1.txt文件到test2目录下重命名为file1.txt.bak

  1. [root@centos-linux test2]# cp file1.txt file1.txt.bak
  2. [root@centos-linux test2]# ls
  3. file1.txt file1.txt.bak test3

5、在当前用户家目录下新建文件user_file.txt

  1. [root@centos-linux test2]# cd
  2. [root@centos-linux ~]# touch user_file.txt

6、将user_file.txt复制到TEST目录下;

  1. [root@centos-linux ~]# cp user_file.txt /home/TEST
  2. [root@centos-linux ~]# cd /home/TEST
  3. [root@centos-linux TEST]# ls
  4. user_file.txt

7、将test1目录下内容复制到TEST目录下;

  1. [root@centos-linux TEST]# cp -r /root/test1 /home/TEST
  2. [root@centos-linux TEST]# ls
  3. test1 user_file.txt

8、将TEST目录重命名为test;

  1. [root@centos-linux ~]# cd /home
  2. [root@centos-linux home]# ls
  3. cyy TEST
  4. [root@centos-linux home]# mv TEST test
  5. [root@centos-linux home]# ls
  6. cyy test

9、将test目录下文件user_file.txt和test2下的file1.txt移动到根目录下;

  1. [root@centos-linux home]# mv /home/test/user_file.txt /root
  2. mv:是否覆盖"/root/user_file.txt" yes
  3. [root@centos-linux home]# mv /root/test1/test2/file1.txt /root
  4. [root@centos-linux home]# cd
  5. [root@centos-linux ~]# ls
  6. anaconda-ks.cfg file1.txt user_file.txt 模板 图片 下载 桌面
  7. core.3751 test1 公共 视频 文档 音乐

10、将test2目录下file1.txt.bak文件删除;

  1. [root@centos-linux ~]# cd /root/test1/test2
  2. [root@centos-linux test2]# ls
  3. file1.txt.bak test3
  4. [root@centos-linux test2]# rm -f file1.txt.bak

11、删除test1目录下所有内容并不提醒用户;

  1. [root@centos-linux ~]# rm -rf /root/test1

12、使用vi编辑器编辑1.txt和2.txt

  1. [root@centos-linux ~]# vi 1.txt
  2. [root@centos-linux ~]# vi 2.txt

13、将1.txt和2.txt的内容合并到3.txt中,并用cat查看3.txt内容

  1. [root@centos-linux ~]# cat 1.txt 2.txt>3.txt
  2. [root@centos-linux ~]# cat 3.txt
  3. 111111111
  4. 1111111111111
  5. 222222222222222222222222222222222222222222222222