计算机三级Linux应用与开发技术知识总结(1)——文件管理

基本概念:

        文件:文件系统中存储数据的一个命名对象。它是用户眼中最小的逻辑存储单元,所有文件都由数据项和文件控制块组成,文件控制块又被称为索引节点。Linux系统中一切都是文件,硬件设备也是文件。根据文件的数据项是否有结构,可以将文件分为无结构文件和有结构文件两大类。但通常情况下Linux文件被划分为以下几种类型:

                (1)普通文件:不包含文件系统的结构信息,这种类型的文件按其内部结构又可分为文本文件和二进制文件。

                (2)目录文件:用于存放文件名及其相关信息,它是内核组织文件系统的基本节点。目录文件可以包含下一级文件目录或普通文件。

                (3)链接文件:指向一个真实文件的链接,类似于Windows系统的快捷方式。这种类型的文件又可以细分为硬链接文件和符号链接文件。

                (4)设备文件:Linux系统为外部设备提供一种标准的接口,将外部设备视为一种特殊文件,用户可以向访问普通文件一样访问任何外部设备。根据访问数据方式的不同,设备文件又可以分为块设备和字符设备。

        目录:包含许多文件控制块项目的一类特殊文件。目录支持文件系统的层次结构。文件系统中的每个文件都登记在一个或多个目录中。

       子目录:被包含在另一目录中的目录。包含子目录的目录称为父目录。除了根目录外,所有目录都是子目录。

        绝对路径:从根目录开始写起的文件或目录名称。

        相对路径:相对于当前路径的文件或目录名称。

Linux系统文件和目录的命名规则

(1)除了字符“/”外,所有字符均可以使用,但应尽量避免使用<、>、?、*和非打印字符等。如果一个文件名中包含了特殊字符,如空格,那么访问该文件的时候就需要使用引号将文件名括起来。

(2)目录名或文件名的长度不能超过255个字符。

(3)目录名和文件名是区分大小写的。

(4)与Windows系统不同,文件的扩展名对Linux系统没有特殊含义。

注意:如果一个文件名或目录是以“.”开始,则表示该文件或目录是一个隐藏文件或目录,以默认方式查找不会显示该文件。

文件操作命令

(1)cat 命令

#cat [文件名]:显示文件的所有内容。
[root@6c810f7f0bd8 ~]# cat file
this is frist line.
this is second line.
this is third line.

#对所有行进行编号
[root@6c810f7f0bd8 ~]# cat -n file
     1  this is frist line.
     2  this is second line.
     3  this is third line.
#cat -b 只对非空行编号
#cat -s 将连续的多个空行替换为1行
[root@6c810f7f0bd8 ~]# cat -bs file1

     1  this is fourth line in file1.

#显示多个文件的内容
[root@6c810f7f0bd8 ~]# cat file file1
this is frist line.
this is second line.
this is third line.



this is fourth line in file1.

#将内容复制到目标文件,目标若不存在将被创建
[root@6c810f7f0bd8 ~]# cat file > file2
[root@6c810f7f0bd8 ~]# cat file2
this is frist line.
this is second line.
this is third line.

(2)more 命令 

#分页显示文本文件内容
[root@6c810f7f0bd8 ~]# more file
this is frist line.
this is second line.
this is third line.
this is fourt line.
this is fifth line.
....
this is twenty-seventh line.
this is twenty-eighth line.
this is twenty-ninth line.
--More--(96%)

# -c或 -p 先清屏后显示内容
# -n 一次显示的行数
# +n 从第n行开始显示

[root@6c810f7f0bd8 ~]# more -p10 file

this is frist line.
this is second line.
this is third line.
this is fourt line.
this is fifth line.
this is sixth line.
this is seventh line.
this is eighth line.
this is ninth line.
this is tenth line.
--More--(28%)

#按下空格键

this is eleventh line.
this is twelfth line.
this is thirteenth line.
this is fourteenth line.
this is fifteenth line.
this is sixteenth line.
this is seventeenth line.
this is eighteenth line.
this is nineteenth line.
this is twentieth line.
--More--(62%)

#按下空格键

--More--(62%)
this is twenty-frist line.
this is twenty-second line.
this is twenty-third line.
this is twenty-fourth line.
this is twenty-fifth line.
this is twenty-sixth line.
this is twenty-seventh line.
this is twenty-eighth line.
this is twenty-ninth line.
this is thirtieth line.

# -s 将连续多个空行替换为1个空行

less命令兼容了more命令,并且提供了更多功能。

(3)head 命令

# 显示文件的前几行
[root@6c810f7f0bd8 ~]# head file
this is frist line.
this is second line.
this is third line.
this is fourt line.
this is fifth line.
this is sixth line.
this is seventh line.
this is eighth line.
this is ninth line.
this is tenth line.

#-k 或 -n k显示文件前 k行内容
[root@6c810f7f0bd8 ~]# head -3 file
this is frist line.
this is second line.
this is third line.

#-c k 显示文件前k个字节的内容
[root@6c810f7f0bd8 ~]# head -c10 file
this is fr
#-v 显示文件名

(4)tail 命令

# 显示文件的末尾几行
[root@6c810f7f0bd8 ~]# tail file
this is twenty-frist line.
this is twenty-second line.
this is twenty-third line.
this is twenty-fourth line.
this is twenty-fifth line.
this is twenty-sixth line.
this is twenty-seventh line.
this is twenty-eighth line.
this is twenty-ninth line.
this is thirtieth line.

#-k 或 -n k显示文件最后 k行内容
[root@6c810f7f0bd8 ~]# tail -3 file
this is twenty-eighth line.
this is twenty-ninth line.
this is thirtieth line.

#-c k 显示文件最后k个字节的内容
[root@6c810f7f0bd8 ~]# tail -c10 file
eth line.

#-f 监听文件新增加的内容

(5)touch 命令

linux系统中文件具有三个时间参数

[root@6c810f7f0bd8 ~]# stat file
  File: file
  Size: 719             Blocks: 8          IO Block: 4096   regular file
Device: a1h/161d        Inode: 16093       Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-09-12 09:40:38.891634000 +0000
Modify: 2021-09-12 09:40:37.531634000 +0000
Change: 2021-09-12 09:40:37.531634000 +0000
 Birth: -

访问时间在文件内容被读取时会更新,比如用cat查看文件内容;

数据修改时间在文件数据被修改时会更新;

状态事件在文件状态改变时会更新,比如权限被修改。

# touch 创建文件:若文件不存在将被创建,若存在则修改文件的3个时间参数

#-a 只修改文件的访问时间
#-c 修改文件的3个时间参数,若文件不存在则不会创建
#-m 只修改文件的数据修改时间
#-t 自定义事件

(6)grep 命令

# -F 字符串查找模式
[root@6c810f7f0bd8 ~]# grep -F twenty file
this is twenty-frist line.
this is twenty-second line.
this is twenty-third line.
this is twenty-fourth line.
this is twenty-fifth line.
this is twenty-sixth line.
this is twenty-seventh line.
this is twenty-eighth line.
this is twenty-ninth line.

# -E 正则表达式查找模式

[root@6c810f7f0bd8 ~]# grep -E [abc] file
this is second line.
this is twenty-second line.

# -c 列出文件中包含模式的行数
[root@6c810f7f0bd8 ~]# grep -F twenty -c file
9

# -i 忽略字母大小写
# -n 行首列出行号
# -v 列出没有匹配模式的行

(7)sed 命令

sed是一种流编辑器,他会将当前处理的行存储在临时缓冲区中,称为“模式空间”,文件内容并不会改变。

# sed常用编辑指令:
    # p 打印匹配行
    # d 删除指定行
    # a 在匹配行后追加
    # i 在匹配行前追加
    # c 整行替换
    # r 读取文件内容
    # w 将文本写入文件
    # 字符串替换(匹配正则表达式)

# -n 只显示匹配处理的行(否则会输出所有)。

#显示前两行
[root@6c810f7f0bd8 ~]# sed -n '1,2p' file
this is frist line.
this is second line.

#删除第1行
[root@6c810f7f0bd8 ~]# sed '1d' file
this is second line.
this is third line.
this is fourt line.
this is fifth line.
this is sixth line.
...
this is thirtieth line.

#在第2行后追加
[root@6c810f7f0bd8 ~]# sed '2a abc' file
this is frist line.
this is second line.
abc
this is third line.
...
this is thirtieth line.

#将第一行替换为abc
[root@6c810f7f0bd8 ~]# sed '1c abc' file
abc
this is second line.
this is third line.
...
this is thirtieth line.

#将文本中的frist替换为1st
[root@6c810f7f0bd8 ~]# sed 's/frist/1st/' file
this is 1st line.
this is second line.
this is third line.
...
this is thirtieth line.

(8)awk 命令

# 数据字段的变量代表的含义:
# $0 : 整个文本行
# $n : 文本行的第n个数据字段

#显示文本第二个数据段
[root@6c810f7f0bd8 ~]# cat file
frist line of this text.
second line of this text.
third line of this text.
[root@6c810f7f0bd8 ~]# awk '{print $2}' file
line
line
line

# 使用分号分割多条命令
[root@6c810f7f0bd8 ~]# echo "this is a apple"|awk '{$4 = "peacth";print $0}'
this is a peacth

# 使用BEGIN和END在awk处理数据前后执行
[root@6c810f7f0bd8 ~]# awk 'BEGIN {print "The File Contents:"}
> {print $0} END {print "END of File"}' file
The File Contents:
frist line of this text.
second line of this text.
third line of this text.
END of File

(9)cp 命令

# -i 若目标文件已存在,询问是否覆盖
# -p 复制后保留源文件的属性,包括所有者、所有组、权限和时间
# -r 递归复制,用于复制目录
# -l 把目标文件建立为源文件的硬链接文件
# -s 把目标文件建立为源文件的软链接文件
# -u 若目标文件与源文件有差异,则更新目标文件,此选项可用于文件的升级和备用
# -a 相当于-d、-p、-r选项的集合

[root@6c810f7f0bd8 ~]# cp -i file file1
cp: overwrite 'file1'? n

#复制目录
[root@6c810f7f0bd8 ~]# mkdir files
[root@6c810f7f0bd8 ~]# cp -r files files1

(10)rm 命令

#删除文件
[root@6c810f7f0bd8 ~]# rm file2
rm: remove regular file 'file2'? y

#删除目录
[root@6c810f7f0bd8 ~]# rm -r files
rm: remove directory 'files'? y

# -f 强制删除,系统不会询问
#强制删除目录
[root@6c810f7f0bd8 ~]# rm -rf files1

(11)mv 命令

# -f 若目标文件存在,则强制覆盖,不会询问
# -i 若目标文件存在,则询问
# -n 若目标文件存在,则不覆盖移动
# -v 显示目录或文件的移动过程
# -u 若目标文件存在,但源文件更新,则对目标文件升级

#文件移动
[root@6c810f7f0bd8 ~]# mv file /tmp
[root@6c810f7f0bd8 ~]# ls
anaconda-ks.cfg  anaconda-post.log  file1  file3  original-ks.cfg
[root@6c810f7f0bd8 ~]# ls /tmp
file  ks-script-esd4my7v  ks-script-eusq_sc5

#若源文件和目标文件是同一目录,则相当于是改名
[root@6c810f7f0bd8 ~]# mv file1 file2
[root@6c810f7f0bd8 ~]# ls
anaconda-ks.cfg  anaconda-post.log  file2  file3  original-ks.cfg

(12)sort 命令

sort将文件的每一行作为一个单位,比较原则为:从首字母向后依次按ASCII码值比较,最后升序输出。

# -f 忽略大小写
# -b 忽略每行前面的空白
# -n 以数值型进行排序
# -r 反向排序
# -u 删除重复行,就是uniq命令
# -t 指定分隔符,默认为制表符
# -k [n,m] 按照指定的字段范围排序

[root@6c810f7f0bd8 ~]# echo -e 'b c b d\na c b c\nc b d a' > file4
[root@6c810f7f0bd8 ~]# sort file4
a c b c
b c b d
c b d a

#按照第四个字段排序
[root@6c810f7f0bd8 ~]# sort -k4,4 file4
c b d a
a c b c
b c b d

目录操作命令

(1)mkdir 命令

# -m 手动配置所创建目录的权限
# -p 递归创建目录

[root@6c810f7f0bd8 ~]# mkdir files/files_admin/file1
mkdir: cannot create directory ‘files/files_admin/file1’: No such file or directory
[root@6c810f7f0bd8 ~]# mkdir -p files/files_admin/file1
[root@6c810f7f0bd8 ~]# ls -R files
files:
files_admin

files/files_admin:
file1

files/files_admin/file1:

(2)rmdir命令

只能删除空目录

[root@6c810f7f0bd8 ~]# rmdir files
rmdir: failed to remove 'files': Directory not empty

(3)cd 命令

​
#目录切换:

#当前工作目录用“.”表示;父目录用“..”表示;当前登录用户的主目录用“~”表示,上次所在目录用“-”表示

#当前路径为/usr/bin,通过绝对路径切换到/usr/local/share

[root@6c810f7f0bd8 bin]# cd /usr/local/share

#通过相对路径切换到/usr/local/share

[root@6c810f7f0bd8 bin]# cd ../local/share


​#切换到主目录
[root@6c810f7f0bd8 bin]# cd
[root@6c810f7f0bd8 ~]#

(4) pwd命令

#查看当前工作目录的绝对路径

[root@6c810f7f0bd8 ~]# pwd
/root

(5)ls 命令

# -a 显示全部文件,包含隐藏文件
[root@6c810f7f0bd8 ~]# ls -a
.   anaconda-ks.cfg    .bash_history  .bash_profile  .cshrc  file3  files            .tcshrc
..  anaconda-post.log  .bash_logout   .bashrc        file2   file4  original-ks.cfg  .viminfo

# -A 显示全部文件,包含隐藏文件,但不包括“.”和“..”这两个目录
[root@6c810f7f0bd8 ~]# ls -A
anaconda-ks.cfg    .bash_history  .bash_profile  .cshrc  file3  files            .tcshrc
anaconda-post.log  .bash_logout   .bashrc        file2   file4  original-ks.cfg  .viminfo

# -d 仅列出目录本身,而不列出目录内的文件数据
[root@6c810f7f0bd8 ~]# ls -d
.

# -f 不排序直接列出结果
[root@6c810f7f0bd8 ~]# ls -f
.bash_profile      original-ks.cfg  anaconda-ks.cfg  .bashrc  ..  file3  .bash_history  file4
anaconda-post.log  .cshrc           .bash_logout     .tcshrc  .   file2  .viminfo       files
# -r 将排序结果反向输出
# -R 连同子目录内容一起列出来
# -S 按文件容量大小排序
# -t 以时间排序
# -F 在文件或目录后加上文件类型的指示符号,"*"代表可执行文件,"/"代表目录,"="代表socket文件,"|"代表FIFO文件
[root@6c810f7f0bd8 ~]# ls -F
anaconda-ks.cfg  anaconda-post.log  file2  file3  file4  files/  original-ks.cfg

# -h 以易读的方式显示文件或目录大小
# -i 显示inode信息
# -l 使用长格式列出文件和目录信息
[root@6c810f7f0bd8 ~]# ls -l
total 28
-rw------- 1 root root 2328 Dec  4  2020 anaconda-ks.cfg
-rw-r--r-- 1 root root  435 Dec  4  2020 anaconda-post.log
-rw-r--r-- 1 root root   33 Sep 12 09:33 file2
-rw-r--r-- 2 root root  719 Sep 10 18:00 file3
-rw-r--r-- 1 root root   24 Sep 12 12:34 file4
drwxr-xr-x 3 root root 4096 Sep 12 12:40 files
-rw------- 1 root root 2026 Dec  4  2020 original-ks.cfg

# -n 以GID和UID分别代替群组名和文件用户名显示出来
[root@6c810f7f0bd8 ~]# ls -n
total 28
-rw------- 1 0 0 2328 Dec  4  2020 anaconda-ks.cfg
-rw-r--r-- 1 0 0  435 Dec  4  2020 anaconda-post.log
-rw-r--r-- 1 0 0   33 Sep 12 09:33 file2
-rw-r--r-- 2 0 0  719 Sep 10 18:00 file3
-rw-r--r-- 1 0 0   24 Sep 12 12:34 file4
drwxr-xr-x 3 0 0 4096 Sep 12 12:40 files
-rw------- 1 0 0 2026 Dec  4  2020 original-ks.cfg

# --full-time 以完整的时间模式输出
[root@6c810f7f0bd8 ~]# ls --full-time
total 28
-rw------- 1 root root 2328 2020-12-04 17:37:38.000000000 +0000 anaconda-ks.cfg
-rw-r--r-- 1 root root  435 2020-12-04 17:37:38.000000000 +0000 anaconda-post.log
-rw-r--r-- 1 root root   33 2021-09-12 09:33:12.411634000 +0000 file2
-rw-r--r-- 2 root root  719 2021-09-10 18:00:00.000000000 +0000 file3
-rw-r--r-- 1 root root   24 2021-09-12 12:34:08.441634000 +0000 file4
drwxr-xr-x 3 root root 4096 2021-09-12 12:40:41.171634000 +0000 files
-rw------- 1 root root 2026 2020-12-04 17:37:38.000000000 +0000 original-ks.cfg

(6)ln 命令

硬链接的特点:

(1)对于源文件和硬链接文件,不论修改哪个文件的数据,另一个文件的数据都会发生改变;

(2)不论是删除源文件或硬链接文件,只要存在一个这个文件均可以被访问;

(3)硬链接不会建立新的inode信息,也不会更改inode总数;

(4)硬链接不能跨文件系统(分区)建立,因为在不同的文件系统中,inode号是重新计算的;

(5)硬链接不能链接目录 

软链接的特点:

(1)修改源文件会改变软链接文件;

(2)删除软链接文件,源文件不受影响;而删除源文件,软链接文件将找不到实际的数据,从而显示文件不存在;

(3)软链接会创建自己的inode信息和block,但block中存储的是源文件的文件名及inode号;

(4)软链接可以链接目录,可以跨分区;

# -s 创建文件软链接,不加-s则创建文件硬链接

#硬链接 修改一个文件
[root@6c810f7f0bd8 ~]# echo '123' >> file1
[root@6c810f7f0bd8 ~]# ln file1 test
[root@6c810f7f0bd8 ~]# echo '456' >> test
[root@6c810f7f0bd8 ~]# cat test
123
456
[root@6c810f7f0bd8 ~]# cat file1
123
456

#软链接 删除源文件
[root@6c810f7f0bd8 ~]# ln -s file2 test2
[root@6c810f7f0bd8 ~]# rm -f file2
[root@6c810f7f0bd8 ~]# cat test2
cat: test2: No such file or directory

访问权限管理

文件类型和权限由10个字符组成,第一个代表类型:

字符含义
-普通文件
d目录
l链接文件
b块设备文件
c字符设备文件

 

权限:r表示读,w表示写,x表示可

后面9个字符可分为3个1组,第1组表示文件所有者的权限,第2组表示文件所属组的权限,第3组表示其他人的权限。

rwx 权限对文件和目录的作用

对于文件:

r表示可以读取文化的内容,如执行cat、more、less、head、tail等文件查看命令

w表示可以增加、修改、编辑文件内容,但是不能删除,除非拥有上级目录的写权限

x表示该文件具有被系统执行的权限

对于目录:

r表示可以读取目录结构列表,即能够查看目录中有哪些文件和子目录

w是目录的最高权限,能够对该目录中的文件进行创建、删除、移动、更改名称的操作

x表示能够进入该目录

对目录来说,常用来设定目录的权限只有0(---)、5(r-w)、7(rwx)三种

修改权限位的命令

(1)chmod 命令

权限与数字的对应关系:r -> 4, w -> 2, x -> 1

# -R 递归修改子目录中文件的所有权限

# u 文件所有者
# g 文件所属组
# o 其他用户
# a 相当于u g o 
# + 增加权限
# - 删除权限
# = 设定权限

# 将file的权限设置为rwxr-xr-x(755)
[root@6c810f7f0bd8 ~]# chmod u=rwx,go=rx file(chmod 755 file)
[root@6c810f7f0bd8 ~]# ls -l file
-rwxr-xr-x 1 root root 76 Sep 13 06:04 file

# 增加file文件每种用户的w权限
[root@6c810f7f0bd8 ~]# ls -l file
-rwxrwxrwx 1 root root 76 Sep 13 06:04 file

(2)umask 命令

Linux系统通过使用umask默认权限来给所有新建的文件和目录赋予初始权限。


[root@6c810f7f0bd8 ~]# umask
0022

umask的值由4个八进制数组成,第一个表示文件所具有的特殊权限(SetUID、SetGID、Sticky BIT)。

文件(或目录)的真正初始权限 = 文件(或目录)的最大默认权限 - umask 权限

这里的减法其实是“遮盖”的意思,即最大默认权限中和umask权限的公共部分通过减法运算会被遮盖掉,剩下的“最大默认权限”,才是最终赋予文件或目录的初始权限。

在Linux系统中,文件可拥有的默认最大权限是666(rw-rw-rw-),目录可拥有的最大默认权限是777(rwxrwxrwx)

#新建文件的初始权限:(666)-(022)=(644)-rw-r--r--
[root@6c810f7f0bd8 ~]# touch newfile
[root@6c810f7f0bd8 ~]# ls -l newfile
-rw-r--r-- 1 root root 0 Sep 13 09:29 newfile

#新建目录的初始权限:(777) - (022)= (755)drwxr-xr-x
[root@6c810f7f0bd8 ~]# ls -ld newfiles
drwxr-xr-x 2 root root 4096 Sep 13 09:31 newfiles


#修改umask的值(仅临时有效,若要永久生效需要修改环境变量配置文件/etc/profile)
[root@6c810f7f0bd8 ~]# umask 044
[root@6c810f7f0bd8 ~]# umask
0044

(3)chown 命令

# 修改文件所有者为max
[root@6c810f7f0bd8 ~]# ls -l file
-rwxr--r-- 1 root root 76 Sep 13 06:04 file
[root@6c810f7f0bd8 ~]# chown max file
[root@6c810f7f0bd8 ~]# ls -l file
-rwxr--r-- 1 max root 76 Sep 13 06:04 file

# 普通用户仅能修改自己文件的权限
[max@6c810f7f0bd8 root]$ chmod 775 newfile
chmod: cannot access 'newfile': Permission denied

(4)chgrp 命令

# -R 递归修改目录中的所有文件及子目录
# 修改文件的所属组为group1
[root@6c810f7f0bd8 ~]# groupadd group1
[root@6c810f7f0bd8 ~]# chgrp group1 file
[root@6c810f7f0bd8 ~]# ls -l file
-rwxr--r-- 1 max group1 76 Sep 13 06:04 file

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值