Linux基础指令完全指南:从入门到熟练

Linux作为现代计算的核心操作系统之一,广泛应用于服务器、云计算、嵌入式设备及开发环境。掌握Linux基础指令是每个开发者、运维工程师和技术爱好者的必备技能。

无论你是Linux初学者,还是希望巩固命令行操作的进阶用户,本文将带你系统学习最常用、最实用的Linux基础指令,涵盖:

  • 文件与目录操作(lscdmkdircpmvrm...)

  • 文本查看与编辑(catlessnanovim...)

  • 权限管理(chmodchownsudo...)

  • 进程与系统监控(pstophtopkill...)

  • 网络与工具(pingcurlsshscp...)

通过实例演示+场景解析,你将快速掌握Linux命令行的核心操作逻辑,提升工作效率。

为什么学习Linux指令?

  1. 高效管理服务器:Linux是Web服务器(如Nginx/Apache)、数据库(MySQL/MongoDB)的首选OS。

  2. 自动化运维:通过脚本(Bash/Python)结合指令实现任务自动化。

  3. 开发必备:Git、Docker、Kubernetes等工具深度依赖Linux环境。

  4. 求职竞争力:运维、DevOps、后端开发等岗位的常见技术要求。

ls指令

语法: ls [选项] [⽬录或⽂件]
功能:对于⽬录,该命令列出该⽬录下的所有⼦⽬录与⽂件。对于⽂件,将列出⽂件名以及其他信
息。
常⽤选项:

• -a 列出⽬录下的所有⽂件,包括以 . 开头的隐含⽂件。
• -d 将⽬录像⽂件⼀样显⽰,⽽不是显⽰其下的⽂件。 如:ls ‒d 指定⽬录
• -i 输出⽂件的 i 节点的索引信息。 如 ls ‒ai 指定⽂件
• -k 以 k 字节的形式表⽰⽂件的⼤⼩。ls ‒alk 指定⽂件
• -l 列出⽂件的详细信息
比特就业课
• -n ⽤数字的 UID,GID 代替名称。 (介绍 UID, GID)
• -F 在每个⽂件名后附上⼀个字符以说明该⽂件的类型,“*”表⽰可执⾏的普通⽂件;“/”表⽰
⽬录;“@”表⽰符号链接;“|”表⽰FIFOs;“=”表⽰套接字(sockets)。(⽬录类型识别)
• -r 对⽬录反向排序
• -t 以时间排序
• -s 在l⽂件名后输出该⽂件的⼤⼩。(⼤⼩排序,如何找到⽬录下最⼤的⽂件)
• -R 列出所有⼦⽬录下的⽂件。(递归)
• -l⼀⾏只输出⼀个⽂件。

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# ls
dir  test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# 
root@lijunhua-virtual-machine:/home/lijunhua/study# la
dir  test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# 
root@lijunhua-virtual-machine:/home/lijunhua/study# ls
dir  test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ls -a
.  ..  dir  test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ls -a -l
total 12
drwxr-xr-x  3 root     root     4096  4月 16 23:22 .
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ..
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ls -l -a -F
total 12
drwxr-xr-x  3 root     root     4096  4月 16 23:22 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ../
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir/
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ls -laFn
total 12
drwxr-xr-x  3    0    0 4096  4月 16 23:22 ./
drwxr-x--- 16 1000 1000 4096  4月 16 23:21 ../
drwxr-xr-x  2    0    0 4096  4月 16 23:21 dir/
-rw-r--r--  1    0    0    0  4月 16 23:22 test.txt

pwd 命令

语法: pwd
功能:显⽰⽤⼾当前所在的⽬录
举例:
root@lijunhua-virtual-machine:/home/lijunhua/study# pwd
/home/lijunhua/study

cd 指令

语法: cd ⽬录名
功能:改变⼯作⽬录。将当前⼯作⽬录改变到指定的⽬录下
​​​​​​​cd /path/to/directory   # 进入指定目录
cd ..                   # 返回上一级目录
cd ~                    # 进入用户的主目录
举例:
root@lijunhua-virtual-machine:/home/lijunhua# pwd
/home/lijunhua
root@lijunhua-virtual-machine:/home/lijunhua# cd study
root@lijunhua-virtual-machine:/home/lijunhua/study# pwd
/home/lijunhua/study
root@lijunhua-virtual-machine:/home/lijunhua/study# cd dir
root@lijunhua-virtual-machine:/home/lijunhua/study/dir# pwd
/home/lijunhua/study/dir
root@lijunhua-virtual-machine:/home/lijunhua/study/dir# cd ./ //到现在的目录
root@lijunhua-virtual-machine:/home/lijunhua/study/dir# pwd  
/home/lijunhua/study/dir
root@lijunhua-virtual-machine:/home/lijunhua/study/dir# cd ../ //回到上级目录
root@lijunhua-virtual-machine:/home/lijunhua/study# pwd
/home/lijunhua/study

touch 指令

语法: touch [选项]... ⽂件...
功能:touch命令参数可更改⽂档或⽬录的⽇期时间,包括存取时间和更改时间,或者新建⼀个不存在的⽂件。
常⽤选项:
• -a : change only the access time
• -c : change only the modification time

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# ls
d1  dir  test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# touch file.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ls
d1  dir  file.txt  test.txt

//显示文件时间
root@lijunhua-virtual-machine:/home/lijunhua/study# stat file.txt
  File: file.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d	Inode: 655622      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-04-16 23:45:13.179486977 +0800
Modify: 2025-04-16 23:45:13.179486977 +0800
Change: 2025-04-16 23:45:13.179486977 +0800
 Birth: 2025-04-16 23:45:13.179486977 +0800

//修改文件access时间
root@lijunhua-virtual-machine:/home/lijunhua/study# touch -a file.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# stat file.txt
  File: file.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d	Inode: 655622      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-04-16 23:46:13.089939437 +0800
Modify: 2025-04-16 23:45:13.179486977 +0800
Change: 2025-04-16 23:46:13.089939437 +0800
 Birth: 2025-04-16 23:45:13.179486977 +0800

//修改文件modify时间
root@lijunhua-virtual-machine:/home/lijunhua/study# touch -m file.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# stat file.txt
  File: file.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d	Inode: 655622      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-04-16 23:46:13.089939437 +0800
Modify: 2025-04-16 23:46:59.920739851 +0800
Change: 2025-04-16 23:46:59.920739851 +0800
 Birth: 2025-04-16 23:45:13.179486977 +0800

mkdir 指令

语法mkdir [选项] dirname...
功能:在当前⽬录下创建⼀个名为 “dirname”的⽬录
常⽤选项:
• -p/--parents: 可以是⼀个路径名称。此时若路径中的某些⽬录尚不存在,加上此选项后,系统将⾃动建⽴好那些尚不存在的⽬录,即⼀次可以建⽴多个⽬录
举例:
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 12
drwxr-xr-x  3 root     root     4096  4月 16 23:50 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ../
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir/
-rw-r--r--  1 root     root        0  4月 16 23:46 file.txt
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# mkdir mydir
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 16
drwxr-xr-x  4 root     root     4096  4月 16 23:51 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ../
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir/
-rw-r--r--  1 root     root        0  4月 16 23:46 file.txt
drwxr-xr-x  2 root     root     4096  4月 16 23:51 mydir/
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt

//递归创建目录
root@lijunhua-virtual-machine:/home/lijunhua/study# mkdir -p path1/path2/path3/path4

//tree是展示目录树状结构(系统没有的话可以使用apt -y install tree(ubunto))
root@lijunhua-virtual-machine:/home/lijunhua/study# tree .
.
├── dir
├── file.txt
├── mydir
├── path1
│   └── path2
│       └── path3
│           └── path4
└── test.txt

6 directories, 2 files

rmdir 指令 && rm 指令

rmdir是⼀个与mkdir相对应的命令,mkdir是建⽴⽬录,⽽rmdir是删除命令
语法rmdir [-p] [dirName]
适⽤对象:具有当前⽬录操作权限的所有使⽤者
功能:删除空⽬录
常用选项:
-p 当⼦⽬录被删除后如果⽗⽬录也变成空⽬录的话,就连带⽗⽬录⼀起删除。

 举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 20
drwxr-xr-x  5 root     root     4096  4月 16 23:52 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ../
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir/
-rw-r--r--  1 root     root        0  4月 16 23:46 file.txt
drwxr-xr-x  2 root     root     4096  4月 16 23:51 mydir/
drwxr-xr-x  3 root     root     4096  4月 16 23:52 path1/
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# tree mydir
mydir

0 directories, 0 files
root@lijunhua-virtual-machine:/home/lijunhua/study# rmdir mydir
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 16
drwxr-xr-x  4 root     root     4096  4月 16 23:56 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ../
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir/
-rw-r--r--  1 root     root        0  4月 16 23:46 file.txt
drwxr-xr-x  3 root     root     4096  4月 16 23:52 path1/
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# tree path1
path1
└── path2
    └── path3
        └── path4

3 directories, 0 files
root@lijunhua-virtual-machine:/home/lijunhua/study# rmdir -p path1/path2/path3/path4
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 12
drwxr-xr-x  3 root     root     4096  4月 17 00:00 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 16 23:21 ../
drwxr-xr-x  2 root     root     4096  4月 16 23:21 dir/
-rw-r--r--  1 root     root        0  4月 16 23:46 file.txt
-rw-r--r--  1 root     root        0  4月 16 23:22 test.txt

# 指定路径中有不为空的路径,便⽆法删除
root@lijunhua-virtual-machine:/home/lijunhua/study# touch path1/path2/myfile.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# tree path1/
path1/
├── myfile.txt
└── path2
    ├── myfile.txt
    └── path3
        └── path4

3 directories, 2 files
root@lijunhua-virtual-machine:/home/lijunhua/study# rmdir -p path1/path2/path3/path4
rmdir: failed to remove directory 'path1/path2': Directory not empty
rm 命令可以同时删除⽂件或⽬录
语法rm [-f-i-r-v] [dirName/dir]
适⽤对象:所有使⽤者
功能:删除⽂件或⽬录
常⽤选项:
• -f 即使⽂件属性为只读(即写保护),亦直接删除
• -i 删除前逐⼀询问确认
• -r 删除⽬录及其下所有⽂件

 举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# ls
dir  file.txt  path1  test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── dir
├── file.txt
├── path1
│   └── path2
│       └── path3
│           └── path4
└── test.txt

5 directories, 2 files
root@lijunhua-virtual-machine:/home/lijunhua/study# rm file.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 16
drwxr-xr-x  4 root     root     4096  4月 17 18:54 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
drwxr-xr-x  2 root     root     4096  4月 17 18:52 dir/
drwxr-xr-x  3 root     root     4096  4月 17 18:53 path1/
-rw-r--r--  1 root     root        0  4月 17 18:52 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# rm dir
rm: cannot remove 'dir': Is a directory
root@lijunhua-virtual-machine:/home/lijunhua/study# rm -r dir
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 12
drwxr-xr-x  3 root     root     4096  4月 17 18:54 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
drwxr-xr-x  3 root     root     4096  4月 17 18:53 path1/
-rw-r--r--  1 root     root        0  4月 17 18:52 test.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# rm -i test.txt
rm: remove regular empty file 'test.txt'? y
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 12
drwxr-xr-x  3 root     root     4096  4月 17 18:55 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
drwxr-xr-x  3 root     root     4096  4月 17 18:53 path1/
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
└── path1
    └── path2
        └── path3
            └── path4

4 directories, 0 files
root@lijunhua-virtual-machine:/home/lijunhua/study# rm -r path1
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 8
drwxr-xr-x  2 root     root     4096  4月 17 18:55 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
root@lijunhua-virtual-machine:/home/lijunhua/study# 

man 指令

Linux的命令有很多参数,我们不可能全记住,可以通过查看联机⼿册获取帮助
语法: man [选项] 命令
常⽤选项:
• -k 根据关键字搜索联机帮助
• num 只在第num章节查找
• -a 将所有章节的都显⽰出来,⽐如 man printf 它缺省从第⼀章开始搜索,知道就停⽌,⽤a选项,当按下q退出,他会继续往后⾯搜索,直到所有章节都搜索完毕

 解释:man⼿册分为9章(不同系统可能会有差别)

1 是普通的命令
2 是系统调⽤,如open,write之类的(通过这个,⾄少可以很⽅便的查到调⽤这个函数,需要加什么头⽂件)
3 是库函数,如printf,fread4是特殊⽂件,也就是/dev下的各种设备⽂件
4 略
5 是指⽂件的格式,⽐如passwd, 就会说明这个⽂件中各个字段的含义
6 是给游戏留的,由各个游戏⾃⼰定义
7 是附件还有⼀些变量,⽐如像environ这种全局变量在这⾥就有说明
8 是系统管理⽤的命令,这些命令只能由root使⽤,如ifconfig
9 略

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# man printf

 

cp 指令

语法cp [选项] 源⽂件或⽬录 ⽬标⽂件或⽬录
功能: 复制⽂件或⽬录
说明:
cp指令⽤于复制⽂件或⽬录
• 如同时指定两个以上的⽂件或⽬录,且最后的⽬的地是⼀个已经存在的⽬录,则它会把前⾯指定的所有⽂件或⽬录复制到此⽬录中

常用选项:

• -f 或 --force 强⾏复制⽂件或⽬录, 不论⽬的⽂件或⽬录是否已经存在
• -i 或 --interactive 覆盖⽂件之前先询问⽤⼾
• -r 递归处理,将指定⽬录下的⽂件与⼦⽬录⼀并处理。若源⽂件或⽬录的形态,不属于⽬录或符号链接,则⼀律视为普通⽂件处理

 举例:

//输入重定向
root@lijunhua-virtual-machine:/home/lijunhua/study# echo "hello world">file.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 12
drwxr-xr-x  2 root     root     4096  4月 17 19:50 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
-rw-r--r--  1 root     root       12  4月 17 19:50 file.txt
//显示文件内容
root@lijunhua-virtual-machine:/home/lijunhua/study# cat file.txt
hello world
root@lijunhua-virtual-machine:/home/lijunhua/study# cp file.txt myfile.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 16
drwxr-xr-x  2 root     root     4096  4月 17 19:55 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
-rw-r--r--  1 root     root       12  4月 17 19:50 file.txt
-rw-r--r--  1 root     root       12  4月 17 19:55 myfile.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# cat myfile.txt
hello world
root@lijunhua-virtual-machine:/home/lijunhua/study# mkdir dir
root@lijunhua-virtual-machine:/home/lijunhua/study# cp *.txt dir
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── dir
│   ├── file.txt
│   └── myfile.txt
├── file.txt
└── myfile.txt

1 directory, 4 files
root@lijunhua-virtual-machine:/home/lijunhua/study# echo "i will replace you">myfile.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# cp myfile.txt file.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# cat file.txt
i will replace you
//强制递归目录
root@lijunhua-virtual-machine:/home/lijunhua/study# cp -rf dir test
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── dir
│   ├── file.txt
│   └── myfile.txt
├── file.txt
├── myfile.txt
└── test
    ├── dir
    │   ├── file.txt
    │   └── myfile.txt
    ├── file.txt
    └── myfile.txt

3 directories, 8 files

mv 指令

mv命令是move的缩写,可以⽤来移动⽂件或者将⽂件改名(move (rename) files,经常⽤来备份⽂件或者⽬录
语法: mv [选项] 源⽂件或⽬录 ⽬标⽂件或⽬录
功能:
1. 视mv命令中第⼆个参数类型的不同(是⽬标⽂件还是⽬标⽬录),mv命令将⽂件重命名或将其移⾄⼀个新的⽬录中。
2. 当第⼆个参数类型是⽂件时,mv命令完成⽂件重命名,此时,源⽂件只能有⼀个(也可以是源⽬录名),它将所给的源⽂件或⽬录重命名为给定的⽬标⽂件名。
3. 当第⼆个参数是已存在的⽬录名称时,源⽂件或⽬录参数可以有多个,mv命令将各参数指定的源⽂件均移⾄⽬标⽬录中。

 常⽤选项:

-f :force 强制的意思,如果⽬标⽂件已经存在,不会询问⽽直接覆盖
-i :若⽬标⽂件 (destination) 已经存在时,就会询问是否覆盖!

 举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# mv myfile.txt yourfile.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 24
drwxr-xr-x  4 root     root     4096  4月 17 20:20 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
drwxr-xr-x  2 root     root     4096  4月 17 19:57 dir/
-rw-r--r--  1 root     root       19  4月 17 19:58 file.txt
drwxr-xr-x  3 root     root     4096  4月 17 20:00 test/
-rw-r--r--  1 root     root       19  4月 17 19:58 yourfile.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# mv yourfile.txt temp
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 24
drwxr-xr-x  4 root     root     4096  4月 17 20:24 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 17 18:49 ../
drwxr-xr-x  2 root     root     4096  4月 17 19:57 dir/
-rw-r--r--  1 root     root       19  4月 17 19:58 file.txt
-rw-r--r--  1 root     root       19  4月 17 19:58 temp
drwxr-xr-x  3 root     root     4096  4月 17 20:00 test/
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── dir
│   ├── file.txt
│   └── myfile.txt
├── file.txt
├── temp
└── test
    ├── dir
    │   ├── file.txt
    │   └── myfile.txt
    ├── file.txt
    └── myfile.txt

3 directories, 8 files

cat 指令

语法cat [选项] [⽂件]
功能: 查看⽬标⽂件的内容
常⽤选项:
-b 对⾮空输出⾏编号
-n 对输出的所有⾏编号
-s 不输出多⾏空⾏
root@lijunhua-virtual-machine:/home/lijunhua/study# cnt=0; while [ $cnt -le 10 ]; do echo "hello world"; let cnt++; done > temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# cat temp.txt
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
root@lijunhua-virtual-machine:/home/lijunhua/study# cat -b temp.txt
     1	hello world
     2	hello world
     3	hello world
     4	hello world
     5	hello world
     6	hello world
     7	hello world
     8	hello world
     9	hello world
    10	hello world
    11	hello world
root@lijunhua-virtual-machine:/home/lijunhua/study# echo " ">>temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# echo " ">>temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# echo " ">>temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# echo "aaaaaaaa">>temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# echo "aaaaaaaa">>temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# cat temp.txt
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
 
 
 
aaaaaaaa
aaaaaaaa
root@lijunhua-virtual-machine:/home/lijunhua/study# cat -s temp.txt
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
 
aaaaaaaa
aaaaaaaa

more 指令

语法more [选项]
功能:more命令,功能类似 cat
常用选项:
-n 指定输出⾏数
q 退出more

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# more -11 temp.txt
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say

less 指令

less ⼯具也是对⽂件或其它输出进⾏分⻚显⽰的⼯具,应该说是linux正统查看⽂件内容的⼯具,功能极其强⼤
less 的⽤法⽐起 more 更加的有弹性,在 more 的时候,我们并没有办法向前⾯翻, 只能往后⾯看
但若使⽤了 less 时,就可以使⽤ [pageup] [pagedown] 等按键的功能来往前往后翻看⽂件,更容易⽤来查看⼀个⽂件的内容
除此之外,在 less ⾥头可以拥有更多的搜索功能,不⽌可以向下搜,也可以向上搜。
语法less [参数] ⽂件
功能:less与more类似,但使⽤less可以随意浏览⽂件,⽽more仅能向前移动,却不能向后移动,⽽且less在查看之前不会加载整个⽂件。

选项

• -i 忽略搜索时的⼤⼩写
• -N 显⽰每⾏的⾏号
• /字符串:向下搜索“字符串”的功能
• ?字符串:向上搜索“字符串”的功能
• n:重复前⼀个搜索(与 / 或 ? 有关)
• N:反向重复前⼀个搜索(与 / 或 ? 有关)
• q: 退出

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# less -N temp.txt
      1 what can i say
      2 what can i say
      3 what can i say
      4 what can i say
      5 what can i say
      6 what can i say
      7 what can i say
      8 what can i say
      9 what can i say
     10 what can i say
     11 what can i say
     12 what can i say
     13 what can i say
     14 what can i say
     15 what can i say
     16 what can i say
     17 what can i say
     18 what can i say
     19 what can i say
     20 what can i say
     21 what can i say
/what

head 指令

head 与 tail 就像它的名字⼀样的浅显易懂,它是⽤来显⽰开头或结尾某个数量的⽂字区块,head ⽤来显⽰档案的开头⾄标准输出中,⽽ tail 想当然尔就是看档案的结尾。
语法: head [参数]... [⽂件]...
功能
head ⽤来显⽰档案的开头⾄标准输出中,默认head命令打印其相应⽂件的开头10⾏。
选项
-n<⾏数> 显⽰的⾏数

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# head temp.txt
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
root@lijunhua-virtual-machine:/home/lijunhua/study# head -13 temp.txt
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say
what can i say

tail 指令

tail 命令从指定点开始将⽂件写到标准输出.使⽤tail命令的-f选项可以⽅便的查阅正在改变的⽇志⽂
件,tail -f filename会把filename⾥最尾部的内容显⽰在屏幕上,并且不断刷新,使你看到最新的⽂件内容.
语法tail 必要参数 [⽂件]
功能:⽤于显⽰指定⽂件末尾内容,不指定⽂件时,作为输⼊信息进⾏处理。常⽤查看⽇志⽂件。
选项
-f 循环读取
-n<⾏数> 显⽰⾏数

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# cnt=0; while [ $cnt -le 2000 ]; do echo "hello$cnt"; let cnt++; done > temp.txt
root@lijunhua-virtual-machine:/home/lijunhua/study# tail temp.txt
hello1991
hello1992
hello1993
hello1994
hello1995
hello1996
hello1997
hello1998
hello1999
hello2000
root@lijunhua-virtual-machine:/home/lijunhua/study# tail -3 temp.txt
hello1998
hello1999
hello2000
//使用管道展示中间内容
root@lijunhua-virtual-machine:/home/lijunhua/study# head -100 temp.txt |tail -20
hello80
hello81
hello82
hello83
hello84
hello85
hello86
hello87
hello88
hello89
hello90
hello91
hello92
hello93
hello94
hello95
hello96
hello97
hello98
hello99

date 指令

指定格式显⽰时间: date +%Y:%m:%d

⽤法date [OPTION]... [+FORMAT]

1. 在显⽰⽅⾯,使⽤者可以设定欲显⽰的格式,格式设定为⼀个加号后接数个标记,其中常⽤的标记列表如下

• %H : ⼩时(00..23)
• %M : 分钟(00..59)
• %S : 秒(00..61)

• %X : 相当于 %H:%M:%S
• %d : ⽇ (01..31)
• %m : ⽉份 (01..12)
• %Y : 完整年份 (0000..9999)
• %F : 相当于 %Y-%m-%d

2. 在设定时间⽅⾯
• date -s //设置当前时间,只有root权限才能设置,其他只能查看。
• date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
• date -s 01:01:01 //设置具体时间,不会对⽇期做更改
• date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
• date -s “01:01:01 20080523″ //这样可以设置全部时间
• date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
• date -s “20080523 01:01:01″ //这样可以设置全部时间
3. 时间戳
• 时间->时间戳:date +%s
• 时间戳->时间:date -d@1508749502
• Unix时间戳(英⽂为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1⽉1⽇(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# date
2025年 04月 19日 星期六 00:42:21 CST
root@lijunhua-virtual-machine:/home/lijunhua/study# date +%Y/%M/%d
2025/43/19
root@lijunhua-virtual-machine:/home/lijunhua/study# date +%Y/%M/%d--%H:%M:%S
2025/43/19--00:43:31
root@lijunhua-virtual-machine:/home/lijunhua/study# date +%s
1744994626
root@lijunhua-virtual-machine:/home/lijunhua/study# date +%Y/%M/%d--%H:%M:%S -d @0
1970/00/01--08:00:00
root@lijunhua-virtual-machine:/home/lijunhua/study# date +%Y/%M/%d--%H:%M:%S -d @10086
1970/48/01--10:48:06

find 指令

语法find pathname -options
功能:⽤于在⽂件树中查找⽂件,并作出相应的处理(可能访问磁盘)
常用选项
-name 按照⽂件名查找⽂件
其他选项需要在查,这个命令其实⽐较复杂

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── temp.txt
└── test
    ├── dir
    │   ├── file.txt
    │   └── myfile.txt
    ├── file.txt
    └── myfile.txt

2 directories, 5 files
root@lijunhua-virtual-machine:/home/lijunhua/study# find -name file.txt
./test/file.txt
./test/dir/file.txt

which 指令

功能:搜索系统指定的命令
举例:
root@lijunhua-virtual-machine:/home/lijunhua/study# which ls
/usr/bin/ls
root@lijunhua-virtual-machine:/home/lijunhua/study# which pwd
/usr/bin/pwd

whereis 指令

功能:⽤于找到程序的源、⼆进制⽂件或⼿册
举例:
root@lijunhua-virtual-machine:/home/lijunhua/study# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
root@lijunhua-virtual-machine:/home/lijunhua/study# whereis pwd
pwd: /usr/bin/pwd /usr/share/man/man1/pwd.1.gz

alias 指令

功能:设置命令的别名

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# alias hello='ls -a -n'
root@lijunhua-virtual-machine:/home/lijunhua/study# hello
total 32
drwxr-xr-x  3    0    0  4096  4月 17 20:37 .
drwxr-x--- 16 1000 1000  4096  4月 19 13:55 ..
-rw-r--r--  1    0    0 18900  4月 19 00:31 temp.txt
drwxr-xr-x  3    0    0  4096  4月 17 20:00 test

grep 指令

语法grep [选项] 搜寻字符串 ⽂件
功能:在⽂件中搜索字符串,将找到的⾏打印出来
常⽤选项:
-i :忽略⼤⼩写的不同,所以⼤⼩写视为相同
-n :顺便输出⾏号
-v :反向选择,亦即显⽰出没有 '搜寻字符串' 内容的那⼀⾏
root@lijunhua-virtual-machine:/home/lijunhua/study# cat temp.txt
what can i say?
hello world!
hello ljh
hello world!
hello ljh
hello ljh
what can i say?
root@lijunhua-virtual-machine:/home/lijunhua/study# grep -n "what" temp.txt
1:what can i say?
7:what can i say?
root@lijunhua-virtual-machine:/home/lijunhua/study# grep -nv "what" temp.txt
2:hello world!
3:hello ljh
4:hello world!
5:hello ljh
6:hello ljh
root@lijunhua-virtual-machine:/home/lijunhua/study# grep -ni "whAt" temp.txt
1:what can i say?
7:what can i say?

zip/unzip 指令

语法zip 压缩⽂件.zip ⽬录或⽂件
功能:将⽬录或⽂件压缩成zip格式

常用选项:

-r : 递归处理,将指定目录下的所有文件一并处理

举例:

root@lijunhua-virtual-machine:/home/lijunhua/study# zip temp.zip temp.txt 
  adding: temp.txt (deflated 52%)
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 20
drwxr-xr-x  3 root     root     4096  4月 19 14:11 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 19 13:55 ../
-rw-r--r--  1 root     root       88  4月 19 14:06 temp.txt
-rw-r--r--  1 root     root      208  4月 19 14:11 temp.zip
drwxr-xr-x  3 root     root     4096  4月 17 20:00 test/

root@lijunhua-virtual-machine:/home/lijunhua/study# unzip temp.zip -d test
Archive:  temp.zip
  inflating: test/temp.txt           
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── temp.txt
├── temp.zip
└── test
    ├── dir
    │   ├── file.txt
    │   └── myfile.txt
    ├── file.txt
    ├── myfile.txt
    └── temp.txt

2 directories, 7 files

tar 指令

打包/解包,不打开它,直接看内容
语法tar [-cxtzjvf] ⽂件与⽬录 .
参数

• -c :建⽴⼀个压缩⽂件的参数指令(create 的意思);
• -x :解开⼀个压缩⽂件的参数指令!
• -t :查看 tarfile ⾥⾯的⽂件!
• -z :是否同时具有 gzip 的属性?亦即是否需要⽤ gzip 压缩?
• -j :是否同时具有 bzip2 的属性?亦即是否需要⽤ bzip2 压缩?
• -v :压缩的过程中显⽰⽂件!这个常⽤,但不建议⽤在背景执⾏过程!
• -f :使⽤档名,请留意,在 f 之后要⽴即接档名喔!不要再加参数!
• -C : 解压到指定⽬录

选项很多,但实际我们基本上只会用到几个固定的

tar -czvf dst.tgz scr
tar -xzvf scr.tgz -C dstdir
root@lijunhua-virtual-machine:/home/lijunhua/study# mkdir -p d1/d2/d3/d4
//打包压缩
root@lijunhua-virtual-machine:/home/lijunhua/study# tar -czvf d.tgz d1
d1/
d1/d2/
d1/d2/d3/
d1/d2/d3/d4/
root@lijunhua-virtual-machine:/home/lijunhua/study# ll
total 24
drwxr-xr-x  4 root     root     4096  4月 21 00:45 ./
drwxr-x--- 16 lijunhua lijunhua 4096  4月 21 00:36 ../
drwxr-xr-x  3 root     root     4096  4月 21 00:44 d1/
-rw-r--r--  1 root     root      149  4月 21 00:45 d.tgz
-rw-r--r--  1 root     root       88  4月 19 14:06 temp.txt
drwxr-xr-x  3 root     root     4096  4月 19 14:12 test/
//解包
root@lijunhua-virtual-machine:/home/lijunhua/study# tar -xzvf d.tgz -C d1
d1/
d1/d2/
d1/d2/d3/
d1/d2/d3/d4/
root@lijunhua-virtual-machine:/home/lijunhua/study# tree
.
├── d1
│   ├── d1
│   │   └── d2
│   │       └── d3
│   │           └── d4
│   └── d2
│       └── d3
│           └── d4
├── d.tgz
├── temp.txt
└── test
    ├── dir
    │   ├── file.txt
    │   └── myfile.txt
    ├── file.txt
    ├── myfile.txt
    └── temp.txt

10 directories, 7 files

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值