Linux文件管理
Linux文件管理
文件目录的层级结构
为了方便管理文件和目录,Linux 系统将它们组织成一个以根目录 / 开始的倒置的树状结构。Linux 中的目录,和 Windows 系统中的文件夹类似,不同之处在于,Linux 系统中的目录也被当做文件看待。
在 Linux 操作系统中,所有的文件和目录都被组织成以一个根节点“/”开始的倒置的树状结构,如图所示:
其中,目录就相当于 Windows 中的文件夹,目录中存放的既可以是文件,也可以是其他的子目录,而文件中存储的是真正的信息
为了方便管理和维护,Linux 系统采用了文件系统层次标准,也称为 FHS 标准,它规定了根目录下各个目录应该存在哪些类型的文件(或子目录)
[root@linux30 ~]# cd /
[root@linux30 /]# ls -l
总用量 24
lrwxrwxrwx. 1 root root 7 9月 6 15:40 bin -> usr/bin ## 常见的用户命令
dr-xr-xr-x. 5 root root 4096 11月 9 13:33 boot ## 内核和启动文件
drwxr-xr-x. 20 root root 3240 12月 20 16:15 dev ## 设备文件
drwxr-xr-x. 88 root root 8192 12月 20 16:15 etc ## 系统和服务的配置文件
drwxr-xr-x. 7 root root 77 12月 20 09:43 home ## 系统默认的普通用户的家目录 ~ / root
lrwxrwxrwx. 1 root root 7 9月 6 15:40 lib -> usr/lib ## 系统函数库目录
drwxr-xr-x. 2 root root 6 4月 11 2018 media ## 系统用来挂载光驱等临时文件系统的挂载点
drwxr-xr-x. 3 root root 18 11月 9 09:29 mnt ## 系统加载文件系统时常用的挂载点
drwxr-xr-x. 11 root root 162 11月 15 10:19 opt ## 第三方软件安装目录
dr-xr-xr-x. 138 root root 0 12月 20 16:14 proc ## 虚拟文件系统
dr-xr-x---. 13 root root 4096 12月 16 22:57 root ## root用户的家目录
lrwxrwxrwx. 1 root root 8 9月 6 15:40 sbin -> usr/sbin ## 存放系统管理命令
drwxrwxrwt. 16 root root 4096 12月 20 16:15 tmp ## 临时文件的存放目录
drwxr-xr-x. 13 root root 155 9月 6 15:40 usr ## 存放和用户直接相关的文件和目录
[root@linux30 /]#
绝对路径及相对路径
1)绝对路径
Linux系统采用了目录树的文件组织结构,在Linux下每个目录或文件都可以从根目录处开始寻找,比如:/usr/local/src目录。这种从根目录开始的全路径被称为“绝对路径”,绝对路径一定是以“/”开头的。
2)当前目录:pwd
[root@linux30 ~]# pwd
/root
[root@linux30 ~]#
3)特殊目录:(.)和(..)
在每个目录下,都会固定存在两个特殊目录,分别是一个点(.)和两个点(..)的目录。一个点(.)代表的是当前目录,两个点(..)代表的是当前目录的上层目录。在Linux下,所有以点开始的文件都是“隐藏文件”,对于这类文件,只使用命令ls -l是看不到的,必须要使用ls -la才可以看到
[root@linux30 ~]# ls -l
总用量 4
-rw-------. 1 root root 1440 9月 6 15:43 anaconda-ks.cfg
drwxr-xr-x. 5 root root 53 10月 21 17:49 logs
drwxr-xr-x. 4 root root 34 10月 25 15:47 nacos
[root@linux30 ~]# ls -la
总用量 76
dr-xr-x---. 13 root root 4096 12月 16 22:57 .
dr-xr-xr-x. 18 root root 236 9月 29 15:34 ..
-rw-------. 1 root root 1440 9月 6 15:43 anaconda-ks.cfg
drwxr-xr-x. 3 root root 32 10月 21 17:49 .arthas
-rw-------. 1 root root 17993 12月 20 16:14 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 219 12月 10 17:59 .bashrc
drwxr-xr-x. 4 root root 33 11月 15 13:58 .cache
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 25 11月 29 16:54 .docker
drwxr-xr-x. 3 root root 19 11月 15 15:15 .gem
-rw-r--r--. 1 root root 32 11月 11 14:44 .gitconfig
drwxr-xr-x. 3 root root 40 11月 8 18:53 .ivy2
drwxr-xr-x. 3 root root 19 10月 12 10:11 .java
drwxr-xr-x. 11 root root 4096 10月 12 10:12 .jenkins
-rw-------. 1 root root 116 12月 17 12:01 .lesshst
drwxr-xr-x. 5 root root 53 10月 21 17:49 logs
-rw-r--r--. 1 root root 36 11月 15 15:55 .logstash_jdbc_last_run
drwxr-xr-x. 4 root root 34 10月 25 15:47 nacos
-rw-------. 1 root root 0 11月 9 09:32 .node_repl_history
drwxr-----. 3 root root 19 9月 28 10:21 .pki
-rw-------. 1 root root 1024 10月 16 16:01 .rnd
drwx------. 2 root root 25 10月 25 22:49 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
-rw-------. 1 root root 4506 12月 16 22:57 .viminfo
[root@linux30 ~]#
4)相对路径
顾名思义,“相对路径”的关键在于当前在什么路径下。假设当前目录在/usr/local下,那么它的上层目录(/usr目录)可以用../表示,而/usr/local的下层目录(src)则可以用./src表示。前面讲到的(.)和(..)目录实际上也是属于相对路径
[root@linux30 ~]# cd /mnt/ ##现在进入/mnt目录
[root@linux30 mnt]# ls -la
总用量 0
drwxr-xr-x. 3 root root 18 11月 9 09:29 . ## 代表当前目录
dr-xr-xr-x. 18 root root 236 9月 29 15:34 .. ## 代表上层目录
drwxr-xr-x. 2 root root 6 11月 9 09:29 hgfs
[root@linux30 mnt]# cd . ## 进入当前目录
[root@linux30 mnt]# pwd ## 显示当前目录
/mnt
[root@linux30 mnt]# cd .. ## 进入当前目录的上层目录
[root@linux30 /]# pwd
/
[root@linux30 /]# ## 进入了上层目录,也就是/目录中
文件的相关操作
下面将介绍如何创建、删除、移动、重命名、查看文件,以及不同系统之间进行格式转换。
1)创建文件:touch
如果在使用touch命令创建文件的时候,当前目录中已经存在了这个文件,那么这个命令不会对当前的同名文件造成影响,因为它并不会修改文件的内容,虽然实际上touch确实对该文件做了“修改”—它会更新文件的创建时间属性。
2)删除文件:rm
[root@linux30 tmp]# rm test.txt
rm:是否删除普通空文件 "test.txt"?y
[root@linux30 tmp]#
3)移动或重命名文件:mv
[root@linux30 tmp]# mkdir src dest ## 创建src和dest目录
[root@linux30 tmp]# ll
总用量 4
drwxr-xr-x. 2 root root 6 12月 20 16:46 dest
drwxr-xr-x. 2 root root 6 12月 20 16:46 src
[root@linux30 tmp]# cd src/ ## 进入src目录
[root@linux30 src]# ll
总用量 0
[root@linux30 src]# touch test.txt ## 创建test.txt文件
[root@linux30 src]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 20 16:46 test.txt
[root@linux30 src]# mv test.txt ../dest/ ## 移动test.txt文件到dest目录
[root@linux30 src]# ll
总用量 0
[root@linux30 src]# cd ../dest/ ## 进入dest目录
[root@linux30 dest]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 20 16:46 test.txt
[root@linux30 dest]# mv test.txt test2.txt ## 重命名test.txt -> test2.txt
[root@linux30 dest]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 20 16:46 test2.txt
[root@linux30 dest]#
4)查看文件:cat
使用 -n 参数可以显示每行的行号
5)查看文件头:head
有时候文件非常大,使用cat命令显示出来的内容太多,而我们可能并不想查看所有内容,只是想看看文件开始部分的内容,这时候就可以使用head命令了,后面跟上需要查看的文件名就可以了。默认情况下,head将显示该文件前10行的内容。
6)查看文件尾:tail
当文件很大时,可以使用该命令查看文件尾部的内容,默认情况下tail也是只显示文件的最后10行内容,同样可以使用-n参数指定显示的行数。
tail还有个更实用的功能,就是可以动态地查看文件尾。这对查看一些不断改变的文件来说非常有用。
目录相关操作
进入目录:cd
[root@linux30 fileSys]# cd ## 不带参数,进入到root目录
[root@linux30 ~]# pwd
/root
[root@linux30 ~]# cd linux-course/fileSys/ ## 进入/root/linux-course/fileSys目录
[root@linux30 fileSys]# pwd
/root/linux-course/fileSys
[root@linux30 fileSys]#
创建目录:mkdir
[root@linux30 fileSys]# mkdir dir1 ## 创建dir1目录
[root@linux30 fileSys]# cd dir1 ## 进入dir1目录
[root@linux30 dir1]# mkdir dir2 ## 创建dir2目录
[root@linux30 dir1]# cd dir2/ ## 进入dir2目录
[root@linux30 dir2]# pwd
/root/linux-course/fileSys/dir1/dir2
[root@linux30 dir2]# mkdir dir3/dir4
mkdir: 无法创建目录"dir3/dir4": 没有那个文件或目录
[root@linux30 dir2]# mkdir -p dir3/dir4 ## 创建dir3目录并在dir3目录下创建dir4目录
[root@linux30 dir2]# cd dir3/dir4/
[root@linux30 dir4]# pwd
/root/linux-course/fileSys/dir1/dir2/dir3/dir4
[root@linux30 dir4]#
删除目录:rmdir和rm
大家要注意,该命令只能删除空目录,如果目录不为空(存在文件或者子目录),那么该命令将拒绝删除指定的目录。
[root@linux30 dir2]# rmdir dir3
rmdir: 删除 "dir3" 失败: 目录非空
[root@linux30 dir2]#
[root@linux30 dir3]# ll dir4/
总用量 0
[root@linux30 dir3]# rmdir dir4/
[root@linux30 dir3]# ll
总用量 0
[root@linux30 dir3]#
[root@linux30 fileSys]# rm dir1/
rm: 无法删除"dir1/": 是一个目录
[root@linux30 fileSys]# rm -r dir1/
rm:是否进入目录"dir1/"? y
rm:是否进入目录"dir1/dir2"? y
rm:是否删除目录 "dir1/dir2/dir3"?y
rm:是否删除目录 "dir1/dir2"?y
rm:是否删除目录 "dir1/"?y
[root@linux30 fileSys]#
文件和目录复制:cp
[root@linux30 fileSys]# mkdir -p dir1/dir2/dir3/dir4
[root@linux30 fileSys]# cp anaconda-ks.cfg dir1/dir2/dir3/dir4 ## 文件的复制
[root@linux30 fileSys]# ll dir1/dir2/dir3/dir4
总用量 4
-rw-------. 1 root root 1440 12月 20 17:29 anaconda-ks.cfg
[root@linux30 fileSys]#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?