练习2
(1)使用pwd命令查看当前工作目录,使用ls命令查看当前目录下有哪些内容。
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls
anaconda-ks.cfg example.c file11 m1.c m2.c m3.c newfile newfile1 test
(2)使用cd命令切换到tmp目录,使用pwd命令检查当前工作目录是否改变。
[root@localhost ~]# cd /tmp
[root@localhost tmp]# pwd
/tmp
(3)使用-l选项查看tmp目录下的详细信息。在这一步中,要求同学们根据输出的第一个字符判断文件的类型,即判断哪些是目录,哪些是普通文件;使用-a选项查看隐藏文件,观察隐藏文件的特点。
ls -l
:查看目录下的详细信息
-开头:普通文件
c开头:字符设备文件
d开头:目录文件
[root@localhost tmp]# ls -l
total 4
-rwx------. 1 root root 836 Oct 19 21:03 ks-script-KVrC5w
drwx------. 3 root root 17 Oct 19 23:59 systemd-private-23cd026782854f5585ba18e6d4beeca3-chronyd.service-sW9KfH
drwx------. 3 root root 17 Oct 26 09:45 systemd-private-a674722a40694713854e3d1ad67a2d3c-chronyd.service-T82FRi
drwx------. 2 root root 6 Oct 19 23:59 vmware-root_534-2957583592
drwx------. 2 root root 6 Oct 19 23:34 vmware-root_537-4257134911
drwx------. 2 root root 6 Oct 19 23:49 vmware-root_538-2999460707
drwx------. 2 root root 6 Oct 19 23:44 vmware-root_539-4248811709
drwx------. 2 root root 6 Oct 20 02:03 vmware-root_542-2991268582
drwx------. 2 root root 6 Oct 19 18:23 vmware-root_545-4257003801
drwx------. 2 root root 6 Oct 26 09:45 vmware-root_550-2991137472
drwx------. 2 root root 6 Oct 19 21:04 vmware-root_552-2957583561
-rw-------. 1 root root 0 Oct 19 20:57 yum.log
ls -a
:查看隐藏文件
[root@localhost tmp]# ls -a
. vmware-root_534-2957583592
.. vmware-root_537-4257134911
.ICE-unix vmware-root_538-2999460707
.Test-unix vmware-root_539-4248811709
.X11-unix vmware-root_542-2991268582
.XIM-unix vmware-root_545-4257003801
.font-unix vmware-root_550-2991137472
ks-script-KVrC5w vmware-root_552-2957583561
systemd-private-23cd026782854f5585ba18e6d4beeca3-chronyd.service-sW9KfH yum.log
systemd-private-a674722a40694713854e3d1ad67a2d3c-chronyd.service-T82FRi
(4)使用cat命令查看文件file1的内容,并显示行号。
cat file1 -n
方式会将空白行也标上序号
cat file1 -b
方式则不会
[root@localhost tmp]# cat > file1
test
today is Monday
Linux
[root@localhost tmp]# cat file1
test
today is Monday
Linux
[root@localhost tmp]# cat file1 -n
1 test
2 today is Monday
3
4 Linux
[root@localhost tmp]# cat file1 -b
1 test
2 today is Monday
3 Linux
(5)在tmp目录下创建子目录dir2、文件file2及file3。将file1复制到目录dir1中,复制后的文件名为file1.bak。将file2移动到dir2中,将file3重命名为file3.bak。
①将file1复制到目录dir1中,复制后的文件名为file1.bak:
[root@localhost ~]# cd /tmp
[root@localhost tmp]# mkdir dir1
[root@localhost tmp]# mkdir dir2
[root@localhost tmp]# touch file2
[root@localhost tmp]# touch file3
[root@localhost tmp]# ls
dir1 file3
dir2 systemd-private-52d1682975934dae93bcba3b60fc9a91-chronyd.service-si6Ff3
file1 vmware-root_540-2999591780
file2 vmware-root_550-2991137472
[root@localhost tmp]# cp file1 dir1/file1.bak
[root@localhost tmp]# cd dir1
[root@localhost dir1]# ls
file1.bak
②将file2移动到dir2中:
[root@localhost tmp]# cp file2 dir2/
[root@localhost tmp]# cd dir2
[root@localhost dir2]# ls
file2
③将file3重命名为file3.bak:
[root@localhost dir2]# cd /tmp
[root@localhost tmp]# ls
dir1 file3
dir2 systemd-private-52d1682975934dae93bcba3b60fc9a91-chronyd.service-si6Ff3
file1 vmware-root_540-2999591780
file2 vmware-root_550-2991137472
[root@localhost tmp]# mv file3 file3.bak
[root@localhost tmp]# ls
dir1 file3.bak
dir2 systemd-private-52d1682975934dae93bcba3b60fc9a91-chronyd.service-si6Ff3
file1 vmware-root_540-2999591780
file2 vmware-root_550-2991137472
(6)用grep命令在profile文件中对关键字then进行查询,并与上面的结果比较。
[root@localhost tmp]# find / -name "profile"
/etc/profile
[root@localhost tmp]# grep then /etc/profile
if [ "$2" = "after" ] ; then
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
if [ "$EUID" = "0" ]; then
if [ "$HISTCONTROL" = "ignorespace" ] ; then
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
find / -name "profile"
:一开始不知道profile文件在哪里,所以需要进行查询
grep then /etc/profile
:用grep命令在profile文件中对关键字then进行查询
(7)给文件profile创建一个符号链接lnsprofile和一个硬链接lnhprofile。
[root@localhost ~]# cd /etc
[root@localhost etc]# ln -s profile lnsprofile
[root@localhost etc]# ln profile lnhprofile
(8)长格式显示文件profile、lnsprofile和lnhprofile的详细信息。注意比较3个文件链接数的不同。
[root@localhost etc]# ls -l profile lnsprofile lnhprofile
-rw-r--r--. 3 root root 1819 4月 1 2020 lnhprofile
lrwxrwxrwx. 1 root root 7 11月 2 15:27 lnsprofile -> profile
-rw-r--r--. 3 root root 1819 4月 1 2020 profile
分析:
①软链接是可以通过ls -l看到指向的,而硬链接不能
②看3个文件的链接数:
软链接lnsprofile:链接数=1,
硬链接lnhprofile:链接数=3,其中1个是我们创建的,还有两个分别代表当前目录和上级目录(可参考:)
源文件profile:链接数=3,
(9)删除文件profile,用长格式显示文件lnsprofile和lnhprofile的详细信息,比较文件lnhprofile的链接数的变化。
[root@localhost etc]# rm profile
rm: remove regular file 'profile'? yes
[root@localhost etc]# ls -l lnsprofile lnhprofile
-rw-r--r--. 2 root root 1819 Apr 1 2020 lnhprofile
lrwxrwxrwx. 1 root root 7 Nov 2 15:27 lnsprofile -> profile
结果:
硬链接lnhprofile的数量减1
软链接lnsprofile的数量不变
其实,在SSH中命令显示的情况如下:
黑底红字:表示这是无效链接(broken link)
关于SSH中的颜色显示,可参考:linux中颜色的含义
(10)用less命令查看文件lnsprofile的内容,看看有什么结果。
[root@localhost etc]# ls lnsprofile
显示如下:
(11)删除文件lnsprofile,显示当前目录下的文件列表,回到上层目录。
[root@localhost etc]# rm lnsprofile
rm: remove symbolic link 'lnsprofile'? yes
[root@localhost etc]# ls
[root@localhost etc]# cd ..
(12)把文件test.tar.gz改名为backup.tar.gz。
//查询是否有压缩文件test.tar.gz
[root@localhost /]# find / -name "test.tar.gz"
//查询为无,则进入有test文件的目录root下,找到test文件进行压缩
[root@localhost /]# cd root
[root@localhost ~]# ls
anaconda-ks.cfg example.c file11 m1.c m2.c m3.c newfile newfile1 test
//打包test文件为test.tar
[root@localhost ~]# tar -cvf test.tar test
test
//用gzip命令把打好的包进行压缩
[root@localhost ~]# gzip test.tar
//显示当前目录下的文件和目录列表,确认打包压缩成功
[root@localhost ~]# ls
anaconda-ks.cfg file11 m2.c newfile test
example.c m1.c m3.c newfile1 test.tar.gz
//把文件test.tar.gz改名为backup.tar.gz
[root@localhost ~]# mv test.tar.gz backup.tar.gz
(13)显示当前目录下的文件和目录列表,确认重命名成功。
[root@localhost ~]# ls
anaconda-ks.cfg example.c m1.c m3.c newfile1
backup.tar.gz file11 m2.c newfile test
(14)把文件backup.tar.gz移动到test目录下。
[root@localhost ~]# mv backup.tar.gz test
(15)显示当前目录下的文件和目录列表,确认移动成功。
[root@localhost ~]# ls
anaconda-ks.cfg example.c file11 m1.c m2.c m3.c newfile newfile1 test
(16)进入test目录,显示目录中的文件列表。
[root@localhost ~]# cd test
[root@localhost test]# ls
backup.tar.gz
(17)显示当前目录下的文件和目录列表,复制test目录为testbak目录作为备份。
【注意】使用cp指令复制目录时,必须使用参数 -r 或者 -R
[root@localhost ~]# cp -R test testbak
[root@localhost ~]# ls
anaconda-ks.cfg file11 m2.c newfile test
example.c m1.c m3.c newfile1 testbak
(18)查找root用户自己的主目录下的所有名为newfile的文件。
[root@localhost ~]# sudo find /root -name newfile
/root/newfile
(19)删除test子目录下的所有文件。
[root@localhost ~]# rm -R test/*
rm: remove regular file 'test/backup.tar.gz'? yes
//验证是否删除成功
[root@localhost ~]# ls
anaconda-ks.cfg file11 m2.c newfile test
example.c m1.c m3.c newfile1 testbak
[root@localhost ~]# cd test
[root@localhost test]# ls -l
total 0
(20)利用rmdir命令删除空子目录test。
[root@localhost ~]# rmdir test
(21)回到上层目录,利用rm命令删除目录test和其下所有文件。
//回到上层目录(其实我不太清楚这里回到上层目录的意义何在?现在上层目录中又没有test文件,还是只能在/root路径下删除才行)
[root@localhost ~]# cd ..
//所以以下指令还是在/root路径下执行的(之前只好又用mkdir命令创建了一次test文件)
[root@localhost ~]# rm -R test
rm: remove directory 'test'? yes
【一些补充】
(1)删除目录
用rm -rf
,可以强制直接删除目录,不会事先询问(-f:表示force,强制删除)
(2)返回上一级目录
cd ..
返回根目录
cd /