天天看点

玩转Redhat Linux 8.0系列 | 使用命令行工具管理文件

作者:热爱编程的通信人

素材来源:Redhat Linux 8.0培训教材《RH124》、《RH134》和《RH294》

玩了5-6年的Linux,现在再来温习一遍RHCE培训教材,按照指导完成实验并与大家分享。

附上汇总贴:玩转Redhat Linux 8.0系列 | 合集_热爱编程的通信人的博客-CSDN博客

1 使用ssh命令,以student用户身份登录servera。系统已配置为使用SSH密钥来进行身份验证,因此不需要提供密码。

[student@workstation ~]$ ssh student@servera
The authenticity of host 'servera (172.16.190.228)' can't be established.
ECDSA key fingerprint is SHA256:GEKaIl/VRjqlHWHB9Spu4YlhO5jzagdPuHBj4cvcy+0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'servera,172.16.190.228' (ECDSA) to the list of known hosts.
student@servera's password: 
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Fri May 19 09:17:40 2023
[student@servera ~]$            

2 在student用户的主目录中,使用mkdir命令创建三个子目录:Music、Pictures和Videos。

[student@servera ~]$ mkdir Music Pictures Videos
[student@servera ~]$            

3 继续在student用户的主目录中,使用touch命令创建本实验过程中要使用的一套空白的练习文件。

  • 创建六个文件,并以song X.mp 3形式取名。
  • 创建六个文件,并以snapX.jpg形式取名。
  • 创建六个文件,并以filmX.avi形式取名。

在每一组文件中,将X替换为数字1到6。

[student@servera ~]$ touch song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3
[student@servera ~]$ touch snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg
[student@servera ~]$ touch film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi
[student@servera ~]$ ls -l
total 0
drwxr-xr-x. 2 student student 6 May 19  2023 Desktop
drwxr-xr-x. 2 student student 6 May 19  2023 Documents
drwxr-xr-x. 2 student student 6 May 19  2023 Downloads
-rw-rw-r--. 1 student student 0 May 19 09:24 film1.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film2.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film3.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film4.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film5.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film6.avi
drwxrwxr-x. 2 student student 6 May 19 09:21 Music
drwxrwxr-x. 2 student student 6 May 19 09:21 Pictures
drwxr-xr-x. 2 student student 6 May 19  2023 Public
-rw-rw-r--. 1 student student 0 May 19 09:23 snap1.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap2.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap3.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap4.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap5.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap6.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 song1.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song2.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song3.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song4.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song5.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song6.mp3
drwxr-xr-x. 2 student student 6 May 19  2023 Templates
drwxrwxr-x. 2 student student 6 May 19 09:21 Videos
-rw-rw-r--. 1 student student 0 May 19 09:09 zcat
[student@servera ~]$            

4 继续在student用户的主目录中,将歌曲文件移动到Music子目录,将快照文件移到Pictures子目录,并将影片文件移到Videos子目录。

将文件从一个位置分发到多个位置时,首先更改到包含源文件的目录。使用最简单的路径语法(绝对或相对路径),到达各项文件管理任务的目标位置。

[student@servera ~]$ mv song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3 Music
[student@servera ~]$ mv snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg Pictures
[student@servera ~]$ mv film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi Videos
[student@servera ~]$ ls -l Music Pictures Videos
Music:
total 0
-rw-rw-r--. 1 student student 0 May 19 09:23 song1.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song2.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song3.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song4.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song5.mp3
-rw-rw-r--. 1 student student 0 May 19 09:23 song6.mp3

Pictures:
total 0
-rw-rw-r--. 1 student student 0 May 19 09:23 snap1.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap2.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap3.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap4.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap5.jpg
-rw-rw-r--. 1 student student 0 May 19 09:23 snap6.jpg

Videos:
total 0
-rw-rw-r--. 1 student student 0 May 19 09:24 film1.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film2.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film3.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film4.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film5.avi
-rw-rw-r--. 1 student student 0 May 19 09:24 film6.avi
[student@servera ~]$            

5 继续在student用户的主目录中,创建三个子目录,以便将文件整理到项目中。将这些子目录命名为friends、family和work。使用单个命令一次性创建所有三个子目录。

您将使用这些目录把文件重新整理到项目中。

[student@servera ~]$ ls -l
total 0
drwxr-xr-x. 2 student student   6 May 19  2023 Desktop
drwxr-xr-x. 2 student student   6 May 19  2023 Documents
drwxr-xr-x. 2 student student   6 May 19  2023 Downloads
drwxrwxr-x. 2 student student   6 May 19 09:28 family
drwxrwxr-x. 2 student student   6 May 19 09:28 friends
drwxrwxr-x. 2 student student 108 May 19 09:26 Music
drwxrwxr-x. 2 student student 108 May 19 09:26 Pictures
drwxr-xr-x. 2 student student   6 May 19  2023 Public
drwxr-xr-x. 2 student student   6 May 19  2023 Templates
drwxrwxr-x. 2 student student 108 May 19 09:26 Videos
drwxrwxr-x. 2 student student   6 May 19 09:28 work
-rw-rw-r--. 1 student student   0 May 19 09:09 zcat
[student@servera ~]$            

6 将选定的新文件复制到项目目录family和friends中。根据需要,使用多个命令。不必像示例中那样仅使用一个命令。对于每个项目,首先更改到项目目录,然后将源文件复制到此目录中。请记住,您正在制作副本,因此在将文件复制到项目目录后,原始文件会保留在其原始位置。

  • 将含有数字1和2的文件(所有类型)复制到friends子目录。
  • 将含有数字3和4的文件(所有类型)复制到family子目录。

在将文件从多个位置复制到一个位置时,红帽建议您在复制文件之前更改到目标目录。使用最简单的路径语法(绝对或相对路径),到达各项文件管理任务的源位置。

[student@servera ~]$ cd friends
[student@servera friends]$ cp ~/Music/song1.mp3 ~/Music/song2.mp3 ~/Pictures/snap1.jpg ~/Pictures/snap2.jpg ~/Videos/film1.avi ~/Videos/film2.avi .
[student@servera friends]$ ls -l
total 0
-rw-rw-r--. 1 student student 0 May 19 09:30 film1.avi
-rw-rw-r--. 1 student student 0 May 19 09:30 film2.avi
-rw-rw-r--. 1 student student 0 May 19 09:30 snap1.jpg
-rw-rw-r--. 1 student student 0 May 19 09:30 snap2.jpg
-rw-rw-r--. 1 student student 0 May 19 09:30 song1.mp3
-rw-rw-r--. 1 student student 0 May 19 09:30 song2.mp3
[student@servera friends]$ cd ../family
[student@servera family]$ cp ~/Music/song3.mp3 ~/Music/song4.mp3 ~/Pictures/snap3.jpg ~/Pictures/snap4.jpg ~/Videos/film3.avi ~/Videos/film4.avi .
[student@servera family]$ ls -l
total 0
-rw-rw-r--. 1 student student 0 May 19 09:31 film3.avi
-rw-rw-r--. 1 student student 0 May 19 09:31 film4.avi
-rw-rw-r--. 1 student student 0 May 19 09:31 snap3.jpg
-rw-rw-r--. 1 student student 0 May 19 09:31 snap4.jpg
-rw-rw-r--. 1 student student 0 May 19 09:31 song3.mp3
-rw-rw-r--. 1 student student 0 May 19 09:31 song4.mp3
[student@servera family]$            

7 对于您的工作项目,创建额外的副本。

[student@servera family]$ cd ../work
[student@servera work]$ cp ~/Music/song5.mp3 ~/Music/song6.mp3 ~/Pictures/snap5.jpg ~/Pictures/snap6.jpg ~/Videos/film5.avi ~/Videos/film6.avi .
[student@servera work]$ ls -l
total 0
-rw-rw-r--. 1 student student 0 May 19 09:33 film5.avi
-rw-rw-r--. 1 student student 0 May 19 09:33 film6.avi
-rw-rw-r--. 1 student student 0 May 19 09:33 snap5.jpg
-rw-rw-r--. 1 student student 0 May 19 09:33 snap6.jpg
-rw-rw-r--. 1 student student 0 May 19 09:33 song5.mp3
-rw-rw-r--. 1 student student 0 May 19 09:33 song6.mp3
[student@servera work]$            

8 您的项目任务现已完成,是时候清理项目了。

更改到student用户的主目录。尝试通过一个rmdir命令同时删除family和friends项目目录。

[student@servera work]$ cd
[student@servera ~]$ rmdir family friends
rmdir: failed to remove 'family': Directory not empty
rmdir: failed to remove 'friends': Directory not empty
[student@servera ~]$            

使用rmdir命令应该会失败,因为两个子目录都包含文件。

9 使用rm -r命令以递归方式删除family和friends子目录及其内容。

[student@servera ~]$ rm -r family friends
[student@servera ~]$ ls -l
total 0
drwxr-xr-x. 2 student student   6 May 19  2023 Desktop
drwxr-xr-x. 2 student student   6 May 19  2023 Documents
drwxr-xr-x. 2 student student   6 May 19  2023 Downloads
drwxrwxr-x. 2 student student 108 May 19 09:26 Music
drwxrwxr-x. 2 student student 108 May 19 09:26 Pictures
drwxr-xr-x. 2 student student   6 May 19  2023 Public
drwxr-xr-x. 2 student student   6 May 19  2023 Templates
drwxrwxr-x. 2 student student 108 May 19 09:26 Videos
drwxrwxr-x. 2 student student 108 May 19 09:33 work
-rw-rw-r--. 1 student student   0 May 19 09:09 zcat
[student@servera ~]$            

10 删除工作项目中的所有文件,但不要删除work目录本身。

[student@servera ~]$ cd work
[student@servera work]$ rm song5.mp3 song6.mp3 snap5.jpg snap6.jpg film5.avi film6.avi 
[student@servera work]$ ls -l
total 0
[student@servera work]$            

11 最后,从student用户的主目录,使用rmdir命令删除work目录。该命令现在应该成功完成,因为目录已为空。

[student@servera work]$ cd
[student@servera ~]$ rmdir work
[student@servera ~]$ ls -l
total 0
drwxr-xr-x. 2 student student   6 May 19  2023 Desktop
drwxr-xr-x. 2 student student   6 May 19  2023 Documents
drwxr-xr-x. 2 student student   6 May 19  2023 Downloads
drwxrwxr-x. 2 student student 108 May 19 09:26 Music
drwxrwxr-x. 2 student student 108 May 19 09:26 Pictures
drwxr-xr-x. 2 student student   6 May 19  2023 Public
drwxr-xr-x. 2 student student   6 May 19  2023 Templates
drwxrwxr-x. 2 student student 108 May 19 09:26 Videos
-rw-rw-r--. 1 student student   0 May 19 09:09 zcat
[student@servera ~]$            

12 从servera退出。

[student@servera ~]$ exit
logout
Connection to servera closed.
[student@workstation ~]$            

继续阅读