2026/2/20 2:00:37
网站建设
项目流程
网站工程师平均工资,wordpress ssl 500,东莞网站搭建,开源saas多用户建站系统目录
前言#xff1a;
一、前置知识#xff1a;先搞懂 Linux 终端与命令格式
二、必学基础指令
2.1 定位当前位置#xff1a;pwd 指令
2.2 浏览目录内容#xff1a;ls 指令
2.3 切换工作目录#xff1a;cd 指令
2.4 创建空文件#xff1a;touch 指令
2.5 创建目录…目录前言一、前置知识先搞懂 Linux 终端与命令格式二、必学基础指令2.1 定位当前位置pwd 指令2.2 浏览目录内容ls 指令2.3 切换工作目录cd 指令2.4 创建空文件touch 指令2.5 创建目录mkdir 指令2.6 删除文件 / 目录rmdir rm 指令2.7 查看命令帮助man 指令2.8 复制文件 / 目录cp 指令前言Linux 指令是操作服务器、开发环境的 “基本功”比图形界面更高效、更通用。如果你是刚接触 Linux 的运维 / 开发新手想要快速掌握日常操作那本文覆盖 8 个高频基础指令附实操示例学完就能上手常用操作。一、前置知识先搞懂 Linux 终端与命令格式我们要先了解两个概念终端 / Shell是用户与 Linux 系统交互的文本界面如 XShell、系统自带终端命令基本格式命令 【选项】 【参数/选项】比如ls -l /home,ls是命令-l是选项/home是参数二、必学基础指令2.1 定位当前位置pwd 指令功能显示用户当前所在的工作目录路径常用场景不确定自己在哪个目录时快速查看位置实操实例[rootVM-0-12-centos ~]# pwd /rootLinux下的路径认识Linux的文件系统很像我们之前在数据结构中学习过的树状结构根目录/是树根父节点。这里有两个重要的概念绝对路径和相对路径。绝对路径就是从树根到你当前位置的完整路线。相对路径是以一个位置为参考位置来定位一个文件。2.2 浏览目录内容ls 指令功能对于目录该命令列出该目录下的所有子目录与问件。对于文件将列出文件名以及其他信息。常用选项- a 列出目录下的所有文件包括以 . 开头的隐含文件。- d 将目录像文件一样显示而不是显示其下的文件。如ls -d 指定目录- i 输出文件的 i 节点的索引信息。如 ls -ai 指定文件- k 以 k 字节的形式表示文件的大小。ls -alk 指定文件- l 列出文件的详细信息- n 用数字的 UID,GID 代替名称。介绍 UIDGID- F 在每个文件名后附上一个字符以说明该文件的类型“*” 表示可执行的普通文件“/” 表示目录“” 表示符号链接“|” 表示 FIFOs“” 表示套接字(sockets)。目录类型识别- r 对目录反向排序- t 以时间排序- s 在l文件名后输出该文件的大小。大小排序如何找到目录下最大的文件- R 列出所有子目录下的文件。(递归)- 1 一行只输出一个文件。实操实例[rootVM-0-12-centos ~]# ls mian.c mydir test.c [rootVM-0-12-centos ~]# ls -l total 8 drwxr-xr-x 3 root root 4096 Dec 16 10:25 mian.c drwxr-xr-x 2 root root 4096 Dec 15 20:45 mydir -rw-r--r-- 1 root root 0 Dec 15 21:01 test.c [rootVM-0-12-centos ~]# ls -la total 68 dr-xr-x---. 8 root root 4096 Dec 16 10:33 . dr-xr-xr-x. 20 root root 4096 Dec 16 16:26 .. -rw------- 1 root root 1570 Dec 16 16:26 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc drwxr-xr-x 4 root root 4096 Dec 8 05:08 .cache drwxr-xr-x 3 root root 4096 Mar 7 2019 .config -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwxr-xr-x 3 root root 4096 Dec 16 10:25 mian.c drwxr-xr-x 2 root root 4096 Dec 15 20:45 mydir -rw-r--r-- 1 root root 44 Nov 28 22:54 .npmrc drwxr-xr-x 2 root root 4096 Nov 28 22:53 .pip -rw-r--r-- 1 root root 73 Nov 28 22:53 .pydistutils.cfg drwx------ 2 root root 4096 Nov 5 2019 .ssh -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc -rw-r--r-- 1 root root 0 Dec 15 21:01 test.c -rw------- 1 root root 771 Dec 16 10:33 .viminfo我们注意在Linux中就上述的实例中以d开头的都是目录以-开头的是普通文件而且以 . 开头的文件是隐藏文件就我们对于电脑的常识可以知道目录就是一个文件夹。然后对于任意一个目录而言即使是空目录系统都默认自带 . 和 .. 目录. 目录是当前目录 .. 目录是上级目录。2.3 切换工作目录cd 指令功能改变工作目录将当前工作目录改变到指定目录下常用场景cd (目录路径)切换到指定目录绝对路径 / 相对路径均可cd ~快速回到当前用户的家目录cd ..回到上级目录cd -回到上一次所在的目录实操实例[rootVM-0-12-centos ~]# cd mydir [rootVM-0-12-centos mydir]# cd a [rootVM-0-12-centos a]# pwd /root/mydir/a [rootVM-0-12-centos a]# cd .. [rootVM-0-12-centos mydir]# pwd /root/mydir [rootVM-0-12-centos mydir]# cd - /root/mydir/a [rootVM-0-12-centos a]# cd ~ [rootVM-0-12-centos ~]# pwd /root2.4 创建空文件touch 指令功能touch命令参数可更改文档或目录的日期时间包括存取时间和更改时间或者新建一个不存在的文件。实操实例[rootVM-0-12-centos mytest]# touch test1 [rootVM-0-12-centos mytest]# cd test1 -bash: cd: test1: Not a directory [rootVM-0-12-centos mytest]# stat test1 File: ‘test1’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd01h/64769d Inode: 786464 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2025-12-16 17:58:11.828371058 0800 Modify: 2025-12-16 17:58:11.828371058 0800 Change: 2025-12-16 17:58:11.828371058 0800 Birth: -这些信息我们可以用stat命令查看到1.Access访问时间含义文件最后一次被读取 / 访问的时间仅读取内容不修改。触发场景用cat/less查看文件、执行可执行文件、播放音视频等。查看方式ls -lu或stat 文件名。2.Modify修改时间含义文件内容最后一次被修改的时间仅针对内容变化。触发场景用vim编辑并保存、echo 内容 文件追加数据等。注意修改内容会同时更新Access和Change时间。查看方式ls -l或stat 文件名。3.Change改变时间含义文件元数据属性最后一次被修改的时间如权限、所有者、文件名等。触发场景chmod改权限、chown改所有者、mv重命名等。注意修改内容也会间接更新此时间因为文件大小等元数据会变化。查看方式ls -lc或stat 文件名。2.5 创建目录mkdir 指令功能创建新目录常用参数-p递归创建多级目录实操实例[rootVM-0-12-centos ~]# mkdir mytest [rootVM-0-12-centos ~]# cd mytest [rootVM-0-12-centos mytest]# pwd /root/mytest [rootVM-0-12-centos mytest]# mkdir a/b/c/d mkdir: cannot create directory ‘a/b/c/d’: No such file or directory [rootVM-0-12-centos mytest]# mkdir -p a/b/c/d [rootVM-0-12-centos mytest]# ls -l total 4 drwxr-xr-x 3 root root 4096 Dec 16 17:49 a [rootVM-0-12-centos mytest]# ls -la total 12 drwxr-xr-x 3 root root 4096 Dec 16 17:49 . dr-xr-x---. 9 root root 4096 Dec 16 17:48 .. drwxr-xr-x 3 root root 4096 Dec 16 17:49 a [rootVM-0-12-centos mytest]# tree a a -- b -- c -- d 3 directories, 0 files我们也要注意tree命令是一个递归以树状结构显示指定目录的命令可能需要安装才能使用安装命令如果是root用户使用yum install -y tree进行安装。2.6 删除文件 / 目录rmdir rm 指令rmdir仅能删除空目录rm功能更强大可删除文件 / 非空目录注意Linux 中rm删除的内容无法恢复常用参数-f强制删除不提示-r:递归删除用于删除目录包含目录内的所有内容谨慎使用删除实操实例这里是用别人的代码直接给大家演示了。# 删除普通⽂件 [whbbite-alicloud test]$ ll total 8 drwxrwxr-x 2 whb whb 4096 Jan 11 14:22 dir -rw-rw-r-- 1 whb whb 0 Jan 11 14:22 file.txt -rw-rw-r-- 1 whb whb 0 Jan 11 15:09 newFile.txt drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 [whbbite-alicloud test]$ rm file.txt [whbbite-alicloud test]$ ll total 8 drwxrwxr-x 2 whb whb 4096 Jan 11 14:22 dir -rw-rw-r-- 1 whb whb 0 Jan 11 15:09 newFile.txt drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 # 删除⽬录⽂件 [whbbite-alicloud test]$ ll total 8 drwxrwxr-x 2 whb whb 4096 Jan 11 14:22 dir -rw-rw-r-- 1 whb whb 0 Jan 11 15:09 newFile.txt drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 [whbbite-alicloud test]$ rm dir rm: cannot remove ‘dir’: Is a directory [whbbite-alicloud test]$ rm -r dir [whbbite-alicloud test]$ ll total 4 -rw-rw-r-- 1 whb whb 0 Jan 11 15:09 newFile.txt drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 # 删除普通⽂件前询问 [whbbite-alicloud test]$ ll total 4 -rw-rw-r-- 1 whb whb 0 Jan 11 15:09 newFile.txt drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 [whbbite-alicloud test]$ rm -i newFile.txt rm: remove regular empty file ‘newFile.txt’? y [whbbite-alicloud test]$ ll total 4 drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 [whbbite-alicloud test]$ ll total 4 drwxrwxr-x 3 whb whb 4096 Jan 11 15:26 path1 # 删除⽬录前询问 [whbbite-alicloud test]$ mkdir -p d/d/d/d [whbbite-alicloud test]$ tree d d └── d └── d └── d 3 directories, 0 files [whbbite-alicloud test]$ rm -ri d rm: descend into directory ‘d’? y rm: descend into directory ‘d/d’? y rm: descend into directory ‘d/d/d’? y rm: remove directory ‘d/d/d/d’? y rm: remove directory ‘d/d/d’? y rm: remove directory ‘d/d’? y rm: remove directory ‘d’? y [whbbite-alicloud test]$ ll total 0 # 递归强制删除⾮空⽬录 [whbbite-alicloud test]$ tree path1/ path1/ ├── myfile.txt └── path2 └── myfile.txt 1 directory, 2 files [whbbite-alicloud test]$ rm -f path1 rm: cannot remove ‘path1’: Is a directory [whbbite-alicloud test]$ rm -rf path1 [whbbite-alicloud test]$ ll total 02.7 查看命令帮助man 指令功能查看Manual命令的官方帮助文档包含功能、参数、示例常用场景忘记指令参数时快速查询提示按q键退出帮助文档实操实例[rootVM-0-12-centos mytest]# man man [rootVM-0-12-centos mytest]# man ls2.8 复制文件 / 目录cp 指令功能复制文件或目录常用选项- f 或 -force强行复制文件或目录不管目标文件/目录是否已存在-i或 -interactive覆盖文件前先询问用户-r递归处理把指定目录下的文件和子目录一起处理若源不是目录或符号链接就当作普通文件处理实操实例[rootVM-0-12-centos mytest]# pwd /root/mytest [rootVM-0-12-centos mytest]# ls -la total 12 drwxr-xr-x 3 root root 4096 Dec 16 17:58 . dr-xr-x---. 9 root root 4096 Dec 16 19:08 .. drwxr-xr-x 3 root root 4096 Dec 16 17:49 a -rw-r--r-- 1 root root 0 Dec 16 17:58 test1 [rootVM-0-12-centos mytest]# cp test1 test2 [rootVM-0-12-centos mytest]# ls -la total 12 drwxr-xr-x 3 root root 4096 Dec 16 19:15 . dr-xr-x---. 9 root root 4096 Dec 16 19:08 .. drwxr-xr-x 3 root root 4096 Dec 16 17:49 a -rw-r--r-- 1 root root 0 Dec 16 17:58 test1 -rw-r--r-- 1 root root 0 Dec 16 19:15 test2