实验报告:Linux指令入门-文件与权限

简介: 云起实验室:Linux指令入门-文件与权限的实验报告

    这个实验虽然非常简单,只是一些最基本的linux文件与权限的操作,里面也有一些实用的技巧和容易忽视的细节,这个报告对此做了详细的记录和说明,对运维和管理应该稍有帮助,方便日后自己查阅,各位小伙伴也看一眼,或许有你平常不注意的地方。

1 文件管理

     创建、显示、移动、删除、重命名文件和目录是我们运维人员经常需要做的操作,涉及到了几个linux系统中的最基本的命令,正确使用这些命令是系统运维人员的最基本的要求。

1.1 文件和目录显示

一个最基本的操作时显示当前所在的目录,命令时pwd

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# pwd

/root

这个命令应该时系统运维人员经常用的命令,做每个操作之前最好运行一下这个命令,看一在当前目录,然后再执行其它操作,尤其时使用相对路径删除文件或目录时,相对路径只以用户当前工作路径为起点的,如果当前路径不对,可能误删文件或目录,导致不可预料的后果。


显示文件的命令时ls,这个命令有几个实用的选项,简单运行ls命令显示当前目录下的所有文件,不包含隐藏文件

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls

当前目录下没有任何文件,加上-a选项,就可以显示当前目录下的隐藏文件了。

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -a

.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .pip  .pydistutils.cfg  .ssh  .tcshrc

. 表示当前目录,.. 表示上一级目录,.bash_progfile 是bash shell下当前用户的配置文件,我们可以在里面设置用户常用的环境变量。ls加-l选项可以显示文件或目录的详细信息

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -l

total 0

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -la

total 44

dr-xr-x---.  4 root root 4096 Jul 11 08:56 .

dr-xr-xr-x. 18 root root 4096 Jul 11 08:55 ..

-rw-------   1 root root   95 Jul 11 08:56 .bash_history

-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout

-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile

-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc

-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc

drwxr-xr-x   2 root root 4096 Apr 26  2020 .pip

-rw-r--r--   1 root root  206 Jul 11 08:55 .pydistutils.cfg

drwx------   2 root root 4096 Apr 26  2020 .ssh

-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc

ls 的-t选项可以按照时间顺序排列文件

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -at

.  .bash_history  ..  .pydistutils.cfg  .pip  .ssh  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc

可以看出不是字母顺序,加l选项查看一下详细信息

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -alt

total 44

dr-xr-x---.  4 root root 4096 Jul 11 08:56 .

-rw-------   1 root root   95 Jul 11 08:56 .bash_history

dr-xr-xr-x. 18 root root 4096 Jul 11 08:55 ..

-rw-r--r--   1 root root  206 Jul 11 08:55 .pydistutils.cfg

drwxr-xr-x   2 root root 4096 Apr 26  2020 .pip

drwx------   2 root root 4096 Apr 26  2020 .ssh

-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout

-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile

-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc

-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc

-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc

-r选项可以反向显示

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -ra

.tcshrc  .ssh  .pydistutils.cfg  .pip  .cshrc  .bashrc  .bash_profile  .bash_logout  .bash_history  ..  .

从上面的输出可以看出,显示的顺序是按照字母顺序反向显示的。

-R选项是递归显示当前目录,即显示每一级子目录的信息

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -Ral

.:

total 44

dr-xr-x---.  4 root root 4096 Jul 11 08:56 .

dr-xr-xr-x. 18 root root 4096 Jul 11 08:55 ..

-rw-------   1 root root   95 Jul 11 08:56 .bash_history

-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout

-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile

-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc

-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc

drwxr-xr-x   2 root root 4096 Apr 26  2020 .pip

-rw-r--r--   1 root root  206 Jul 11 08:55 .pydistutils.cfg

drwx------   2 root root 4096 Apr 26  2020 .ssh

-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc

./.pip:

total 12

drwxr-xr-x  2 root root 4096 Apr 26  2020 .

dr-xr-x---. 4 root root 4096 Jul 11 08:56 ..

-rw-r--r--  1 root root  252 Jul 11 08:55 pip.conf

./.ssh:

total 8

drwx------  2 root root 4096 Apr 26  2020 .

dr-xr-x---. 4 root root 4096 Jul 11 08:56 ..

-rw-------  1 root root    0 Jul 11 08:55 authorized_keys

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll -a

total 44

dr-xr-x---.  4 root root 4096 Jul 11 08:56 .

dr-xr-xr-x. 18 root root 4096 Jul 11 08:55 ..

-rw-------   1 root root   95 Jul 11 08:56 .bash_history

-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout

-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile

-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc

-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc

drwxr-xr-x   2 root root 4096 Apr 26  2020 .pip

-rw-r--r--   1 root root  206 Jul 11 08:55 .pydistutils.cfg

drwx------   2 root root 4096 Apr 26  2020 .ssh

-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc

1.2 改变当前目录

用cd命令改变用户的当前路径

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# cd /usr/local/etc

[root@iZuf6jb1biwnrz7sk87e7sZ etc]# pwd

/usr/local/etc

已经进入到/usr/local/etc 路径下

cd ..可以进入到上级目录,cd ~ 可以进入到当前用户的home目录下,直接键入cd 也可以转到用户的home目录下

[root@iZuf6jb1biwnrz7sk87e7sZ etc]# cd

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# pwd

/root

可以看到当前目录变为root用户的home目录

1.3 文件的创建和调整

用touch命令创建空文件

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# touch demo1.txt demo2.txt

ll是系统定义的别名,运行的命令是ls -l

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 0

-rw-r--r-- 1 root root 0 Jul 11 09:02 demo1.txt

-rw-r--r-- 1 root root 0 Jul 11 09:02 demo2.txt

也可用touch命令更给文件的调整时间,后面直接跟上要调整的文件名即可,

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# touch demo1.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 0

-rw-r--r-- 1 root root 0 Jul 11 09:03 demo1.txt

-rw-r--r-- 1 root root 0 Jul 11 09:02 demo2.txt

可以看到,demo1.txt的时间变为当前的时间,-r选项可以依据一个文件改变另一个文件的时间,比如要根据demo1.txt时间更改demo2.txt的时间,命令应该这样写

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# touch -r demo2.txt demo1.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 0

-rw-r--r-- 1 root root 0 Jul 11 09:02 demo1.txt

-rw-r--r-- 1 root root 0 Jul 11 09:02 demo2.txt

可以看到demo2.txt的时间已经改成了demo1.txt的时间。


1.4 目录操作

用mkdir命令创建目录,-p选项创建不存在的父目录

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# mkdir -p a/b/c/d

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -lR

.:

total 4

drwxr-xr-x 3 root root 4096 Jul 11 09:04 a

-rw-r--r-- 1 root root    0 Jul 11 09:02 demo1.txt

-rw-r--r-- 1 root root    0 Jul 11 09:02 demo2.txt

./a:

total 4

drwxr-xr-x 3 root root 4096 Jul 11 09:04 b

./a/b:

total 4

drwxr-xr-x 3 root root 4096 Jul 11 09:04 c

./a/b/c:

total 4

drwxr-xr-x 2 root root 4096 Jul 11 09:04 d

./a/b/c/d:

total 0

可以看到创建a,b,c,d目录,a,b,c是d的各级父目录,也可以用tree命令查看,更直观一些,这个命令在有的linux系统上需要单独安装。

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# tree

.

├── a

│   └── b

│       └── c

│           └── d

├── demo1.txt

└── demo2.txt

4 directories, 2 files

删除文件和目录一般实用rm 命令,-r选项可以删除目录及其各级子目录,以及目录级各级子目录下的文件,这个选项应该慎用,运行命令之前应该对要执行的操作有准确的认知,按下回车之前应该检查屏幕上的命令是否是自己要执行的,多余的空格可能造成不可挽回的后果,尤其是rm -rf  * 之前,一定要确认当前的工作目录,不可键入命令后随手回车。

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# rm -rf demo*

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

drwxr-xr-x 3 root root 4096 Jul 11 09:04 a

drwxr-xr-x 3 root root 4096 Jul 11 09:04 a

这个命令删除了所有名字以demo开头的文件和目录

要删除目录a下面b子目录,命令应该这样写

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# rm -rf a/b

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# tree

.

└── a

1 directory, 0 files

可以看到已经删除了b目录下的文件及子目录。用cp命令拷贝文件及目录,-r选项拷贝文件及各个级别的子目录及文件。

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# cp -r c a/b/

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# tree

.

├── a

│   └── b

│       └── c

│           └── d

└── c

   └── d

6 directories, 0 files

用mv命令移动文件和目录

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# touch a.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# mv a.txt b.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 8

drwxr-xr-x 3 root root 4096 Jul 11 09:07 a

-rw-r--r-- 1 root root    0 Jul 11 09:09 b.txt

drwxr-xr-x 3 root root 4096 Jul 11 09:07 c

a.txt被移动到了b.txt,a.txt文件已经被删除。也可以移动目录

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# mv c a/b/c/d

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# tree

.

├── a

│   └── b

│       └── c

│           └── d

│               └── c

│                   └── d

└── b.txt

6 directories, 1 file

将当前目录下所有内容移动到/tmp目录下

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# mv ./* /tmp

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -l /tmp

total 27804

drwxr-xr-x 3 root root     4096 Jul 11 09:07 a

drwxr-xr-x 3 root root     4096 Jul 11 08:55 aliyun_assist_6E9FF212AC6DEC9207A0959101945F57

-rw-r--r-- 1 root root 28453387 Jul 11 08:55 aliyun_assist_6E9FF212AC6DEC9207A0959101945F57.zip

-rw------- 1 root root        0 Jul 11 08:55 AliyunAssistClientSingleLock.lock

srwxrwxrwx 1 root root        0 Jul 11 08:55 argus.sock

-rw-r--r-- 1 root root        0 Jul 11 09:09 b.txt

drwx------ 3 root root     4096 Jul 11 08:55 systemd-private-16b1a8d8373a49e29d79467dc5322268-chronyd.service-SGpg13

drwx------ 2 root root     4096 Jul 11 08:55 tmp.I0uHH4StYu

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# tree /tmp/a

/tmp/a

└── b

   └── c

       └── d

           └── c

               └── d

5 directories, 0 files

1.5 文件重命名

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# touch demo1.txt demo2.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls

demo1.txt  demo2.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# rename demo DEMO *

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls

DEMO1.txt  DEMO2.txt

可以看到,当前目录下所有以demo开头的文件名字被改为以DEMO开头。

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# rename .txt .text *

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls

DEMO1.text  DEMO2.text

文件的后缀由txt改成了text。

2 文件权限管理

2.1 显示文件的权限信息

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -l /boot/

total 140804

-rw-r--r--  1 root root   153187 Mar 18  2020 config-3.10.0-1062.18.1.el7.x86_64

-rw-r--r--. 1 root root   152976 Aug  8  2019 config-3.10.0-1062.el7.x86_64

drwxr-xr-x. 3 root root     4096 Apr 26  2020 efi

drwxr-xr-x. 2 root root     4096 Apr 26  2020 grub

drwx------. 5 root root     4096 Apr 26  2020 grub2

-rw-------. 1 root root 57931787 Apr 26  2020 initramfs-0-rescue-20200426154603174201708213343640.img

-rw-------  1 root root 18197454 Apr 26  2020 initramfs-3.10.0-1062.18.1.el7.x86_64.img

-rw-------  1 root root 10734218 Apr 26  2020 initramfs-3.10.0-1062.18.1.el7.x86_64kdump.img

上面的显示信息中,

第1位表示存档类型,d表示目录,-表示一般文件。

第2~4位表示当前用户的权限(属主权限)。

第5~7位表示同用户组的用户权限(属组权限)。

第8~10位表示不同用户组的用户权限(其他用户权限)。

第11位是一个半角句号.,表示SELinux安全标签。

2.2 文件权限更改

创建一个hello.sh 脚本文件

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# echo "echo 'Hello World'" > hello.sh

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO2.text

-rw-r--r-- 1 root root 19 Jul 11 09:15 hello.sh

用chmod命令给脚本加上可执行权限

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# chmod u+x hello.sh  

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO2.text

-rwxr--r-- 1 root root 19 Jul 11 09:15 hello.sh

撤销可执行权限

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# chmod u-x hello.sh  

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO2.text

-rw-r--r-- 1 root root 19 Jul 11 09:15 hello.sh

也可以实用数字方式改变文件权限,4读,2为写,1为执行,744表示owner用于读、写、执行权限,组内其它成员和组外成员用于写权限

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# chmod 744 hello.sh  

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO2.text

-rwxr--r-- 1 root root 19 Jul 11 09:15 hello.sh

用户拥有文件的执行权限才能执行文件

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# bash hello.sh

Hello World

2.2 更改文件属主及属组

查看当前用户

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# whoami

root

这个命令应该也是系统运维经常运行的命令,做任何操作之前先确认当前用户是必要的审慎行为,可以避免很多不必要的麻烦。

创建一个文件

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# touch test.txt

创建test用户

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# adduser test

为test用户创建密码

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# passwd test

Changing password for user test.

New password:  

BAD PASSWORD: The password contains the user name in some form

Retype new password:  

passwd: all authentication tokens updated successfully.

创建admin用户

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# adduser admin

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# passwd admin

Changing password for user admin.

New password:  

BAD PASSWORD: The password contains the user name in some form

Retype new password:  

passwd: all authentication tokens updated successfully.

用chown命令更改文件的属主

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# chown test test.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ls -l

total 4

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root root  0 Jul 11 09:11 DEMO2.text

-rwxr--r-- 1 root root 19 Jul 11 09:15 hello.sh

-rw-r--r-- 1 test root  0 Jul 11 09:18 test.txt

文件的属主已经被改为test,属组仍然是root

同时更改属组和属主,仍然用chown命令,像下面这样写

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# chown admin:admin test.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

-rw-r--r-- 1 root  root   0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root  root   0 Jul 11 09:11 DEMO2.text

-rwxr--r-- 1 root  root  19 Jul 11 09:15 hello.sh

-rw-r--r-- 1 admin admin  0 Jul 11 09:18 test.txt

只改文件的属组可以用chgrp

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# chgrp root test.txt

[root@iZuf6jb1biwnrz7sk87e7sZ ~]# ll

total 4

-rw-r--r-- 1 root  root  0 Jul 11 09:11 DEMO1.text

-rw-r--r-- 1 root  root  0 Jul 11 09:11 DEMO2.text

-rwxr--r-- 1 root  root 19 Jul 11 09:15 hello.sh

-rw-r--r-- 1 admin root  0 Jul 11 09:18 test.txt

相关文章
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
1108 0
7种比较Linux中文本文件的最佳工具
|
Linux 数据安全/隐私保护
Linux文件权限调整:chmod指令解析
在日常使用或系统管理中,对 `chmod`的熟练应用是提高工作效率和维持系统稳定性的关键。掌握如何给予或限制权限是每个Linux使用者都应该具备的技能。
947 17
|
存储 数据管理 Linux
区分Linux中.tar文件与.tar.gz文件的不同。
总之,".tar"文件提供了一种方便的文件整理方式,其归档但不压缩的特点适用于快速打包和解压,而".tar.gz"文件通过额外的压缩步骤,尽管处理时间更长,但可以减小文件尺寸,更适合于需要节约存储空间或进行文件传输的场景。用户在选择时应根据具体需求,考虑两种格式各自的优劣。
2513 13
|
Linux
【Linux】 Linux文件I/O常见操作技巧
以上就是Linux文件I/O操作的一些技巧,接纳它们,让它们成为你在Linux世界中的得力伙伴,工作会变得轻松许多。不过记住,技巧的运用也需要根据实际情况灵活掌握,毕竟,最适合的才是最好的。
455 28
|
安全 Linux
Linux赋予文件000权限的恢复技巧
以上这些步骤就像是打开一扇锁住的门,步骤看似简单,但是背后却有着严格的逻辑和规则。切记,在任何时候,变更文件权限都要考虑安全性,不要无谓地放宽权限,那样可能
560 16
|
移动开发 Linux Windows
Linux常用基本指令
对Linux中常用命令,包括文件创建与销毁,压缩与打包,查看搜索文件的方式等指令的介绍
493 1
|
存储 Linux 数据处理
深入剖析Linux中一切即文件的哲学和重定向的机制
在计算机的奇妙世界中,Linux的这套哲学和机制减少了不同类型资源的处理方式,简化了抽象的概念,并蕴藏着强大的灵活性。就像变戏法一样,轻轻松松地在文件、程序与设备之间转换数据流,标准输入、输出、错误流就在指尖舞动,程序的交互和数据处理因此变得既高效又富有乐趣。
337 4
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
4426 12
|
Linux Shell
Linux系统下快速批量创建和删除文件的方法
总的来说,使用shell脚本来批量处理文件是一种非常强大的工具,只要你愿意花时间学习和实践,你会发现它能大大提高你的工作效率。
964 19
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
879 3