1)文件查看

ls

这个命令的作用是查看目录下所有文件或子目录

# 使用格式
ls [option]
​
option:
-a :查看所有文件,包括隐藏文件或目录
-l : 以长列表的方式来查看目录下的内容
-d :查看目录本身而不是目录中的内容
-i :查看文件的inode值
-n :查看文件创建者的uid和gid值
-R :递归查看目录下的内容

cat

用于查看文件内容,它会把文件内容全部加载进内存中。所以这个命令一般用于小文件的内容查看。

# 使用格式
cat [option]
​
option:
-n :显示文件中每一行的内容和行号,包括空行
-b :显示文件中每一行的内容和行号,但是会跳过空行
-E :每行后会以$符号结尾

less

可以按分页的方式来查看文件的内容

1. 按回车是一次向下滚动一行
2. 按空空格是一次向下翻一页
3. 按q键退出

more

可以按分页的方式来查看文件的内容

1. 按回车是一次向下滚动一行
2. 按空空格是一次向下翻一页

注意:more和less的功能基本相同,但是也有区别:

  1. more会显示文件的百分比,less没有

  2. less查看完文件后需要按q才能退出命令,而more查看完文件后自动退出

head

查看文件前指定的行数,默认会显示文件的前10行

option:
-n num :指定要查看的行数

tail

查看文件末尾指定的行数,默认会显示文件最后10行

option:
-n num :指定要查看的行数
-f :监控文件内容的变化

2)文件或目录创建

mkdir

这个命令用于目录创建

1. 创建一级目录
[root@localhost ~]# mkdir d2 d3 d4
[root@localhost ~]# ls
anaconda  anaconda-ks.cfg  d1  d2  d3  d4
​
2. 创建一级目录
[root@localhost ~]# mkdir d{11..14}
[root@localhost ~]# ls
anaconda  anaconda-ks.cfg  d1  d11  d12  d13  d14  d2  d3  d4
​
3. 创建多级目录
[root@localhost ~]# mkdir -p a/b/c
[root@localhost ~]# tree a
a
└── b
    └── c

touch

用于创建空白文件

1. 创建一个名为f1的空白文件,并且这个文件不存在
[root@localhost ~]# touch f1
[root@localhost ~]# ls
a         anaconda-ks.cfg  d11  d13  d2  d4
anaconda  d1               d12  d14  d3  f1
​
2. 一次创建多个空白文件
[root@localhost ~]# touch f2 f3 f4
[root@localhost ~]# ls
a         anaconda-ks.cfg  d11  d13  d2  d4  f2  f4
anaconda  d1               d12  d14  d3  f1  f3
​
3. 一次创建多个空白文件
[root@localhost ~]# touch f{44..46}
[root@localhost ~]# ls
a         anaconda-ks.cfg  d11  d13  d2  d4  f2  f4   f45
anaconda  d1               d12  d14  d3  f1  f3  f44  f46

注意:如果touch命令在创建时文件不存在则会将该文件创建好;如果被创建的文件已经存在了,则会修改文件最后访问时间、最后修改时间和最后元数据的修改时间,而创建时间不变。可通过 stat 命令来查看

vi/vim

也可以通过 vi/vim 编辑器来创建文件。格式为 vi/vim 文件路径

[root@localhost ~]# vim t1.txt
[root@localhost ~]# cat t1.txt
hello
python
java
springboot

使用 vim/vi 编辑创建文件时,如果文件不存在则创建,如果文件存在则打开。

vim 编辑器常用快捷键:

1、光标移动

shift+G:将光标定位到文件的最后一行的行首位置

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

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

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

l:每按一次光标向后移动一个字符的距离

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

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

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

shift+^:将光标定位到当前所在行的行首位置

shift+$:将光标定位到当前所在行的行尾位置

2、模式切换

i:将命令模式切换为编辑模式,并且在光标所在位置之前插入内容

I:将命令模式切换为编辑模式,并且会将光标移动到行首位置,然后在光标之前插入内容

a:将命令模式切换为编辑模式,并且光标后向移动一位后在光标之前插入内容

A:将命令模式切换为编辑模式,并且会将光标移动到行尾位置,然后在光标之前插入内容

o:将命令模式切换为编辑模式,并且在光标所在行之下插入新行

O:将命令模式切换为编辑模式,并且在光标所在行之上插入新行

s:将命令模式切换为编辑模式,并且会将光标所在位置的字符删除

S:将命令模式切换为编辑模式,并且会将光标所在行删除

esc:从编辑模式切换为命令模式

: :将命令模式切换为末行模式

3、文件操作

yy:复制光标所在行

p:在光标所在行之下粘贴复制的内容

shift+p:在光标所在行之上粘贴复制的内容

Nyy:复制指定的行数(N行)

dd:删除光标所在行

Ndd:从光标所在行及向下删除N行

d+shift+$:从光标所在位置向后删除到行尾

d+shift+^:从光标所在位置向前删除到行首

d+w:删除光标所在位置之后的单词

r:修改光标所在位置的字符

u:撤销前面的操作

4、文件查找和替换

/keyword:根据指定的keyword来查找,查找到后,按 n 向下查找,按 N 向上查找

:%s/old/new/g:将new替换所有的old,如果不带g,则只替换一个

5、文件保存

w:保存文件但不退出

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

q:退出但不保存

q!:强制退出且不保存

wq:保存并退出

shift+zz:保存并退出

echo

我们也可以使用 echo 命令来创建文件并给文件指定内容。

[root@localhost ~]# echo haha hehe xixi > t2.txt
[root@localhost ~]# cat t2.txt 
haha hehe xixi

使用 echo 命令时,需要使用到输出重定向符号。

> :以覆盖的形式将 echo 命令之后的内容写入到指定的文件中

>> :以追加的形式将 echo 命令之后的内容写入到指定的文件中

输出和输入重定向

这种方式也是需要大家重点掌握

# 1. 将t2.txt文件的内容写入到t3.txt文件中
[root@localhost ~]# cat t2.txt > t3.txt
[root@localhost ~]# cat t3.txt 
11
haha hehe xixi
​
# 2. 将指定的内容写入到t4.txt文件中
[root@localhost ~]# cat > t4.txt <<EOF
> hello
> world
> python
> EOF
[root@localhost ~]# cat t4.txt 
hello
world
python
​
​
# 另一种写法
cat <<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

3)修改文件

我们使用 mv 命令来实现

[root@localhost ~]# mkdir d1
[root@localhost ~]# ls
d1  passwd  t1.txt  t2.txt  t3.txt  t4.txt
​
# 1. 将d1目录更名为d11
[root@localhost ~]# mv d1 d11
[root@localhost ~]# ls
d11  passwd  t1.txt  t2.txt  t3.txt  t4.txt
​
# 2. 将t1.txt文件更名为t11.txt
[root@localhost ~]# mv t1.txt t11.txt
[root@localhost ~]# ls
d11  passwd  t11.txt  t2.txt  t3.txt  t4.txt
​
# 3. 将t11.txt文件放到d11目录下并修改为t1.txt
[root@localhost ~]# mv t11.txt d11/t1.txt
[root@localhost ~]# ls
d11  passwd  t2.txt  t3.txt  t4.txt
[root@localhost ~]# ls d11/
t1.txt

4)复制文件

使用 cp 命令来实现

[root@localhost ~]# mkdir d2
[root@localhost ~]# tree .
.
├── d11
│   └── t1.txt
├── d2
├── passwd
├── t2.txt
├── t3.txt
└── t4.txt
​
# 1. 复制t2.txt文件当前目录下并指定为t5.txt
[root@localhost ~]# cp t2.txt t5.txt
[root@localhost ~]# cat t2.txt t5.txt 
11
haha hehe xixi
11
haha hehe xixi
​
# 2. 复制t3.txt到d2目录下
[root@localhost ~]# cp t3.txt d2/
[root@localhost ~]# ls d2
t3.txt
​
# 3. 复制目录
# 3.1 将d11目录复制到d2目录下
[root@localhost ~]# cp d11 d2/
cp: -r not specified; omitting directory 'd11'
# 将我们复制目录时,需要在命令上带 -r 选项,用于递归复制
[root@localhost ~]# cp d11 d2/ -r
[root@localhost ~]# tree .
.
├── d11
│   └── t1.txt
├── d2
│   ├── d11
│   │   └── t1.txt
│   └── t3.txt
├── passwd
├── t2.txt
├── t3.txt
├── t4.txt
└── t5.txt
​
# 3.2 复制目录或者文件时将目录或文件的属性一同复制
[root@localhost ~]# cp -rp d11 d2/d11/
# 需要在命令之后带 -p 选项

5)查找文件

who

这个命令用于查看那些终端正连接。

[root@localhost ~]# who
root     tty1         2025-10-18 19:13
root     pts/0        2025-10-18 19:13 (192.168.72.1)

w

这个命令除了可以查看连接所终端外,还可以查看用户的数量以及平均负载数。

[root@localhost ~]# w
 20:28:25 up  1:15,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1      19:13    1:14m  0.02s  0.02s -bash
root     pts/0     19:13    1.00s  0.15s  0.01s w

whereis

查找文件在哪些位置

[root@localhost ~]# whereis t2.txt
t2.txt:

which

查找可执行文件所以路径及说明信息

[root@localhost ~]# which cd
/usr/bin/cd

find

根据条件查找文件

Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]
​
find - search for files in a directory hierarchy
​
-type :根据文件类型查找
-name :根据文件名称查找
-perm : 根据文件权限查找
-size :根据文件大小查找

使用示例:

[root@localhost ~]# touch f1.txt
[root@localhost ~]# mkdir d1
[root@localhost ~]# ls
d1  f1.txt
[root@localhost ~]# touch f1.txt
[root@localhost ~]# touch d1/f1.txt
[root@localhost ~]# mkdir d2
[root@localhost ~]# touch d2/d1
[root@localhost ~]# tree .
.
├── d1
│   └── f1.txt
├── d2
│   └── d1
└── f1.txt
​
​
# 1. 在 /root 目录下查找名称为 f1.txt 的文件
[root@localhost ~]# find /root -name f1.txt
/root/f1.txt
/root/d1/f1.txt
​
​
[root@localhost ~]# touch F1.txt
[root@localhost ~]# ls
d1  d2  f1.txt  F1.txt
​
# 2. 在 /root 目录下查找文件名称为 d1 并且文件类型为普通文件
[root@localhost ~]# find /root -name d1
/root/d1
/root/d2/d1
# 但是这个结果不是我们想要的,因为包含了目录和文件
[root@localhost ~]# find /root -name d1 -type f
/root/d2/d1
# 此时的结果才是我们需要的
​
# 3. 根据权限查找
[root@localhost ~]# ls -l /root/d2/d1
-rw-r--r--. 1 root root 0 Oct 18 20:42 /root/d2/d1
[root@localhost ~]# ll /root/d1 -d
drwxr-xr-x. 2 root root 20 Oct 18 20:42 /root/d1
​
# 3.1 查找/root下权限为755并且名称为d1的文件
[root@localhost ~]# find /root -name d1 -perm 0755
/root/d1

6)删除文件

通过 rm 命令来实现,命令的格式:

Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).
​
OPTION:
-f :强制删除文件且没有提示信息
-r, -R :递归删除目录下所有内容
-d :删除空白目录

使用示例:

[root@localhost ~]# mkdir d{3..6}
[root@localhost ~]# mkdir a/b/c -p
[root@localhost ~]# echo t.txt > a/b/c/t.txt
[root@localhost ~]# tree .
.
├── a
│   └── b
│       └── c
│           └── t.txt
├── d1
│   └── f1.txt
├── d2
│   └── d1
├── d3
├── d4
├── d5
├── d6
├── f1.txt
└── F1.txt
​
​
# 1. 删除d3目录
[root@localhost ~]# rm d3
rm: cannot remove 'd3': Is a directory
# 要想成功删除,需要带 -d 选项
[root@localhost ~]# rm -d d3
rm: remove directory 'd3'? yes
[root@localhost ~]# ls
a  d1  d2  d4  d5  d6  f1.txt  F1.txt
​
# 2. 删除d1目录
[root@localhost ~]# rm -d d1
rm: cannot remove 'd1': Directory not empty
# 要想成功删除,需要带 -r 选项
[root@localhost ~]# rm -r d1
rm: descend into directory 'd1'? yes
rm: remove regular empty file 'd1/f1.txt'? yes
rm: remove directory 'd1'? yes
[root@localhost ~]# ls
a  d2  d4  d5  d6  f1.txt  F1.txt
​
# 3. 如果删除时不提示
[root@localhost ~]# mkdir -p a/b/c
[root@localhost ~]# rm -rf a
[root@localhost ~]# ls
d2  d4  d5  d6  f1.txt  F1.txt
​
# 4. 同时删除多个文件
[root@localhost ~]# ls
d2  d4  d5  d6  f1.txt  F1.txt
[root@localhost ~]# rm -rf d2 d3 f1.txt
[root@localhost ~]# ls
d4  d5  d6  F1.txt
​
# 5. 删除指定目录下所有文件
[root@localhost ~]# ls
d4  d5  d6  F1.txt
[root@localhost ~]# rm -rf *
[root@localhost ~]# ls
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐