目录
注意:rm -rf 递归删除目录及其内容且不提醒,慎用!!!!
一、Linux系统目录结构
1、查看目录结构命令 #tree
[root@xaiofei ~]# tree /home
/home
├── liang
└── xiaofei
2 directories, 0 files
说明:此命令需自行下载
2、目录结构操作使用方法
在对目录结构进行操作管理时,可有三种方法对目录进行操作管理:
采用绝对路径方式进行操作管理
采用相对路径方式进行操作管理
采用设备挂载方式进行操作管理
2.1、绝对路径
表示从根路径开始,进入指定的路径中或子目录中,获取指定的数据资源信息
#采用绝对路径方式进入到liang目录中
[root@xaiofei ~]# cd /home/liang
[root@xaiofei liang]#
2.2、相对路径
#采用相对路径方式进入到liang目录中
[root@xaiofei ~]# cd /home
[root@xaiofei home]# cd liang
[root@xaiofei liang]#
说明:采用相对路径的方式切换目录可以提升操作效率。
二、文件与目录操作命令
1、打印工作目录命令 #pwd
[root@xaiofei liang]# pwd
/home/liang/liang/liang
[root@xaiofei liang]#
2、目录切换命令 #cd
cd 命令可以说是 Linux 中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的。 语法:
cd [目录名]
cd . 返回当前登录用户的家目录
cd .. 返回上级目录
cd ~ 返回当前登录用户的家目录
cd - 返回上次跳转到当前目录的目录
3、目录查看命令 #ls
ls 命令是 linux 下最常用的命令。ls 命令就是 list 的缩写 缺省下 ls 用来打印出当前目录的清单,如果 ls 指定其他目 录,那么就会显示指定目录里的文件及文件夹清单。 通过 ls 命令不仅可以查看 linux 文件夹包含的文件 而且可以查看 文件权限(包括目录、文件夹、文件权限) 查看目录信息等等。
[root@xaiofei ~]# ls anaconda-ks.cfg 公共 视频 文档 音乐 initial-setup-ks.cfg 模板 图片 下载 桌面
-a 显示所有文件(包括以.开头的隐藏的文件) [root@xaiofei ~]# ls -a . .bash_profile .dbus .local 视频 桌面 .. .bashrc .esd_auth .tcshrc 图片 anaconda-ks.cfg .cache .ICEauthority .viminfo 文档 .bash_history .config initial-setup-ks.cfg 公共 下载 .bash_logout .cshrc .lesshst 模板 音乐
-l 使用详细列表格式显示文件或目录的详细信息 [root@xaiofei ~]# ls -l 总用量 8 -rw-------. 1 root root 1700 5月 9 00:44 anaconda-ks.cfg -rw-r--r--. 1 root root 1748 5月 9 00:48 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 5月 9 00:48 公共 drwxr-xr-x. 2 root root 6 5月 9 00:48 模板 drwxr-xr-x. 2 root root 6 5月 9 00:48 视频 drwxr-xr-x. 2 root root 6 5月 9 00:48 图片 drwxr-xr-x. 2 root root 6 5月 9 00:48 文档 drwxr-xr-x. 2 root root 6 5月 9 00:48 下载 drwxr-xr-x. 2 root root 6 5月 9 00:48 音乐 drwxr-xr-x. 2 root root 6 5月 9 00:48 桌面
-h 使用人类可读的格式显示文件大小(例如 1K、2M) [root@xaiofei ~]# ls -lh 总用量 8.0K -rw-------. 1 root root 1.7K 5月 9 00:44 anaconda-ks.cfg -rw-r--r--. 1 root root 1.8K 5月 9 00:48 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 5月 9 00:48 公共 drwxr-xr-x. 2 root root 6 5月 9 00:48 模板 drwxr-xr-x. 2 root root 6 5月 9 00:48 视频 drwxr-xr-x. 2 root root 6 5月 9 00:48 图片 drwxr-xr-x. 2 root root 6 5月 9 00:48 文档 drwxr-xr-x. 2 root root 6 5月 9 00:48 下载 drwxr-xr-x. 2 root root 6 5月 9 00:48 音乐 drwxr-xr-x. 2 root root 6 5月 9 00:48 桌面
-r 以相反的顺序列出文件 [root@xaiofei ~]# ls -r 桌面 下载 图片 模板 initial-setup-ks.cfg 音乐 文档 视频 公共 anaconda-ks.cfg
-t按照修改时间(最新的文件在前)排序
-S按照文件大小排序
-d, --directory显示目录本身,而非目录下的文件列表
-i, --inode显示每个文件的 inode 号
--color=auto自动根据文件类型显示不同的颜色
-R, --recursive递归列出目录及其子目录中的文件
4、文件查看命令
4.1、cat命令
cat 命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者 从标准输入读取内容并显示,它常与重定向符号配合使用
功能:
一次显示整个文件:cat filename
从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件.
将几个文件合并为一个文件:cat file1 file2 > file3
追加内容到指定文件
[root@xaiofei liang]# cat 1.txt
123
[root@xaiofei liang]# cat <<e >> 1.txt
> 1
> 2
> 3
> e
> [root@xaiofei liang]# cat 1.txt
> 123
> 1
> 2
> 3
# >> 追加内容到指定文件 [root@xaiofei liang]# cat 2.txt 234234 [root@xaiofei liang]# cat 1.txt >> 2.txt [root@xaiofei liang]# cat 2.txt 234234 123 1 2 3
# 将几个文件合并为一个文件 [root@xaiofei liang]# cat 1.txt 123 1 2 3 [root@xaiofei liang]# cat 2.txt 234234 123 1 2 3 [root@xaiofei liang]# cat 1.txt 2.txt > 3.txt [root@xaiofei liang]# cat 3.txt 123 1 2 3 234234 123 1 2 3
# 将文件1.txt的内容覆盖到文件2.txt [root@xaiofei liang]# cat 1.txt 123 1 2 3 [root@xaiofei liang]# cat 2.txt 234234 123 1 2 3 [root@xaiofei liang]# cat 1.txt > 2.txt [root@xaiofei liang]# cat 2.txt 123 1 2 3
4.2、less命令
看文本文件内容的命令,类似于 more 命令,但提供了更多的功能和操作选项。less 命令可以让用户在文件中进行搜 索、前后翻页、跳转到指定行等操作。
less [选项] [文件名]
操作:
操作 | 作用 |
---|---|
空格键 | 向下滚动一屏。 |
page up或b | 向上滚动一屏。 |
Page Down 或 空格键 | 向下滚动一屏。 |
/pattern | 在文件中搜索指定模式(pattern)。 |
n | 找到下一个匹配的搜索结果。 |
N | 找到上一个匹配的搜索结果。 |
g | 跳转到文件的起始处。 |
G | 跳转到文件的末尾。 |
q | 退出 less 命令。 |
4.3、more 命令
用于逐页查看文本文件内容的命令。当需要查看大型文件时,more 命令能够按页显示文件内容,允许用户逐页浏览 文件内容。
more [选项] [文件名]
4.4、head 命令
head 命令用于显示文件的开头部分,默认情况下显示文件的前 10 行内容。head 命令在查看文件的头部信息时非常 有用,尤其适合用于快速查看文件的开头几行内容。
[root@localhost liang]# head 1.txt 1 2 3 4 5 6 7 8 9 10
管道符:
将前一命令的结果作为后一命令的输入进行再次处理;相当于流水线工作。
#前面是序号,后面是内容 [root@localhost liang]# cat -n 1.txt | head -5 1 1 2 2 3 3 4 4 5 5
4.5 tail 命令
tail 命令用于查看文件的尾部内容,默认情况下显示文件的最后 10 行。tail 命令在查看日志文件或需要关注文件末尾 信息时非常有用。。使用 tail -f 选项可以实时监控文件的变化,这对于监控系统日志或等待文件尾部的特定信息非常 方便。
[root@localhost liang]# tail 1.txt 31 32 33 34 35 36 37 38 39 40
同理,tail命令也可结合管道符进行命令的再处理。
定向输出:
#只要第五行的内容 [root@localhost liang]# cat -n 1.txt | head -5 | tail -1 5 5
# -f 循环读取
5、创建目录命令
mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当 前目录中已有的目录。如果需要创建多级目录,可以使用通配符 * 或多个目录名。还支持使用{}来进行创建多个目 录。
# -p 递归创建目录,如果父目录不存在也会创建。 [root@localhost liang]# ls 1.txt liang [root@localhost liang]# mkdir -p xf/fei [root@localhost liang]# ls 1.txt liang xf [root@localhost liang]# tree xf xf └── fei 1 directory, 0 files
6、创建文件命令
6.1 touch 命令
用于创建一个新的空文件或修改现有文件的时间戳。它的全称是 "touch file",即触摸文件。
[root@localhost liang]# touch 2.txt [root@localhost liang]# ls 1.txt 2.txt liang xf
6.2 echo 命令
用于在终端输出指定的文本内容。
[root@localhost liang]# echo 123 123
#定向输出内容到3.txt文件内 [root@localhost liang]# echo 123 >> 3.txt [root@localhost liang]# ls 1.txt 2.txt 3.txt liang xf [root@localhost liang]# cat 3.txt 123
# -n 在输出内容后不换行 [root@localhost liang]# echo -n 123 123[root@localhost liang]#
# -e 启用转义字符的解析。“\"转义符号;\n:换行;\r:回车;\t:水平制表符(tab) [root@localhost liang]# echo -e "1\n2" 1 2 [root@localhost liang]# echo -e "1\t2" 1 2 [root@localhost liang]# echo -e "1\r2" 2
7、文件或目录复制命令
cp命令用来复制文件和目录的命令
[root@localhost liang]# cp 1.txt /home/liang/liang [root@localhost liang]# ls /home/liang/liang 1.txt 2.txt
# -r或R(递归) 复制目录以及目录内所有子目录和文件。 [root@localhost liang]# cp -r liang xf [root@localhost liang]# tree xf xf ├── fei └── liang ├── 1.txt └── 2.txt 2 directories, 2 files
# -i (交互式) 如果在目标位置存在一个文件且该文件会 被覆盖时,cp命令会询问用户是否继续。 [root@localhost liang]# cp -i 1.txt liang cp:是否覆盖"liang/1.txt"?
#-n(不覆盖) 在目标位置存在文件时,不会覆盖该文件,即不会进行复制操作。
[root@localhost liang]# cp -n 1.txt liang
8、文件或目录移动命令
mv命令用来移动文件或目录,或者重命名文件和目录的命令。
#重命名 1.txt 为 4.txt [root@localhost liang]# ls 1.txt 2.txt 3.txt liang xf [root@localhost liang]# mv 1.txt 4.txt [root@localhost liang]# ls 2.txt 3.txt 4.txt liang xf
#移动 4.txt 到目录 liang 中 [root@localhost liang]# mv 4.txt liang [root@localhost liang]# ls 2.txt 3.txt liang xf [root@localhost liang]# ls liang 1.txt 2.txt 4.txt
# -i (交互式) 如果移动或重命名目标位置已存在且会覆盖时,mv 命令会询问用户是否继续。
[root@localhost liang]# mv 1.txt liang
mv:是否覆盖"liang/1.txt"?
9、文件或目录删除命令
rm命令用于删除文件的命令。它的全称是 "remove",意思是 "删除"。
# -i 在删除之前,显示每个文件是否确认删除。 [root@localhost liang]# rm -i 1.txt rm:是否删除普通文件 "1.txt"?
# -f 强制删除,即使文件正在被使用,或者存在打开的文件描述符。 [root@localhost liang]# rm -f 1.txt [root@localhost liang]# ls 2.txt 3.txt liang xf
# -r 递归地删除目录及其内容。 [root@localhost liang]# rm -r liang rm:是否进入目录"liang"? rm:是否删除普通文件 "liang/2.txt"? rm:是否删除普通文件 "liang/1.txt"? rm:是否删除普通文件 "liang/4.txt"? rm:是否删除目录 "liang"?
注意:rm -rf 递归删除目录及其内容且不提醒,慎用!!!!
10、文件属性查看命令
stat 命令是 Unix 和 Linux 系统中的一个实用程序,用于显示关于文件的状态信息。stat 命令提供比 ls -l 更详细的文 件信息,包括文件的大小、创建时间、最后访问时间、最后修改时间等。
[root@localhost liang]# stat 2.txt 文件:"2.txt" 大小:0 块:0 IO 块:4096 普通空文件 设备:fd02h/64770d Inode:72 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root) 环境:unconfined_u:object_r:user_home_t:s0 最近访问:2025-05-12 15:03:25.894226779 +0800 最近更改:2025-05-12 15:03:25.894226779 +0800 最近改动:2025-05-12 15:03:25.894226779 +0800 创建时间:-
11、目录结构查看命令
tree 命令是一个用于以树状结构显示目录及其内容的实用程序,常用于显示文件系统的层次结构。tree 命令会递归地 列出指定目录下的所有子目录和文件,并以树的形式展示它们之间的关系。(注:这是个需手动安装的命令,如果用不了tree 命令,需下载一下才可使用)
[root@localhost liang]# tree /home
/home
└── liang
├── 2.txt
├── 3.txt
├── liang
│ ├── 1.txt
│ ├── 2.txt
│ └── 4.txt
└── xf
├── fei
└── liang
├── 1.txt
└── 2.txt
5 directories, 7 files
# -a 显示所有文件和目录,包括隐藏的。 [root@localhost liang]# tree -a /home /home └── liang ├── 2.txt ├── 3.txt ├── .bash_logout ├── .bash_profile ├── .bashrc ├── liang │ ├── 1.txt │ ├── 2.txt │ └── 4.txt ├── .mozilla │ ├── extensions │ └── plugins └── xf ├── fei └── liang ├── 1.txt └── 2.txt 8 directories, 10 files
# -d 只显示目录,不显示文件。 [root@localhost liang]# tree -d /home /home └── liang ├── liang └── xf ├── fei └── liang 5 directories
# -f 显示所有文件和目录,包括隐藏的。 [root@localhost liang]# tree -f /home /home └── /home/liang ├── /home/liang/2.txt ├── /home/liang/3.txt ├── /home/liang/liang │ ├── /home/liang/liang/1.txt │ ├── /home/liang/liang/2.txt │ └── /home/liang/liang/4.txt └── /home/liang/xf ├── /home/liang/xf/fei └── /home/liang/xf/liang ├── /home/liang/xf/liang/1.txt └── /home/liang/xf/liang/2.txt 5 directories, 7 files
# -L 指定要显示的层级深度。(这里我显示的是2层) [root@localhost liang]# tree -L 2 /home /home └── liang ├── 2.txt ├── 3.txt ├── liang └── xf 3 directories, 2 files
12、dd命令
用于复制文件和转换数据。它用于将源文件复制到目标文件,或者用于在目标文件上进行数据转换。
选项 | 作用 |
---|---|
if=filename | 指定输入文件。 |
of=filename | 指定输出文件。 |
bs=size | 指定块大小。 |
count=num | 指定要复制的块数。 |
#复制passwd文件的内容到/home/liang/passwwd1文件里 [root@localhost liang]# ls 1.txt 2.txt 3.txt liang passwd xf [root@localhost liang]# dd if=./passwd of=/home/liang/passwwd1 记录了4+1 的读入 记录了4+1 的写出 2262字节(2.3 kB)已复制,0.00130587 秒,1.7 MB/秒 [root@localhost liang]# ls 1.txt 2.txt 3.txt liang passwd passwwd1 xf
#指定一块大小为1K的内容从文件passwd复制到文件passwd2里 [root@localhost liang]# dd if=passwd of=passwd2 bs=1k count=1 记录了1+0 的读入 记录了1+0 的写出 1024字节(1.0 kB)已复制,0.000436809 秒,2.3 MB/秒 [root@localhost liang]# ll -h 总用量 16K -rw-r--r--. 1 root root 5 5月 12 16:17 1.txt -rw-r--r--. 1 root root 0 5月 12 15:03 2.txt -rw-r--r--. 1 root root 4 5月 12 15:05 3.txt drwxr-xr-x. 2 root root 45 5月 12 15:40 liang -rw-r--r--. 1 root root 2.3K 5月 12 16:36 passwd -rw-r--r--. 1 root root 1.0K 5月 12 16:47 passwd2 drwxr-xr-x. 4 root root 30 5月 12 15:32 xf
13、链接命令
ln 命令用于在文件系统中创建链接(links)。链接分为两种类型:硬链接(hard link)和符号链接(symbolic link)。
硬链接(hard link):(目录不允许创建硬链接)
-
硬链接是文件系统中的一个目录条目,它指向另一个文件的数据块。
-
硬链接和原始文件共享同一个 inode,因此对文件的修改会影响到所有硬链接。
-
硬链接不能跨越文件系统边界。
符号(软)链接(symbolic link):
-
符号链接是一个特殊的文件,其内容是指向另一个文件的路径。
-
符号链接和原始文件不共享 inode,因此删除原始文件后,符号链接仍然存在,但无法访问。
-
符号链接可以跨越文件系统边界。
选项 | 作用 |
---|---|
-s | 创建符号链接而不是硬链接。 |
-f | 如果目标文件已存在,则覆盖它。 |
-i | 在覆盖目标文件之前询问用户。 |
-p | 保留源文件的属性,如权限和时间戳。 |
#创建一个1.txt的硬链接 [root@localhost liang]# ln 1.txt 01.txt [root@localhost liang]# ls -l![]()
#创建一个符号(软)链接 [root@localhost liang]# ln -s 1.txt 001.txt [root@localhost liang]# ll -h![]()
14、file命令
在UNIX和Linux系统中, file 命令用于确定文件的类型或属性。它可以提供有关文件的内容、文件系统类型、文件权 限等信息。
# -b 以二进制形式输出文件类型信息。 [root@localhost liang]# file -b 1.txt ASCII text
# -f 仅显示文件类型,不显示其他信息。 [root@localhost liang]# cat 1.txt 2221 [root@localhost liang]# file -f 1.txt 2221: cannot open (No such file or directory)
# -i 以人类可读的格式显示文件类型。 [root@localhost liang]# file -i 1.txt 1.txt: text/plain; charset=us-ascii
# -L 对符号链接进行特殊处理,查看链接指向的真实文件。 [root@localhost liang]# file -L 1.txt 1.txt: ASCII text
三、Vim编辑器
1、Vim操作模式
1.1、命令模式
操作 | 作用 |
---|---|
G | 跳转到文件末尾 |
gg | 返回文件首行 |
home键或者^ | 跳到首字符 |
end键或者$ | 跳到尾字符 |
yy | 单行复制 |
nyy | n行复制 |
P | 将光标移动到想要粘贴位置的上一行 |
p | 将光标移动到想要粘贴位置的下一行 |
u | 撤销操作,注意:进行保存且退出后无效 |
dd | 单行删除 |
ndd | n行删除 |
D或者d$ | 从所在行的光标处删除到末尾 |
d^ | 从所在行的光标处删除到行首 |
r | 替换某一个字符 |
del键 | 光标所在字符的后一个字符 |
/关键字 | 根据关键字查找,n:上翻;N:下翻 |
ZZ | 保存且退出 |
1.2、插入模式
操作 | |
---|---|
a | 进入插入模式,在当前光标的后一个字符插 |
o | 在当前光标的下一行插入 |
i | 在当前光标所在字符插入,返回命令模式时会将光标迁移一个字符 |
A | 在光标所在行的尾字符后插入 |
O | 在光标所在行的首字符插入 |
I(大写i) | 在当前光标的上一行插入 |
ESC(按键) | 返回命令模式 |
1.3、末行模式
作用:保存、退出及字符串替换
操作 | 作用 |
---|---|
:w | 保存 |
:q | 不保存退出 |
:wq或:x | 保存退出 |
! | 强制 |
:X->输入密码->:wq | 设置密码并保存退出 |
:X->回车两次->:wq 或者 :set key=""->:wq | 取消密码 |
:set nu | 设置行号 |
:set nonu | 取消行号 |
:nohl | 取消高亮 |
:行号 | 跳转到指定行 |
:%s/keyword/neword/ | 全文替换,首个匹配替换,当包含路径时,#替换/作为匹配符号 |
:%s/keyword/neword/g | 全文替换,所有匹配替换,当包含路径时,#替换/作为匹配符号 |
:指定行号s/keyword/neword/ | 指定行替换,首个匹配替换,当包含路径时,#替换/作为匹配符号 |
:指定行号s/keyword/neword/g | 指定行替换,所有匹配替换,当包含路径时,#替换/作为匹配符号 |
:起始行号,结束行号s/keyword/neword/ | 范围替换,首个匹配替换,当包含路径时,#替换/作为匹配符号 |
:起始行号,结束行号s/keyword/neword/g | 范围替换,所有匹配替换,当包含路径时,#替换/作为匹配符号 |
1.4、视图模式
操作 | 作用 |
---|---|
v | 进行行选中 |
1.5、视图快模式
操作 | 作用 |
---|---|
ctrl + v | 进行列选中 |
选中要删除的“#”,按del | 批量删除注释 |
选中要添加的列,输入 大写i ,输入#,按两次ESC | 批量增加注释 |
2、vim高级用法
操作 | 作用 |
---|---|
:r!command | 将命令结果插入文件,command命令的结果插入光标下一行 |
:nr! command | 将命令结果插入文件,command命令的结果插入n行后 |
:m,n! command | 使用页面内容作为输入,command以m,n之间的行作为标准输入, 标准输出会代替m,n之间的行. 注意这个操作似乎是不可用ctrl+r撤销的。要谨慎使用。 |
:! /bin/bash | 暂时离开vim来执行命令,使用完sh后exit又可以返回vim |