当前位置: 首页 > news >正文

学习Linux——文件管理

《文件管理》2025年11月7日

在 linux 中,一切皆文件。文件类型分为以下七种:

  1. d:表示目录
  2. -:表示普通文件
  3. l:表示链接文件
  4. c:表示字符设备文件
  5. s:表示套接文件
  6. p:表示管道文件
  7. b:表示块设备文件

目录

创建目录

使用mkdir命令创建目录,他的命令格式如下

Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.OPTION:
-p :表示创建多级目录

示例:

#1.创建单个目录
[root@localhost ~]# mkdir d1
[root@localhost ~]# ls
anaconda-ks.cfg  d1
#2.创建一个已有的目录,不想有报错可以使用 -p
[root@localhost ~]# mkdir d1 
mkdir: cannot create directory ‘d1’: File exists
[root@localhost ~]# mkdir d1 -p
#3.创建多个目录
[root@localhost ~]# mkdir d4 d2 d3
[root@localhost ~]# ls
anaconda-ks.cfg  d1  d2  d3  d4
#4.创建多级目录
[root@localhost ~]# mkdir a/b/c
mkdir: cannot create directory ‘a/b/c’: No such file or directory
创建多级目录时需用-p
[root@localhost ~]# mkdir a/b/c -p
[root@localhost ~]# tree a
a
└── b└── c2 directories, 0 files
查看目录

使用ls 命令来查看。这个命令用于查看目录的内容和目录本身。它的语法格式:

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).OPTION:
-R:递归查看目录下所有内容
-a:查看目录下所有文件,包括隐藏文件
-d:只查看目录本身不查看目录的内容
-i:查看文件所对应的inode值
-l:以长列表的方式查看目录下的所有文件信息
-n:显示文件所有者和所属组的id值
-r:反序显示文件内容

示例:

#1. 查看目录下的内容
[rootlocalhost]# ls /root
a anaconda-ks.cfg d1 d2 d3 d4
[root@localhost ~]# ls .
a  anaconda-ks.cfg  d1  d2  d3  d4
#2. 查看/root本身
[rootlocalhost]# ls /root -d
/root
#3.长列表显示
[root@localhost ~]# ls -ld /root
dr-xr-x---. 8 root root 4096 Oct 18 10:03 /root
#4.查看文件的inode值
[root@localhost ~]# ls -i /root67796546 a                 67796545 d1  201328839 d3
202123234 anaconda-ks.cfg  134869744 d2     586295 d4
# 5. 所有者和所属组以id的方式显示
[root@localhost ~]# ls -ldn /root
dr-xr-x---. 12 0 0 4096 Oct 18 10:01 /root# 6. 查看目录下所有文件
[root@localhost ~]# ls -a
.   a                .bash_history  .bash_profile  .cshrc  d2  d4  d6  d8    .tcshrc
..  anaconda-ks.cfg  .bash_logout   .bashrc        d1      d3  d5  d7  .ssh  .viminfo

在linux中,以 . 开头的文件属于隐藏目录

每一个目录下都会存在两个特殊的目录,. 和 … 目录,. 目录表示当前目录。… 表示当前目录的上一级目录。

切换目录

在 linux 中切换目录使用 cd 命令。

cd: cd [-L|[-P [-e]] [-@]] [dir]Change the shell working directory.

示例:

#1.切换目录
[root@localhost ~]# cd d1
[root@localhost d1]# pwd
/root/d1
#2.从d1目录切换到a/b/c目录
[root@localhost d1]# cd ../a/b/c
[root@localhost c]# 
#3.回到家目录
[root@localhost c]# cd
[root@localhost ~]# 
[root@localhost c]# ~
[root@localhost ~]# 
移动目录

使用 mv 命令来移动目录,语法格式如下:

Usage: mv [OPTION]... [-T] SOURCE DESTor:  mv [OPTION]... SOURCE... DIRECTORYor:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

示例:

#1.将/root/d1 目录修改为 /root/d11
[root@localhost ~]# mv d1 d11
[root@localhost ~]# ls
a  anaconda-ks.cfg  d11  d2  d3  d4#2.将目录/root/d2移动到/root/d3下面
[root@localhost ~]# mv d2 d3/
[root@localhost ~]# ls d3
d2
# 3. 将 /root/d4 目录移到 /root/d5 目录下并修改为 d44
[root@localhost ~]# mv d4 d5/d44
[root@localhost ~]# ls 
a  anaconda-ks.cfg  d11  d3  d5  d6  d7  d8
[root@localhost ~]# ls d5
d44
[root@localhost ~]# ls d5/d44/

总结:

1.使用mv命令时,如果所移动的目录在当前目录下,则会修改目录名称

2.如果两个参数名称不一样也是修改目录的名称

3.如果第二个参数最后一级的名称和第一个参数的名册不一样也是修改目录名称

复制目录

使用 cp 命令来复制目录。语法格式:

[root@localhost ~]# cp --help
Usage: cp [OPTION]... [-T] SOURCE DESTor:  cp [OPTION]... SOURCE... DIRECTORYor:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.OPTION:
-p:将文件的属性一同复制
-R, -r:递归复制目录

示例:

# 1. 将anaconda-ks.cfg复制到d7目录下
[root@localhost ~]# cp anaconda-ks.cfg d7/
[root@localhost ~]# ls d7
anaconda-ks.cfg
[root@localhost ~]# ls
a  anaconda-ks.cfg  d11  d3  d5  d6  d7  d8# 2. 将anaconda-ks.cfg复制到d8目录下并修改为anaconda.cfg
[root@localhost ~]# cp anaconda-ks.cfg d8/anaconda.cfg
[root@localhost ~]# ls d8
anaconda.cfg# 3. 将 a/b/c目录复制到d6目录下
[root@localhost ~]# echo aaa > a/b/c/demo.txt
[root@localhost ~]# tree a
a
└── b└── c└── demo.txt[root@localhost ~]# cp -R a d6/
[root@localhost ~]# tree d6
d6
└── a└── b└── c└── demo.txt3 directories, 1 file# 4. 复制文件的时复制文件的所有属性
# 4.1 在 /root 目录定义一个 my.sh 的脚本
#/bin/bashecho hello
# 4.2 给my.sh文件赋可执行权限
[root@localhost ~]# chmod +x my.sh
[root@localhost ~]# ls
a  anaconda-ks.cfg  d11  d3  d5  d6  d7  d8  my.sh[root@localhost ~]# ll
total 8
drwxr-xr-x. 3 root root  15 Oct 18 10:01 a
-rw-------. 1 root root 805 Oct 17 20:13 anaconda-ks.cfg
drwxr-xr-x. 2 root root   6 Oct 18 09:56 d11
drwxr-xr-x. 3 root root  16 Oct 18 10:24 d3
drwxr-xr-x. 3 root root  17 Oct 18 10:25 d5
drwxr-xr-x. 3 root root  15 Oct 18 10:54 d6
drwxr-xr-x. 2 root root  29 Oct 18 10:51 d7
drwxr-xr-x. 2 root root  26 Oct 18 10:52 d8
-rwxr-xr-x. 1 root root  23 Oct 18 10:56 my.sh[root@localhost ~]# cp my.sh d11
[root@localhost ~]# ls -l d11
total 4
-rwxr-xr-x. 1 root root 23 Oct 18 10:57 my.sh
[root@localhost ~]# cp -ap my.sh d11/my1.sh
[root@localhost ~]# ls -l
total 8
drwxr-xr-x. 3 root root  15 Oct 18 10:01 a
-rw-------. 1 root root 805 Oct 17 20:13 anaconda-ks.cfg
drwxr-xr-x. 2 root root  33 Oct 18 10:58 d11
drwxr-xr-x. 3 root root  16 Oct 18 10:24 d3
drwxr-xr-x. 3 root root  17 Oct 18 10:25 d5
drwxr-xr-x. 3 root root  15 Oct 18 10:54 d6
drwxr-xr-x. 2 root root  29 Oct 18 10:51 d7
drwxr-xr-x. 2 root root  26 Oct 18 10:52 d8
-rwxr-xr-x. 1 root root  23 Oct 18 10:56 my.sh
[root@localhost ~]# ls -l d11
total 8
-rwxr-xr-x. 1 root root 23 Oct 18 10:56 my1.sh
-rwxr-xr-x. 1 root root 23 Oct 18 10:57 my.sh

删除目录

删除目录

可以使用 rm 命令,语法格式:

Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).OPTION:
-f:强制删除不给提示
-i:交互式删除
-r, -R:递归删除目录
-d:删除空目录

示例:

[root@localhost ~]# tree .
.
├── a
│   └── b
│       └── c
│           └── demo.txt
├── anaconda-ks.cfg
├── d11
│   ├── my1.sh
│   └── my.sh
├── d3
│   └── d2
├── d5
│   └── d44
├── d6
│   └── a
│       └── b
│           └── c
│               └── demo.txt
├── d7
│   └── anaconda-ks.cfg
├── d8
│   └── anaconda.cfg
└── my.sh# 1. 删除 d3/d2 目录
[root@localhost ~]# rm d3/d2
rm: cannot remove 'd3/d2': Is a directory
# 提示不能删除这个目录,如果要删除,则需要还 -d 选项
[root@localhost ~]# rm d3/d2 -d
rm: remove directory 'd3/d2'? yes
[root@localhost ~]# ls d3# 2. 删除d7目录
[root@localhost ~]# rm -d d7
rm: cannot remove 'd7': Directory not empty
# 报错,-d 选项只能删除空目录。要想删除非常目录,需要带 -r|-R 选项
[root@localhost ~]# rm -r d7
rm: descend into directory 'd7'? yes
rm: remove regular file 'd7/anaconda-ks.cfg'? yes
rm: remove directory 'd7'? yes# 3. 删除k8目录并且不需要提示信息
[root@localhost ~]# rm -fr d8
[root@localhost ~]# ls
a  anaconda-ks.cfg  d11  d3  d5  d6  my.sh

文件

创建文件

touch

这个命令用于创建空白文件

#1.创建不存在的文件
[root@localhost ~]# ls
a  anaconda-ks.cfg  d11  d3  d4  d5  d6
[root@localhost ~]# touch  t1.txt
[root@localhost ~]# ls
a  anaconda-ks.cfg  d11  d3  d4  d5  d6  t1.txt
[root@localhost ~]# 
#查看文件元数据信息
[root@localhost ~]# stat t1.txtFile: t1.txtSize: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 201501361   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-10-18 11:12:58.825549235 +0800
Modify: 2025-10-18 11:12:58.825549235 +0800
Change: 2025-10-18 11:12:58.825549235 +0800Birth: 2025-10-18 11:12:58.825549235 +0800
#2创建已交存在的文件
[root@localhost ~]# touch  t1.txt
#然后再查看这个文件的元数据信息
[root@localhost ~]# stat t1.txtFile: t1.txtSize: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 201501361   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-10-18 11:14:36.475244506 +0800			(文件访问时间)
Modify: 2025-10-18 11:14:36.475244506 +0800			(内容修改时间)
Change: 2025-10-18 11:14:36.475244506 +0800			(内容更改时间)Birth: 2025-10-18 11:12:58.825549235 +0800			(内容创建时间)
[root@localhost ~]# 
使用vi/vim
[root@localhost ~]# vim t1.txt
[root@localhost ~]# cat t1.txt
a
b
c
c

vim 快捷键说明:

shift+g: 将光标定位到文件最后一行

gg:将光标定位到文件第一行

w:每按一次光标向后移动一个单词的位置

b:每按一次光标向前移动一个单词的位置

l(小写L):每按一次光标向后移动以一个字符位置

h:每按一次光标向前移动一个字符

k:每按一次光标向上移动一行

j:每按一次光标向下移动一行

shift+$:将光标移到当前行的最后

shfit+^:将光标移动到当前行的最前面

2、模式切换

i:将命令模式切换为编辑模式,所输入的字符在光标之前

l:将命令模式切换为编辑模式,并且将光标移到所以行的最前面

a:将命令模式切换为编辑模式,会将光标向后移到一位

A:将命令模式切换到编辑模式,会将光标移到所以行最后面

o:将命令模式切换为编辑模式,会在光标所在行之下生成一个新行

O:将命令模式切换到编辑模式,会在光标所在行之上生成一个新行

esc:将编辑模式切换到命令模式

3、文件写入(保存)

w:将文件写入(保存)

w filepath: 将文件另存为指定路径下

q:退出但不保存

wq:保存并退出

q!:强制退出并不保存

shfit+z+z:保存并退出

4、查找和替换

/keyword:根据关键字来查找,按 n 向下查找,按 N 向上查找

:%s/被替换的内容/将替换的内容/g

5、文件操作

yy:复制光标所在行

p:将复制的内容粘贴到光标所在行的下一行位置

shift+p:将复制的内容粘贴到光标所在行的上一行位置

Nyy:复制从光标所在行开始向下共N行

dd:删除光标所在行

Ndd:删除从光标所在位置向下数共N行

r:替换光标所在位置的字符

u:撤销之前的操作

echo
[root@localhost ~]# echo hello world > t1.txt
[root@localhost ~]# cat t1.txt 
hello world

上面的示例中使用输出重定向的功能,而输出重定向有两种:

  • > :表示以覆盖的方式输出到指定的文件中
  • >> :表示以追加的方式输出到指定文件中
[root@localhost ~]# echo 111 > t1.txt 
[root@localhost ~]# cat t1.txt 
111[root@localhost ~]# echo hello world >> t1.txt 
[root@localhost ~]# cat t1.txt 
111
hello world[root@localhost ~]# echo -n haha >> t1.txt 
[root@localhost ~]# cat t1.txt 
111
hello world
haha[root@localhost ~]# 
输入重定向

输入重定向所使用的符号为 < 或和 <<。注意这种方式需要接合命令完成,一般使用 cat 命令。

方式一:

利用cat向t2.txt 文件中输入<EOF····>EOF内容
[root@localhost ~]# cat > t2.txt <<EOF
> hello
> world
> python
> java
> EOF
[root@localhost ~]# cat t2.txt 
hello
world
python
javacat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF

查看文件

cat(适合小型文件,作用多,应用广)

优点:作用多、可操作空间大

缺点:会一次性滚完文件内所有内容,只适合小文件内容读取

核心作用是 “查看文件内容、拼接文件,或从输入设备读取内容”,名字来源于英文 “concatenate”(拼接)的缩写。

Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.OPTION:
-n:输出文件的每行的行号

一、基本用法:查看文件内容

#1.显示/etc/passwd文件的内容
[rootlocalhost]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
..................
#2.同时显示多个文件(按顺序显示[root@bogon ~]# cat t1.txt t2.txt
dasdhj
sdcbjsd
cisnank
scsak
----(此处是自己加的分割符)
d2112j
sdcbjsd
cis64656nk
sc65561
----

二、进阶用法:创建/修改文件

cat配合使用“重定向符号“( > 覆盖或 >>追加),可以快速创建文件或者往文件里面追加内容,不用使用编辑器打开(如:vim)

语法:cat > 文件名步骤:

执行命令后,终端会进入 “输入模式”,你可以直接输入内容(支持换行);

输入完后,按 Ctrl + c 结束输入,内容会保存到文件里。

示例:创建 note.txt 并写入两行内容

cat > note.txt
这是第一行内容
这是第二行内容
# 按 Ctrl + c 保存退出

注意:如果 note.txt 已经存在,执行这个命令会覆盖原内容,慎用,建议使用find先查找预览!

三、实用技巧:搭配其他选项

cat 有几个简单选项,能让查看内容更方便:

  1. 显示行号:-n选项

语法:cat -n 文件名示例:查看 test.txt 并显示每一行的编号

[root@bogon ~]# cat -n txt.txt1	sdajh2	jsak3	asijs4	hello 5	my wife
  • 效果:适合需要定位 “某一行内容” 的场景,比如排查配置文件错误时,能快速说清 “第 5 行有问题”。

显示特殊字符:-A 选项

语法:cat -A 文件名

  • 作用:显示文件里的 “隐藏特殊字符”,比如换行符(显示为 $)、制表符(显示为 ^I)。
  • 场景:排查 “文件格式问题”,比如 Windows 下的文件在 Linux 里显示乱码(换行符不同),用 cat -A 能看到差异。

示例:查看文件里的特殊字符

[root@bogon ~]# cat > ll.txt <<EOF
> first
> tab\t
>   
> EOF
[root@bogon ~]# cat -A ll.txt
first$
tab\t$$

四、高频组合:cat + 管道符(|

cat 常和管道符配合,把文件内容传给其他命令处理(比如筛选、统计)。

筛选内容:cat 文件名 | grep "关键词"

示例:从 t2.txt 里找包含 “s” 的行

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传统计行数 / 单词数:cat 文件名 | wc -l-l 统计行数)cat

示例:统计 t2.txt 有多少行内容

[root@localhost ~]# cat t2.txt | wc -l
5
less(适合大型文件阅读)

基本语法

less [选项] 文件名 -N:显示行号
-i:搜索时忽略大小写
-s:禁止行折叠,长行会横向滚动显示(避免内容被截断)

核心功能与操作

操作类型按键 / 命令功能说明
翻页 / 滚动PageUp/b向上翻一页
PageDown/space向下翻一页
/j向上滚动一行
/k向下滚动一行
搜索/关键词从当前位置向下搜索 “关键词”,按 n 找下一个匹配,N 找上一个匹配
?关键词从当前位置向上搜索 “关键词”,按 n/N 同理
跳转g跳转到文件开头
G跳转到文件结尾
数字+G跳转到第数字行(如 50G 跳转到第 50 行)
标记与返回maa 为任意字母)在当前位置标记为 a
'a跳转到标记 a 的位置
退出q退出 less
more

他的功能和less基本相同,区别在于more命令执行后会自动退出

基本语法

more [选项] 文件名
-num:指定每页显示的行数
-d:在底部显示提示信息(“按空格翻页,按q退出),避免用户误操作

核心操作

按键功能说明
space(空格)向下翻一页
Enter向下滚动一行
q退出 more
/关键词从当前位置向下搜索 “关键词”,找到后按 Enter 继续浏览
h显示帮助信息
=显示当前行号和文件总页数
head(查看头文件)

head是 Linux 系统中专注于查看文件开头内容的工具,默认显示文件前 10 行,可通过参数灵活控制显示行数,适合快速获取文件头部关键信息,避免加载整个文件。

基本语法

head [选项] 文件名
-n num :显示文件前num行(-n可省略,直接写数字)
-c 字节数:按字节数显示文件开头内容
-v :显示文件名(在多文件同时查看时,不同文件给上自己的文件名作为开头
tail(查看尾文件)

这个命令的作用是显示指定文件的最后几行,默认为10行,语法:

Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.OPTION:
-n : 指定显示的行数
-f : 监控文件的变化

使用示例:

# 1. 显示passwd文件最后10行内容
[root@localhost ~]# tail passwd 
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/:/usr/sbin/nologin
sssd:x:997:997:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin
chrony:x:996:996:chrony system user:/var/lib/chrony:/sbin/nologin# 2. 显示passwd文件最后3行数据
[root@localhost ~]# tail -n 3 passwd 
sssd:x:997:997:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin
chrony:x:996:996:chrony system user:/var/lib/chrony:/sbin/nologin
[root@localhost ~]# tail -3 passwd 
sssd:x:997:997:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin
chrony:x:996:996:chrony system user:/var/lib/chrony:/sbin/nologin# 3. 监控passwd文件的变化
[root@localhost ~]# tail -f passwd
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/:/usr/sbin/nologin
sssd:x:997:997:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin
chrony:x:996:996:chrony system user:/var/lib/chrony:/sbin/nologin# 然后开启另一个终端,并执行如下的命令:
[root@localhost ~]# while true;do echo hello; sleep 1 >> passwd;done
查看命令的优缺点分析
命令核心功能优点缺点适用场景
cat连续输出文件全部内容,支持合并文件1. 语法极简,执行速度快;2. 可合并多个文件(cat f1 f2 > f3);3. 支持创建短文件(cat > f.txt1. 无法分页,大文件内容会瞬间刷屏,无法回溯;2. 无搜索、跳转功能1. 查看小型文本文件(如配置文件、脚本);2. 合并多个文件;3. 快速创建或追加内容到短文件
more分页输出文件内容(仅向前翻页)1. 操作简单,学习成本低(空格翻页、q 退出);2. 轻量,启动速度快;3. 支持基础向下搜索(/关键词1. 仅支持向前翻页,无法回退;2. 功能单一,无行号、标记等高级功能1. 快速浏览中等长度文件,无需回退操作;2. 分页查看命令输出(如 ls -lRmore);3. 对操作复杂度要求低的场景
less灵活分页浏览,支持双向操作1. 支持前后翻页、行号显示(-N)、双向搜索(/向下、?向上);2. 可标记位置(ma)、跳转行(50G);3. 大文件加载高效(无需读取全部内容)1. 操作逻辑较复杂(需记忆快捷键);2. 启动速度略慢于 more/head1. 查看大型文件(如日志、数据库备份文件);2. 需要搜索、跳转的场景(如定位日志中的错误信息);3. 需反复回溯查看内容的场景
head输出文件开头部分(默认前 10 行)1. 仅加载文件头部,大文件场景下极高效;2. 可按行数(-n)或字节数(-c)精准控制输出;3. 语法简单,易上手1. 仅能查看文件开头,无法查看中间或尾部内容;2. 无分页、搜索功能1. 快速查看文件头部信息(如日志起始记录、CSV 文件表头);2. 结合管道筛选输出前几行(如 ps auxhead -6 看 top5 进程);3. 验证文件格式(如配置文件开头注释)
tail输出文件尾部部分(默认后 10 行)1. 支持实时跟踪文件更新(-f,如监控日志);2. 可按行数(-n)或字节数(-c)控制输出;3. 大文件尾部读取高效1. 仅能查看文件尾部,无法查看开头或中间内容;2. 无分页、搜索功能1. 查看文件最新内容(如日志末尾的错误记录);2. 实时监控文件变化(如 tail -f /var/log/messages 监控系统日志);3. 提取文件最后几行数据(如统计报表末尾的汇总行)

修改文件

一、mv

语法

mv -[选项] 所选目录/文件名 目标目录/修改后的名称
常用选项:
1.-f:强制移动/重命名(覆盖已有文件)
[root@bogon ~]# tree
.
└── 1├── 2│   └── ll.txt└── d3└── txt.txt3 directories, 2 files
[root@bogon ~]# mv 1/d3/txt.txt 1/2/ll.txt
mv: overwrite '1/2/ll.txt'? (不使用时会询问)
[root@bogon ~]# mv -f 1/d3/txt.txt 1/2/ll.txt
[root@bogon ~]# tree
.
└── 1├── 2│   └── ll.txt└── d33 directories, 1 file2.-i:交互是操作(覆盖前提示,需手动确认)
[root@bogon ~]# tree
.
├── 1
│   ├── 2
│   │   └── ll.txt
└── a.txt3 directories, 3 files
[root@bogon ~]# mv -i a.txt 1/2/ll.txt
mv: overwrite '1/2/ll.txt'? y
[root@bogon ~]# tree
.
└── 1├── 2└── ll.txt3 directories, 2 files3.-v:显示移动过程
[root@bogon ~]# mv -v ./1/1.txt /a.txt
renamed './1/1.txt' -> '/a.txt'

例子

[root@bogon ~]# tree
.
├── 1
│   ├── 2
│   │   └── ll.txt
│   └── d3
│       └── txt.txt
└── a.txt3 directories, 3 files
[root@bogon ~]# mv ./a.txt ./1/1.txt
[root@bogon ~]# tree
.
└── 1├── 1.txt├── 2│   └── ll.txt└── d3└── txt.txt

二、rename

在Linux中rename分C语言和perl语言版本
C语言版本的替换命令:
rename old new ***(匹配的文件)
例子:替换所有以.txt文件后缀的文件中的t为T
[root@localhost]# rename s t *.txt
#perl语言版本的替换命令:
rename 's/old/new' ***(匹配的文件)
例子:替换所有以.txt文件后缀的文件中的t为T
[root@localhost]# rename 's/t/T/' *.txt

移动文件

mv命令

参考修改命令的mv内容

复制文件

cp命令

基本语法

cp [选项] 目录/源文件 目标文件/目录
[root@bogon ~]# cp 1/2/ll.txt cp1.txt
[root@bogon ~]# tree
.
├── 1
│   ├── 2
│   │   └── ll.txt
│   └── d3
└── cp1.txt
  • -i:交互式复制(如果目标文件存在同名文件,提示是否覆盖)
- [root@bogon ~]# cp -i  cp1.txt 1/2/ll.txt
- cp: overwrite '1/2/ll.txt'? y
  • -r/-R:递归复制目录(含目录内所有文件和字目录)
[root@bogon ~]# cp -r 1/2 cp1
[root@bogon ~]# tree
.
├── 1
│   ├── 2
│   │   └── ll.txt
│   └── d3
├── cp1
│   └── ll.txt
└── cp1.txt
  • -p:保留文件属性(权限、时间戳、所有者等,但不复制同一个indoe值)
[root@bogon ~]# ls -i cp1.txt cp2.txt
201327425 cp1.txt  201327426 cp2.txt
[root@bogon ~]# ls -l cp1.txt cp2.txt
-rw-r--r--. 1 root root 31 Oct 23 13:37 cp1.txt
-rw-r--r--. 1 root root 31 Oct 23 13:52 cp2.txt
[root@bogon ~]# cp -p cp1.txt cp2.txt
cp: overwrite 'cp2.txt'? y
[root@bogon ~]# ls -i -l cp1.txt cp2.txt
201327425 -rw-r--r--. 1 root root 31 Oct 23 13:37 cp1.txt
201327426 -rw-r--r--. 1 root root 31 Oct 23 13:37 cp2.txt
  • -v:显示复制过程的详细信息
[root@bogon ~]# cp -v cp1.txt cp3.txt 
'cp1.txt' -> 'cp3.txt'
  • -f:强制复制,覆盖文件时不做提醒
[root@bogon ~]# cp -f cp1.txt cp3.txt
  • -a:归档复制,保留所有属性并递归复制目录
[root@bogon ~]# tree
.
├── 1
│   ├── 2
│   │   └── ll.txt
│   └── d3
├── cp1
│   └── ll.txt
├── cp1.txt
├── cp2.txt
└── cp3.txt4 directories, 5 files
[root@bogon ~]# cp -a 1 2
[root@bogon ~]# tree
.
├── 1
│   ├── 2
│   │   └── ll.txt
│   └── d3
├── 2
│   ├── 2
│   │   └── ll.txt
│   └── d3
├── cp1
│   └── ll.txt
├── cp1.txt
├── cp2.txt
└── cp3.txt7 directories, 6 files

常见错误分析

1.复制目录时忘记加-r

cp dir1 dir2  # 错误:未加 -r 会提示“omitting directory ‘dir1’”
cp -r dir1 dir2  # 正确:递归复制目录

2.覆盖文件时需要确认

若需批量覆盖文件且不想逐个确认,可结合 -f 选项;若需逐个确认,用 -i 选项。

删除文件

rm命令

基本语法:

rm [选项] 目录/文件
[root@localhost]#rm 1/cp1.txt

选项:

-i:交互式删除(删除前提示确认)

[root@bogon ~]# rm -i cp3.txt
rm: remove regular file 'cp3.txt'? y

-r/R:递归删除目录(含目录内所有文件和子文件)

[root@bogon ~]# rm -r 2
rm: descend into directory '2'? y
rm: descend into directory '2/2'? y
rm: remove regular file '2/2/ll.txt'? y
rm: remove directory '2/2'? y
rm: remove directory '2/d3'? y
rm: remove directory '2'? y

-f:强制删除(无提示,直接删除)常和-r组合使用

[root@bogon ~]# rm -f cp2.txt

-v:显示删除过程的详细信息

[root@bogon ~]# rm -v cp1/ll.txt
rm: remove regular file 'cp1/ll.txt'? y
removed 'cp1/ll.txt'

查找文件

whereis

这个文件是查找可执行文件在哪,以及他的帮忙文档的路径

[root@localhost ~]# whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz
which

这个文件是查找可执行文件在哪,但是不是显示帮忙文件的路径

[root@localhost ~]# which cat
/usr/bin/cat
who查看当前连接的终端信息
[root@localhost ~]# who
root     tty1         2025-10-19 09:10
root     pts/0        2025-10-19 09:10 (192.168.72.1)
root     pts/1        2025-10-19 09:10 (192.168.72.1)
whoami

查看当前登录用户名

[root@localhost ~]# whoami
root
w

这个命令可以查看连接终端的用户信息,包括连接时间,连接时长,以及平均负载。

[root@localhost ~]# w11:20:22 up  2:10,  3 users,  load average: 0.00, 0.00, 0.00
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1      09:10    2:09m  0.01s  0.01s -bash
root     pts/0     09:10   12:46   5.53s  5.53s -bash
root     pts/1     09:10    0.00s  0.23s  0.01s w
find
find [搜索路径] [搜索条件] [操作] #(在哪找?找什么?要干嘛?)

搜索路径:默认为当前路径
搜索条件:搜索时制定的规则(文件名、类型、大小等)
操作:对找到的结果执行的后续操作

搜索选项:

-name:按区分大小写的文件名查找

-iname:按不区分大小写的文件名查找

-path:按所在路径查找

-type:按文件类型查找

-size:按文件大小查找

-inam:按Indoe值查找

-atime:按最后访问时间查找

-ctime:按创造时间查找

-mtime:按修改时间查找

-perm:按权限查找

-perm -权限制:按指定权限查找

-user:按所有者查找

-group:按所有组查找

链接文件

在 linux 中链接有两种方式:

  • 软链接
  • 硬链接
软链接

创建链接所使用的命令是 ln 命令,它的语法如下:

Usage: ln [OPTION]... [-T] TARGET LINK_NAMEor:  ln [OPTION]... TARGETor:  ln [OPTION]... TARGET... DIRECTORYor:  ln [OPTION]... -t DIRECTORY TARGET...
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist.  Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.OPTOIN:
-s :创建符号链接

使用示例:

# 数据准备
[root@localhost ~]# rm -rf *
[root@localhost ~]# 
[root@localhost ~]# touch t1.txt
[root@localhost ~]# touch t2.txt
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 19 11:41 t1.txt
-rw-r--r--. 1 root root 0 Oct 19 11:42 t2.txt# 1. 给t1.txt文件创建软件链接,名称为 t1_link.txt
[root@localhost ~]# ln -s t1.txt t1_link.txt
[root@localhost ~]# ll
total 0
lrwxrwxrwx. 1 root root 6 Oct 19 11:43 t1_link.txt -> t1.txt
-rw-r--r--. 1 root root 0 Oct 19 11:41 t1.txt
-rw-r--r--. 1 root root 0 Oct 19 11:42 t2.txt# 2. 给t1.txt文件添加内容
[root@localhost ~]# echo haha > t1.txt 
[root@localhost ~]# cat t1.txt 
haha
[root@localhost ~]# cat t1_link.txt 
haha[root@localhost ~]# echo hehe >> t1_link.txt 
[root@localhost ~]# cat t1_link.txt 
haha
hehe
[root@localhost ~]# cat t1.txt 
haha
hehe# 3. 查看源文件和链接文件的inode值
[root@localhost ~]# ls -i t1.txt t1_link.txt 
201327050 t1_link.txt  201327040 t1.txt# 4. 删除链接t1.txt
# 4.1 备份t1.txt文件
[root@localhost ~]# cp t1{,.bak}.txt
[root@localhost ~]# ls
t1.bak.txt  t1_link.txt  t1.txt  t2.txt
# 4.2 删除t1.txt文件
[root@localhost ~]# rm -f t1.txt
[root@localhost ~]# ls
t1.bak.txt  t1_link.txt  t2.txt
# 4.3 链接文件不可用
[root@localhost ~]# cat t1_link.txt
cat: t1_link.txt: No such file or directory
硬链接
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 10 Oct 19 11:46 t1.bak.txt
lrwxrwxrwx. 1 root root  6 Oct 19 11:43 t1_link.txt -> t1.txt
-rw-r--r--. 1 root root  0 Oct 19 11:42 t2.txt# 1. 为t2.txt文件做硬链接名称为t2_link.txt
[root@localhost ~]# ln t2.txt t2_link.txt
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 10 Oct 19 11:46 t1.bak.txt
lrwxrwxrwx. 1 root root  6 Oct 19 11:43 t1_link.txt -> t1.txt
-rw-r--r--. 2 root root  0 Oct 19 11:42 t2_link.txt
-rw-r--r--. 2 root root  0 Oct 19 11:42 t2.txt# 2. 查看t2.txt和t2_link.txt文件的inode值
[root@localhost ~]# ls -i t2.txt t2_link.txt
201327049 t2_link.txt  201327049 t2.txt# 3. 给t2.txt文件添加内容
[root@localhost ~]# echo xixi > t2.txt
[root@localhost ~]# cat t2.txt 
xixi
[root@localhost ~]# cat t2_link.txt
xixi[root@localhost ~]# echo hehe >> t2_link.txt
[root@localhost ~]# cat t2_link.txt
xixi
hehe
[root@localhost ~]# cat t2.txt 
xixi
hehe# 4. 删除t2.txt
[root@localhost ~]# rm -f t2.txt 
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root 10 Oct 19 11:46 t1.bak.txt
lrwxrwxrwx. 1 root root  6 Oct 19 11:43 t1_link.txt -> t1.txt
-rw-r--r--. 1 root root 10 Oct 19 11:51 t2_link.txt[root@localhost ~]# cat t2_link.txt 
xixi
hehe

[!IMPORTANT]

软件链接和硬链接的区别:

1、创建链接的方式不同,软件链接需要带 -s 选项

2、软链接文件和源文件的inode值不同,而硬链接文件与源文件的inode值相同

3、创建软链接后文件的链接数不变,而创建硬链接文件后文件的链接数会加 1

4、对于软链接文件如果删除源文件则链接文件不可用,而对于硬链接文件如果删除源文件则只是将链接数据减 1。

归档文件

tar 命令详解(归档与压缩)

tar命令用于创建、查看、解压归档文件(.tar),常与压缩算法(gzip/bzip2/xz)结合生成压缩包(如.tar.gz),是源码包、日志文件等批量处理的核心工具。

基本语法

tar [选项] 文件名/目录名  # 选项决定操作类型(创建/解压/查看)

选项

-c 创建归档文件(create)

 tar -cf d1.tar d1/
[root@haha ~]# tree
.
├── d1
│   └── d2
│       └── t.txt
├── d1.tar

-x 解压归档文件(extract)

[root@haha ~]# tar -xvf d1.tar
d1/
d1/d2/
d1/d2/t.txt
#-v显示解压文件列表

-f 指定归档文件名(file),必须放在选项末尾(如tar -cf archive.tar dir/)

# 正确用法:-f 紧跟文件名,且放在选项最后
tar -cvf logs.tar /var/log/
# 错误用法:-f 位置错误(会报错)
tar -cfv logs.tar /var/log/  # 错误!-f必须在选项末尾

-v 显示操作过程(verbose,详细输出)

[root@haha ~]# tar -cvf d1_1.tar d1
d1/
d1/d2/
d1/d2/t.txt

-z 用gzip压缩 / 解压(处理.tar.gz/.tgz格式)

[root@haha ~]# tree
.
├── d1
│   └── d2
│       └── t.txt
├── d1_1.tar3 directories, 12 files
[root@haha ~]# tar -zcvf d1.tar.gz d1
d1/
d1/d2/
d1/d2/t.txt
[root@haha ~]# tree
.
├── d1
│   └── d2
│       └── t.txt
├── d1_1.tar
├── d1.tar.gz

-j 用bzip2压缩 / 解压(处理.tar.bz2格式)

# 打包并以bzip2压缩(生成.tar.bz2文件)
tar -jcvf backup.tar.bz2 /home/user/
# 输出:显示压缩的文件列表# 解压.tar.bz2文件
tar -jxvf backup.tar.bz2

-J 用xz压缩 / 解压(处理.tar.xz格式,压缩率更高)

# 打包并以xz压缩(生成.tar.xz文件,压缩率最高)
tar -Jcvf archive.tar.xz documents/
# 输出:显示压缩的文件列表# 解压.tar.xz文件
tar -Jxvf archive.tar.xz

-t 查看归档文件内容(list)

[root@haha ~]# tar -tf d1.tar.gz 
d1/
d1/d2/
d1/d2/t.txt

-C 解压到指定目录(如tar -xf archive.tar -C /tmp)

[root@haha ~]# tree
.
├── d1
│   └── d2
│       └── t.txt
├── d1_1.tar
[root@haha ~]# tar -xf d1_1.tar -C d1/d2/
[root@haha ~]# tar -xvf d1_1.tar -C d1/d2/
d1/
d1/d2/
d1/d2/t.txt
[root@haha ~]# tree
.
├── d1
│   └── d2
│       ├── d1
│       │   └── d2
│       │       └── t.txt
│       └── t.txt

常用场景示例

  • 创建.tar.gz压缩包(打包并压缩nginx-1.25.3目录):

    tar -zcvf nginx-1.25.3.tar.gz nginx-1.25.3/  # -z:gzip压缩;-c:创建;-v:显示过程;-f:指定文件名
    
  • 解压.tar.gz(解压到当前目录):

    tar -zxvf nginx-1.25.3.tar.gz  # -x:解压;其他选项同上
    
  • 解压到指定目录(将包解压到/usr/local/src):

    tar -zxvf nginx-1.25.3.tar.gz -C /usr/local/src/  # -C:指定目标目录
    
  • 查看压缩包内容(不解压,仅列出文件):

    tar -ztvf nginx-1.25.3.tar.gz  # -t:查看;-v:显示详细信息
    
  • 处理.tar.bz2格式(用-j选项):-

    tar -jcvf data.tar.bz2 data/  # 创建
    tar -jxvf data.tar.bz2        # 解压
    
http://www.dtcms.com/a/581236.html

相关文章:

  • C# OpenCVSharp使用yolo11n人脸关键点检测模型进行人脸检测
  • 【ATL定时器深度解析:概念、功能与项目实战指南】
  • wp_head() wordpressseo优化软件有哪些
  • 个人网站如何制作教程电子商务网站建设重点
  • 机器学习实践项目(二)- 房价预测增强篇 - 特征工程四
  • C++20之三路比较运算符
  • 微网站方案用dw做的网站怎么上线
  • 微软解除 Win11 限制,“毛玻璃”效果将无处不在
  • 班级回忆录系统|基于SpringBoot和Vue的班级回忆录系统(源码+数据库+文档)
  • 让 ETL 更懂语义:DataWorks 支持数据集成 AI 辅助处理能力
  • 新能源汽车底盘紧固件的“防腐密码”:从技术革新到体系共创
  • 前端性能优化实战:从理论到实践的深度解析
  • 【C++:红黑树】深入理解红黑树的平衡之道:从原理、变色、旋转到完整实现代码
  • 网站怎么做翻页徐州专业网站seo
  • 《算法通关指南数据结构和算法篇(4)--- 队列和queue》
  • 云计算运维监控实战:生产环境与自建方案对比
  • 深入理解MySQL行锁,间隙锁和临键锁
  • 鸿安建设集团网站wordpress主题2019
  • 申请软著,怎么快速整理软件源代码
  • sam2 点选 分割图片 2025
  • 网站开发源程序重庆建筑人才网官网
  • 如何理解蒙特卡洛方法并用python进行模拟
  • 公司网站代码模板wordpress 虎嗅网
  • 在 Windows 中清理依赖node_modules并重新安装
  • 【数据结构】从零开始认识图论 --- 并查集与最小生成树算法
  • 使用 AWS WAF 防护 Stored XSS 攻击完整指南
  • 当爬虫遇到GraphQL:如何分析与查询这种新型API?
  • 游戏手柄遥控越疆协作机器人[一]
  • MATLAB实现决策树数值预测
  • Maven 多模块项目与 Spring Boot 结合指南