Linux文件管理
1 .文件管理命令
1.1 创建
1.1.1 文件
touch:创建文件;修改文件时间戳
## 创建空文件
touch file1 file2
## 文件时间同步到当前时间
touch file
## 文件时间修改到指定时间
touch -t YYYYMMDDHH file
## 创建含有 空格 的文件
[root@localhost Desktop]# touch "li i"
[root@localhost Desktop]# ls
'li i'
1.1.2 目录
mkdir:创建目录
## 创建空目录
mkdir dir1
## 递归创建目录
mkdir -p test/test/test
1.2 删除
rm命令
## 删除文件
rm file1 file2
## 强制删除
rm -f
## 递归删除 --删除目录
rm -r dir1
1.3 文件编辑
1.3.1 gedit命令
当系统图形开启时可以使用gedit来对文件进行编辑,gedit依赖于图形,只有开启图形时才能使用gedit命令
gedit file
1.3.2 vim命令
-
vim的模式
- 浏览模式(命令模式):可以对文件内容进行浏览,也可以对vim的工作方式进行设定
- 插入模式:vim的编辑模式,只有在插入模式下才能对文件内容进行修改
- 退出模式::当文件内容修修改完毕可以通过退出模式来保存或不保存修改的内容
-
vim的基本使用方法
## 进入到vim的浏览模式,不能编辑文件 vim file ## 在浏览模式中按<i>进入插入模式 # 在vim中鼠标不能控制光标用上下左右移动光标 # 按<ESC>退出插入模式 # 按:wq保存更改 # :q 当vim打开文件后未作任何操作可以直接退出 # :命令! 表示强制执行命令
-
处理vim的异常退出
当vim处理文件时异常退出时,再使用vim会进入异常界面E325: ATTENTION Found a swap file by the name ".file1.swp" owned by: root dated: Wed Mar 19 19:05:09 2025 file name: ~root/Desktop/file1 modified: YES user name: root host name: localhost.localdomain process ID: 35926 While opening file "file1" dated: Wed Mar 19 19:03:23 2025 (1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution. (2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r file1" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".file1.swp" to avoid this message. Swap file ".file1.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
其中:
[O]:表示只读打开文件
[E]:表示继续编辑
[R]:表示恢复内容继续编辑
[D]:表示删除.swp文件继续编辑
[Q]:表示退出当前文件编辑
[A]:表示退出vim -
vim打开多个文件
## 同时打开两个文件 [root@localhost Desktop]# vim -p file1 file2 ## 在浏览模式 :tabp 切换下一个文件,:tabn 切换上一个文件 ## 上下打开两个文件 [root@localhost Desktop]# vim -o file1 file2 ## 在浏览模式 按 ctrl + w 后放开,再 ↑ / ↓ 切换文件
1.3.3 查看文件
- cat命令
直接输出文件全部内容[root@localhost Desktop]# cat history_file1 ## cat参数 -b 显示非空行行号 -n 显示包括空行行号
- less命令
浏览模式看文件 - head命令
输出文件前几 (默认10) 行[root@localhost Desktop]# head history_file1
- tail命令
输出后几 (默认10) 行[root@localhost Desktop]# tail history_file1
- which命令
查看命令存放的绝对路径which [命令]
1.4 文件复制与移动
1.4.1 cp命令
cp命令是按一个文件为模版,新建另外一个文件的过程
## 复制 file1 到 dir1
[root@localhost Desktop]# cp file1 dir1
## 以 dir1 复制到 dir2
[root@localhost Desktop]# cp -r dir1 dir2
## cp 复制多个文件时,复制的终点必须是目录
## cp 复制的终点可以不存在,会自动创建一个新的文件/目录
1.4.2 mv命令
mv命令在 相同磁盘分区下,是将一个 文件/目录 移动修改名称 的过程
[root@localhost Desktop]# ls -i file1
101062757 file1
[root@localhost Desktop]# mv file1 file3
[root@localhost Desktop]# ls -i file3
101062757 file3
mv命令在 不同磁盘分区下,是将一个 文件/目录 复制删除移动修改名称 的过程
[root@localhost Desktop]# ls -i file1
101062757 file1
[root@localhost Desktop]# mv file1 /boot/
[root@localhost Desktop]# ls -i /boot/file1
144 /boot/file1
1.5 文件统计命令
1.5.1 file命令
file命令 用来查看文件类型
[root@localhost Desktop]# file file.exe
file.exe: empty
[root@localhost Desktop]# file file.mp3
file.mp3: empty
[root@localhost Desktop]# file file.awi
file.awi: empty
## 不能根据文件后缀判断文件类型
1.5.2 wc命令
wc命令 用来查看文件大小
[root@localhost Desktop]# vim file1
1
22
333
4444
55555
666666
7777777
88888888
999999999
树
[root@localhost Desktop]# wc file1
10 10 58 file1
[root@localhost Desktop]# wc -l file1
10 file1
[root@localhost Desktop]# wc -w file1
10 file1
[root@localhost Desktop]# wc -c file1
58 file1
[root@localhost Desktop]# wc -m file1
56 file1
## (10):表示文件的行数
## (10):表示文件的单词数
## (58):表示文件的字节数
## (56):表示文件的字符数
# -l 查看行数
# -w 查看单词数
# -c 查看字节数
# -m 查看字符数
1.5.3 ls命令
ls命令 用于列出 文件/目录 信息
[root@localhost Desktop]# ls /root
anaconda-ks.cfg Documents Music Public Videos
Desktop Downloads Pictures Templates
参数 | 作用 |
---|---|
-i | 显示文件id |
-d | 显示名称 |
-l | 显示属性 |
-a | 显示全部文件 |
-s | 显示文件大小 |
-R | 递归显示目录的全部内容 |
2. 文件寻址
2.1 Linux的层级结构
2.2 系统二级目录功能
2.3 系统的寻址方式
2.3.1 相对路径
- 省略的当前所在的系统位置
- 只能直接操作当前目录下的文件及子目录
- 不以 / 开头
2.3.2 绝对路径
- 完整的目录
- 一定以 / 开头
- 任何场景都能用
2.4 寻址相关命令
2.4.1 pwd命令
## 显示当前工作绝对路径
[root@localhost Desktop]# pwd
/root/Desktop
2.4.2 cd命令
移动工作目录
## 移动到当前用户的家目录
cd = cd ~
## 移动到指定用户家目录
cd ~<username>
## 移动到之前所在目录
cd -
cd ~- = cd -
[root@localhost Desktop]# cd -
/root
[root@localhost ~]# cd -
/root/Desktop
## 移动到所在目录的上一级目录
[root@localhost Desktop]# cd ..
[root@localhost ~]# cd ..
[root@localhost /]#
# . 表示当前工作目录
[root@localhost ~]# cd ./Desktop/
[root@localhost Desktop]#
2.5 文件的批处理
2.5.1 通配符
使用符号匹配一类字符
通配符 | 功能 |
---|---|
* | 匹配 0~任意 字符 |
? | 匹配单个字符 |
[[:alpha:]] | 匹配单个字母 |
[[:lower:]] | 匹配单个小写字母 |
[[:upper:]] | 匹配单个大写字母 |
[[:digit:]] | 匹配单个数字 |
[[:alnum:]] | 匹配单个数字或字母 |
[[:punct:]] | 匹配单个符号 |
[[:space:]] | 匹配单个空格 |
[root@localhost Desktop]# touch li1i li2i li3i
[root@localhost Desktop]# ls
li1i li2i li3i
[root@localhost Desktop]# rm -f *
[root@localhost Desktop]# ls
[root@localhost Desktop]#
[root@localhost Desktop]# ls
li li2 li3
[root@localhost Desktop]# rm -f li*
[root@localhost Desktop]# ls
[root@localhost Desktop]#
2.5.3 字符集合表示方式
方式 | 功能 |
---|---|
[ - ] | 或关系 |
模糊匹配 | |
{ … } | 精准匹配 |
![ ] | |
1 | 匹配 [ ] 之外的全部条件 |
[ ] 一般用于已有文件的处理
{ } 一般用于创建文件
[root@localhost Desktop]# touch li[1-3]
[root@localhost Desktop]# ls
'li[1-3]'
[root@localhost Desktop]# touch li{1..3}
[root@localhost Desktop]# ls
li1 li2 li3