Linux文件操作命令,常用命令有哪些?

  • 时间:2025-10-22 17:48 作者: 来源: 阅读:5
  • 扫一扫,手机访问
摘要:1.1 文件创建命令(touch)该命令的作用主要为创建文件,就类似于Windows系统中的新建功能,列如新建一个*.txt、*.md文件,该命令可以在当前目录下创建文件,也可以在指定目录下创建文件。示例1:在当前目录下创建文件touch math.txt执行结果如下:[root@VM-0-6-centos math]# touch math.txt [ro

1.1 文件创建命令(touch)

该命令的作用主要为创建文件,就类似于Windows系统中的新建功能,列如新建一个*.txt、*.md文件,该命令可以在当前目录下创建文件,也可以在指定目录下创建文件。

示例1:在当前目录下创建文件

touch math.txt

执行结果如下:

[root@VM-0-6-centos math]# touch math.txt[root@VM-0-6-centos math]# lsmath.txt

示例2:在指定目录下创建文件

touch english/english.md

执行结果如下:

[root@VM-0-6-centos subject]# ls english/english.md

在上面的示例中,我们是一次只创建了一个文件,我们也可以一次性创建多个文件,只需要文件名之间用空格隔开即可。

示例3:一次创建多个文件

touch xiaohong.txt xiaomin.txt xiaogang.txt

执行结果如下:

[root@VM-0-6-centos name]# touch xiaohong.txt xiaomin.txt xiaogang.txt[root@VM-0-6-centos name]# lsxiaogang.txt  xiaohong.txt  xiaomin.txt

1.2 移动或重命名(mv)

mv命令的作用有两个,一个是移动文件或文件夹,就类似于Windows系统的剪切功能,将一个文件从一个目录或文件移动到另一个目录,还有一个作用是对文件或目录进行重命名。

示例1:移动文件

mv xiaomin.txt name1

执行结果如下:

[root@VM-0-6-centos name]# mv xiaomin.txt name1[root@VM-0-6-centos name]# ls name1xiaomin.txt
[root@VM-0-6-centos name]#

从上面的运行结果我们得知,我们将name目录下的xiaomin.txt文件移动至name1的目录下。

示例2:重命名文件

mv xiaomin.txt xiaominpro.txt

执行结果如下:

[root@VM-0-6-centos name1]# mv xiaomin.txt xiaominpro.txt[root@VM-0-6-centos name1]# lsxiaominpro.txt

在使用mv命令操作文件的时候,如果你第二个参数是一个目录的话,那么此时mv的功能就是移动,如果第二个位置是一个文件的话,那么此时mv的功能就是重命名。mv不仅可以移动或重命名文件,也可以重命名或移动文件夹。

示例3:移动文件夹

 mv name1 name2

执行结果如下:

[root@VM-0-6-centos name]# lsname1  name2  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]# mv name1 name2[root@VM-0-6-centos name]# ls name2name1

示例4:重命名文件夹

 mv name2 name3

执行结果如下:

[root@VM-0-6-centos name]# lsname2  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]# mv name2 name3[root@VM-0-6-centos name]# lsname3  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]#

在使用mv命令操作目录的时候,第二个参数,如果目录是存在的,那么此时mv的作用就是移动,如果第二个参数的目录是不存在的,那么mv的作用就是重命名。

1.3 查看文件内容(cat)

该命令的作用主要是查看文件的内容,列如有一个文本文件,我们想查看它里面写了什么内容,此时就可以使用cat命令。

示例1:查看文件内容

cat flower.txt

执行结果如下:

[root@VM-0-6-centos name]# cat flower.txt阳春三月,风和日暖;信步城外,看阡陌之上杨柳依依,野花绚烂,身心不由得轻爽而浪漫。
漫步陌上,只因陌上花开;花是自然的那种,朴素而恬淡,不落尘俗。“三月风情陌上花”,是花在其中生命得以璀璨,人在其中心情得以畅然的一种意境。这意境,枝繁叶茂,从古代长到现代,不枯不衰;又如水,岁岁年年,流淌在阡陌之上,不知迷醉过古今几个王公贵族、粉黛佳丽、骚人墨客、凡男俗女,三月陌上花,让人爱让人痴,恍惚人的骨子里头都沉淀了花的影子,花的风韵。

1.4 文件复制命令(cp)

该命令的作用为复制文件,cp是英文copy的缩写。

示例1:复制文件到指定目录

cp flower.txt temp

执行结果如下:

[root@VM-0-6-centos name]# lsflower.txt  name3  temp  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]# cp flower.txt temp[root@VM-0-6-centos name]# ls temp/flower.txt

除了可以把文件复制到指定的目录,我们也可以将文件复制给另一个文件,但是会覆盖目标文件的内容,如果目标文件不存在,则会为其创建。

示例2:复制文件内容给指定文件呢

cp flower.txt flower1.txt

执行结果如下:

[root@VM-0-6-centos name]# lsflower.txt  name3  temp  xiaogang.txt  xiaohong.txt[root@VM-0-6-centos name]# cp flower.txt flower1.txt[root@VM-0-6-centos name]# lsflower1.txt  flower.txt  name3  temp  xiaogang.txt  xiaohong.txt[root@VM-0-6-centos name]#

1.5 压缩和解压命令(tar)

在Windows系统中我们一般都是通过解压软件来进行对文件或者文件的压缩和解压,在Linux系统中一般是通过tar解压缩命令来实现的,tar命令的参数如下:

Linux文件操作命令,常用命令有哪些?


在Windows系统中,一般常见的压缩包文件格式是*.zip、*.rar等,但是在Linux系统中压缩包的文件格式一般为*.tar.gz。在使用tar命令进行解压缩的时候,我们一般是将参数组合起来使用的。

示例1:将文件解压到当前目录

tar -zxvf nginx-1.18.0.tar.gz

执行结果如下:

[root@VM-0-6-centos name]# lsflower1.txt  flower.txt  name3  nginx-1.18.0.tar.gz  temp  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]# tar -zxvf nginx-1.18.0.tar.gznginx-1.18.0/
nginx-1.18.0/auto/
nginx-1.18.0/conf/
....
[root@VM-0-6-centos name]# lsflower1.txt  flower.txt  name3  nginx-1.18.0  nginx-1.18.0.tar.gz  temp  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]#

当我们解压完成后,会在当前目录下产生一个解压后的文件夹。

示例2:将文件解压到指定目录

tar -zxf nginx-1.18.0.tar.gz -C temp

执行结果如下:

[root@VM-0-6-centos name]# lsflower1.txt  flower.txt  name3  nginx-1.18.0.tar.gz  temp  xiaogang.txt  xiaohong.txt[root@VM-0-6-centos name]# tar -zxf nginx-1.18.0.tar.gz -C temp[root@VM-0-6-centos name]# ls tempflower.txt  nginx-1.18.0[root@VM-0-6-centos name]#

这里的-C表明 解压到指定的目录,我们将nginx-1.18.0.tar.gz解压到temp的目录下。

示例3:将文件夹压缩至当前目录下

tar -czvf name.tar.gz name

执行结果如下:

[root@VM-0-6-centos filedir]# tar -czvf name.tar.gz namename/
name/nginx-1.18.0.tar.gz
name/xiaohong.txt
name/tmp/name/tmp/nginx-1.21.1/name/tmp/nginx-1.21.1/nginx.exe
name/tmp/nginx-1.21.1/contrib/...
[root@VM-0-6-centos filedir]# lsa  filetemp  name  name.tar.gz  subject
[root@VM-0-6-centos filedir]#

我们来解释一下命令,name.tar.gz为你指定的压缩包名称和后缀名,name表明你要压缩那个目录。有的时候,我们只是想把文件快速打包一下,不想压缩,此时我们可以将-z选项去掉,缺点就是打包后的文件比较大,如果你想要压缩,那么就需要将-z加上,缺点就是压缩的速度慢。

示例4:对文件进行压缩

有的时候我们不想压缩整个目录,只想压缩单个或多个文件,这个时候我们只需要将上面示例中的文件夹目录换成文件即可,如果是多个文件,只需用空格分割开就行。

tar -czvf txt.tar.gz 1.txt 2.txt 3.txt

执行结果如下:

[root@VM-0-6-centos filedir]# ls1.txt  2.txt  3.txt  a  filetemp  name  name.tar.gz  subject[root@VM-0-6-centos filedir]# tar -czvf txt.tar.gz 1.txt 2.txt 3.txt1.txt2.txt3.txt[root@VM-0-6-centos filedir]# ls1.txt  2.txt  3.txt  a  filetemp  name  name.tar.gz  subject  txt.tar.gz[root@VM-0-6-centos filedir]#

1.6 解压zip格式文件(unzip)

在Linux系统中,一般情况压缩的文件后缀都是*.tar.gz,但是它也不是唯一,像Windows系统中的*.zip文件,在Linux系统中也是可以解压的,使用的命令是unzip。

示例1:解压zip文件至当前目录

unzip nginx-1.21.1.zip

执行结果如下:

[root@VM-0-6-centos name]# unzip nginx-1.21.1.zip Archive:  nginx-1.21.1.zip   creating: nginx-1.21.1/   creating: nginx-1.21.1/temp/  inflating: nginx-1.21.1/nginx.exe   creating: nginx-1.21.1/html/....[root@VM-0-6-centos name]# lsflower1.txt  flower.txt  name3  nginx-1.18.0.tar.gz  nginx-1.21.1  nginx-1.21.1.zip  temp  xiaogang.txt  xiaohong.txt

我们通过unzip命令,在命令后面直接输入要解压的文件名,即可解压。但是有些时候我们不想解压到当前目录,我们怎么办呢,此时我们可以将-d参数来指定要解压的位置。

示例2:解压zip文件至指定目录

unzip nginx-1.21.1.zip -d tmp

执行结果如下:

[root@VM-0-6-centos name]# lsnginx-1.18.0.tar.gz  nginx-1.21.1.zip  tmp  xiaogang.txt  xiaohong.txt
[root@VM-0-6-centos name]# unzip nginx-1.21.1.zip -d tmpArchive:  nginx-1.21.1.zip
   creating: tmp/nginx-1.21.1/
   creating: tmp/nginx-1.21.1/temp/
  inflating: tmp/nginx-1.21.1/nginx.exe
   creating: tmp/nginx-1.21.1/html/
  inflating: tmp/nginx-1.21.1/html/index.html
  inflating: tmp/nginx-1.21.1/html/50x.html
   creating: tmp/nginx-1.21.1/docs/[root@VM-0-6-centos name]# ls tmpnginx-1.21.1

通过-d选项将文件解压在tmp目录下。

示例3:压缩zip格式的文件

zip -r name.zip name

执行结果如下:

[root@VM-0-6-centos filedir]# zip -r name.zip name  adding: name/ (stored 0%)  adding: name/nginx-1.18.0.tar.gz (deflated 0%)  adding: name/xiaohong.txt (stored 0%)  adding: name/tmp/ (stored 0%)  adding: name/tmp/nginx-1.21.1/ (stored 0%)  adding: name/tmp/nginx-1.21.1/nginx.exe (deflated 59%)  adding: name/tmp/nginx-1.21.1/contrib/ (stored 0%)...[root@VM-0-6-centos filedir]# ls1.txt  2.txt  3.txt  a  filetemp  name  name.tar.gz  name.zip  subject  txt.tar.gz

值得注意的是-r的选项最好是放在前面,如果放在后面,可能会有问题。

1.7 查找文件命令(find)

在Windows系统中,我们查找文件一般是在文件管理器中的搜索框中进行搜索,在Linux系统中我们一般是使用find命令来查找我们所需的文件。

查找文件名以test开头的文件

find . -name 'test*'

执行结果如下:

[root@VM-0-6-centos test]# lstest1.txt  test2.txt  test3.txt  te.txt
[root@VM-0-6-centos test]# find . -name 'test*'./test1.txt
./test2.txt
./test3.txt
[root@VM-0-6-centos test]#

在命令中,.表明当前目录,即要查找的位置,-name 选项表明:我们通过文件的名称查找,后面的test* 表明我们要查找的文件名称,支持通配符,在当前的目录下有四个文件中,只有三个文件是以test开头的,通过命令我们可以查出符合条件的文件路径。

查找文件名以txt结尾的文件

find . -name '*.txt'

执行结果如下:

[root@VM-0-6-centos test]# lstest1.txt  test2.txt  test3.txt  test.md  te.txt
[root@VM-0-6-centos test]# find . -name '*.txt'./test1.txt
./test2.txt
./test3.txt
./te.txt
[root@VM-0-6-centos test]#

查找文件大小大于3K的文件有的时候,我们发现磁盘的空间不够了,这个时候我们想看看有哪些文件比较大,列如看看有哪些文件是大约1G的,这个时候我们也可以使用find命令把他们找到,把没有用的大文件删掉。为了演示我们这里暂且就查找3K的文件。

find . -type f -size +3k

执行结果如下:

[root@VM-0-6-centos nginx-1.21.1]# find . -type f -size +3k./conf/koi-utf
./conf/nginx.conf
./conf/koi-win
[root@VM-0-6-centos nginx-1.21.1]#

这里我们解释一下命令中参数的含义,find 表明使用查找命令,. 表明从当前目录开始查找,-type表明根据文件的类型来查,f表明只查文件,不查目录,-size表明文件大小,+号表明大于的意思,也可以省略不写,如果是-则表明小于,最后一个参数为查询的条件。

1.8 文件内容查找命令(grep)

我们在Windows的系统中,列如在word打开一个文档,当我们想去查看文档中,有没有我们关注的内容时,这个时候我们一般会使用Ctrl+F快捷键进行搜索,在Linux系统中我们可以使用grep命令。

示例1:查询文件内容中有linlang的行

grep linlang linlang.txt

执行结果如下:

[root@VM-0-6-centos ~]# cat linlang.txtlina
hallo
linlangya
make money
linlang
[root@VM-0-6-centos ~]# grep linlang linlang.txtlinlangya
linlang
[root@VM-0-6-centos ~]#

从上面的结果中,我们发现通过grep会查找出文档中满足条件的行。但是在实际的使用中,grep命令不会单独使用,一般都是与管道结合使用,管道在后面章节单独讲。

1.9 查找可执行文件路径(which)

这个命令的作用主要是查找可执行文件,所在的文件路径,列如我们在Linux中使用的命令,实则也是可执行文件,列如我们此时想看ls、cd这些命令所在的路径,此时就可以通过which来查找,该命令在实际的使用中,一般在shell脚本使用的比较多。

示例1:查看ls命令的位置

which ls

执行结果如下:

[root@VM-0-6-centos ~]# which lsalias ls='ls --color=auto'
        /bin/ls
[root@VM-0-6-centos ~]#

1.10 创建连接接命令(ln)

什么叫创建软链接命令呢,简单地说就是类似于Windows系统的快捷方式。示例1:为文件创建软连接

ln -s linlang.txt linlang_bak.txt

执行结果如下:

[root@iZ8vb15btme7rdyvwu43dyZ study]# lslinlang1  linlang2  linlang3  linlang.txt  plus[root@iZ8vb15btme7rdyvwu43dyZ study]# ln -s linlang.txt linlang_bak.txt[root@iZ8vb15btme7rdyvwu43dyZ study]# ll *.txtlrwxrwxrwx 1 root root 11 8月  15 11:31 linlang_bak.txt -> linlang.txt-rw-rwxrwx 1 root root  0 8月  14 16:41 linlang.txt[root@iZ8vb15btme7rdyvwu43dyZ study]#

命令中有有一个-s的参数,表明创建的是一个软连接,后面接着两个参数,第一个参数表明你要为那个文件创建快捷方式,第二个参数表明你创建的快捷方式叫什么。

结尾

本期的内容就到这里了,如有不足之处还请大家多多指正,欢迎大家留言、关注、转发、收藏,大家也可以关注我们的公众号「郑大钱呀」,一起学习交流,谢谢。

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】Spring Boot3 中实现按模板导出 Word 文档合同的技术指南(2025-10-30 16:04)
【系统环境|】openPangu-Ultra-MoE-718B-V1.1今日正式开源,部署指南来啦!(2025-10-30 16:03)
【系统环境|】Ubuntu + vLLM + DeepSeek 本地部署完全指南(2025-10-30 16:03)
【系统环境|】如何用公众号AI编辑器实现一键排版?一份完整的5步指南(2025-10-30 16:02)
【系统环境|】Spring Boot 与 Nacos 完美整合指南(2025-10-30 16:01)
【系统环境|】Rust MCP开发指南:让AI与应用对话的桥梁(2025-10-30 16:00)
【系统环境|】MCP Server 开发实战指南(2025-10-30 15:59)
【系统环境|】入门指南:使用 Playwright MCP Server 为你的 AI Agent 赋予能力(2025-10-30 15:58)
【系统环境|】一个IT女搬砖工的情人节爱心礼物指南及衍伸 v16.02.14(2025-10-30 15:57)
【系统环境|】百元矿渣显卡淘金全指南(2025-10-30 15:57)
手机二维码手机访问领取大礼包
返回顶部