linux初识--基础指令

Linux下基础指令

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 列出所有⼦⽬录下的⽂件。(递归)
-1 ⼀⾏只输出⼀个⽂件。
举例:

cpp 复制代码
[root@VM-8-16-centos ~]# ls #列出root目录下的文件
repos
[root@VM-8-16-centos ~]# ls -l #详细列出root目录下的文件,包括文件大小 创建时间等
total 4
drwxr-xr-x 2 root root 4096 May 20 14:02 repos
[root@VM-8-16-centos ~]# ls -a #详细列出root目录下包括隐藏的文件,带.开头的文件都是隐藏文件
.              .bash_logout   .cache   .lesshst  .pydistutils.cfg  .tcshrc
..             .bash_profile  .config  .pip      repos             .viminfo
.bash_history  .bashrc        .cshrc   .pki      .ssh
[root@VM-8-16-centos ~]# ls -al #指令可以组合使用,即列出详细信息和隐藏文件
total 68
dr-xr-x---.  8 root root 4096 May 20 14:20 .
dr-xr-xr-x. 19 root root 4096 May 23 08:35 ..
-rw-------   1 root root 1071 May 23 08:35 .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 May 20 03:10 .cache
drwxr-xr-x   3 root root 4096 Mar  7  2019 .config
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
-rw-------   1 root root   35 May 20 14:20 .lesshst
drwxr-xr-x   2 root root 4096 Jun 13  2024 .pip
drwxr-----   3 root root 4096 Jun 13  2024 .pki
-rw-r--r--   1 root root   73 Jun 13  2024 .pydistutils.cfg
drwxr-xr-x   2 root root 4096 May 20 14:02 repos
drwx------   2 root root 4096 May 19 23:18 .ssh
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
-rw-------   1 root root  911 May 20 14:17 .viminfo
[root@VM-8-16-centos ~]# ll  #简写指令==ls -l
total 4
drwxr-xr-x 2 root root 4096 May 20 14:02 repos
[root@VM-8-16-centos ~]# 

pwd 命令

语法: pwd
功能:显⽰⽤⼾当前所在的⽬录
常⽤选项:
无
举例:

cpp 复制代码
[root@VM-8-16-centos repos]# pwd
/root/repos

cd 指令

Linux理论知识:路径的认识
Linux系统中,磁盘上的⽂件和⽬录被组成⼀棵⽬录树,每个节点都是**⽬录或⽂件**
其中普通⽂件⼀定是⽬录树的叶⼦节点
⽬录可能是叶⼦(空⽬录), 也可能是路上节点
理解路径存在的意义: 树状组织⽅式,都是为了保证快速定位查找到指定的⽂件,⽽定位⽂件就
需要具有唯⼀性的⽅案来进⾏定位⽂件。其中任何⼀个节点,都只有⼀个⽗节点,所以,从根⽬
录开始,定位指定⽂件,路径具有唯⼀性
绝对路径:⼀般从/开始,不依赖其他⽬录的定位⽂件的⽅式
相对路径:相对于当前⽤⼾所处⽬录,定位⽂件的路径⽅式
绝对路径⼀般不会随着⽤⼾的路径变化⽽丧失唯⼀性,⼀般在特定服务的配置⽂件中经常被使⽤
相对路径因为它的便捷性,⼀般在命令⾏中使⽤较多


语法: cd ⽬录名
功能:改变⼯作⽬录。将当前⼯作⽬录改变到指定的⽬录下
举例:

cpp 复制代码
[root@VM-8-16-centos repos]# pwd
/root/repos
[root@VM-8-16-centos repos]# cd .. #使用相对路径返回上级目录
[root@VM-8-16-centos ~]# pwd
/root
[root@VM-8-16-centos ~]# cd /root/repos #使用绝对路径转到目录
[root@VM-8-16-centos repos]# pwd
/root/repos
[root@VM-8-16-centos repos]# cd ~ #返回家目录
[root@VM-8-16-centos ~]# pwd
/root
[root@VM-8-16-centos ~]# cd - #返回最近一次所处目录
/root/repos
[root@VM-8-16-centos repos]# 

touch 指令

Linux理论知识:⽂件类型的认识
语法: touch 选项 ... ⽂件 ...
功能:touch命令参数可更改⽂档或⽬录的⽇期时间,包括存取时间和更改时间,或者新建⼀个不存在的⽂件。
常⽤选项:
-a : 仅更改访(access)问时间
•
-c : 仅更改修改(modify)时间
举例:

cpp 复制代码
[root@VM-8-16-centos repos]# ls
test.txt
[root@VM-8-16-centos repos]# touch Newtest.txt #创建一个普通文件
[root@VM-8-16-centos repos]# ls
Newtest.txt  test.txt
[root@VM-8-16-centos repos]# stat Newtest.txt #查看文件详情,包括access时间和modify时间
  File: 'Newtest.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 661227      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-05-23 08:59:26.050370194 +0800
Modify: 2025-05-23 08:59:26.050370194 +0800
Change: 2025-05-23 08:59:26.050370194 +0800
 Birth: -
[root@VM-8-16-centos repos]# touch -a Newtest.txt #修改access时间
[root@VM-8-16-centos repos]# stat Newtest.txt 
  File: 'Newtest.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 661227      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-05-23 09:01:14.791166007 +0800
Modify: 2025-05-23 08:59:26.050370194 +0800
Change: 2025-05-23 09:01:14.791166007 +0800
 Birth: -
[root@VM-8-16-centos repos]# touch -m Newtest.txt #修改modify时间
[root@VM-8-16-centos repos]# stat Newtest.txt 
  File: 'Newtest.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 661227      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2025-05-23 09:01:14.791166007 +0800
Modify: 2025-05-23 09:01:56.621087369 +0800
Change: 2025-05-23 09:01:56.621087369 +0800
 Birth: -

mkdir 指令

语法: mkdir 选项 dirname...
功能:在当前⽬录下创建⼀个名为 "dirname"的⽬录
常⽤选项:
-p/--parents: 可以是⼀个路径名称。此时若路径中的某些⽬录尚不存在,加上此选项后,系统将⾃
动建⽴好那些尚不存在的⽬录,即⼀次可以建⽴多个⽬录
举例:

cpp 复制代码
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# mkdir mydir #创建一个空目录
[root@VM-8-16-centos repos]# ll
total 4
drwxr-xr-x 2 root root 4096 May 23 09:07 mydir
-rw-r--r-- 1 root root    0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root    0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# mkdir -p path1/path2/path3 #递归建⽴多个⽬录,创建指定路径
[root@VM-8-16-centos repos]# ll
total 8
drwxr-xr-x 2 root root 4096 May 23 09:07 mydir
-rw-r--r-- 1 root root    0 May 23 09:01 Newtest.txt
drwxr-xr-x 3 root root 4096 May 23 09:07 path1
-rw-r--r-- 1 root root    0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# tree #tree命令是⼀个递归以树状结构显⽰指定⽬录的命令
.
├── mydir
├── Newtest.txt
├── path1
│   └── path2
│       └── path3
└── test.txt

rmdir 指令 && rm 指令

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

cpp 复制代码
[root@VM-8-16-centos repos]# rmdir mydir #删除空目录
[root@VM-8-16-centos repos]# ll
total 4
-rw-r--r-- 1 root root    0 May 23 09:01 Newtest.txt
drwxr-xr-x 3 root root 4096 May 23 09:07 path1
-rw-r--r-- 1 root root    0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# rmdir -p path1/path2/path3 #删除路径,指定路径中有不为空的路径。⽆法删除
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt

rm 命令可以同时删除⽂件或⽬录

语法: rm -f-i-r-v dirName/dir
适⽤对象:所有使⽤者
功能:删除⽂件或⽬录
常⽤选项:
-f 即使⽂件属性为只读(即写保护),亦直接删除
-i 删除前逐⼀询问确认
-r 删除⽬录及其下所有⽂件
举例:

cpp 复制代码
[root@VM-8-16-centos repos]# ll
total 4
drwxr-xr-x 2 root root 4096 May 23 09:16 mydir
-rw-r--r-- 1 root root    0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root    0 May 23 09:15 test.txt #直接删除普通文件
[root@VM-8-16-centos repos]# rm  -rf test.txt
[root@VM-8-16-centos repos]# rm -rf mydir #递归删除目录文件
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt

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 略
举例:

cpp 复制代码
[root@VM-8-16-centos repos]# man ls
[root@VM-8-16-centos repos]# man rm
[root@VM-8-16-centos repos]# man mkdir

cp 指令

语法: cp 选项 源⽂件或⽬录 ⽬标⽂件或⽬录
功能: 复制⽂件或⽬录
说明:
cp指令⽤于复制⽂件或⽬录
如同时指定两个以上的⽂件或⽬录,且最后的⽬的地是⼀个已经存在的⽬录,则它会把前⾯指定
的所有⽂件或⽬录复制到此⽬录中
常⽤选项
-f 或 --force 强⾏复制⽂件或⽬录, 不论⽬的⽂件或⽬录是否已经存在
-i 或 --interactive 覆盖⽂件之前先询问⽤⼾
-r 递归处理,将指定⽬录下的⽂件与⼦⽬录⼀并处理。若源⽂件或⽬录的形态,不属于⽬录或符
号链接,则⼀律视为普通⽂件处理

cpp 复制代码
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
[root@VM-8-16-centos repos]# echo "hello world">Newtest.txt
[root@VM-8-16-centos repos]# cat Newtest.txt 
hello world
[root@VM-8-16-centos repos]# 
[root@VM-8-16-centos repos]# cp Newtest.txt  tset.txt #复制文件
[root@VM-8-16-centos repos]# ll
total 8
-rw-r--r-- 1 root root 12 May 23 09:27 Newtest.txt
-rw-r--r-- 1 root root 12 May 23 09:28 tset.txt
[root@VM-8-16-centos repos]# cat tset.txt
hello world
[root@VM-8-16-centos repos]# mkdir mydir
[root@VM-8-16-centos repos]# cp *.txt mydir #复制多个文件到指定目录下,*号是通配符
[root@VM-8-16-centos repos]# tree mydir
mydir
├── Newtest.txt
└── tset.txt

mv 指令

mv命令是move的缩写,可以⽤来移动⽂件或者将⽂件改名(move (rename) files,经常⽤来备份⽂件或者⽬录
语法: mv 选项 源⽂件或⽬录 ⽬标⽂件或⽬录
功能:

  1. 视mv命令中第⼆个参数类型的不同(是⽬标⽂件还是⽬标⽬录),mv命令将⽂件重命名或将其
    移⾄⼀个新的⽬录中。
  2. 当第⼆个参数类型是⽂件时,mv命令完成⽂件重命名,此时,源⽂件只能有⼀个(也可以是源⽬录名),它将所给的源⽂件或⽬录重命名为给定的⽬标⽂件名。
  3. 当第⼆个参数是已存在的⽬录名称时,源⽂件或⽬录参数可以有多个,mv命令将各参数指定的源⽂件均移⾄⽬标⽬录中。
    常⽤选项:
    -f :force 强制的意思,如果⽬标⽂件已经存在,不会询问⽽直接覆盖
    -i :若⽬标⽂件 (destination) 已经存在时,就会询问是否覆盖!
    举例:
cpp 复制代码
[root@VM-8-16-centos repos]# ll
total 8
drwxr-xr-x 2 root root 4096 May 23 09:34 mydir
-rw-r--r-- 1 root root   12 May 23 09:27 Newtest.txt
[root@VM-8-16-centos repos]# mv Newtest.txt test.txt #重命名文件
[root@VM-8-16-centos repos]# ll
total 8
drwxr-xr-x 2 root root 4096 May 23 09:34 mydir
-rw-r--r-- 1 root root   12 May 23 09:27 test.txt
[root@VM-8-16-centos repos]# mv test.txt mydir #移动文件到指定目录下
[root@VM-8-16-centos repos]# tree mydir
mydir
└── test.txt

cat 指令

语法: cat 选项 ⽂件
功能: 查看⽬标⽂件的内容
常⽤选项:
-b 对⾮空输出⾏编号
-n 对输出的所有⾏编号
-s 不输出多⾏空⾏
举例:

more 指令

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

less 指令

less ⼯具也是对⽂件或其它输出进⾏分⻚显⽰的⼯具,应该说是linux正统查看⽂件内容的⼯具,
功能极其强⼤
less 的⽤法⽐起 more 更加的有弹性,在 more 的时候,我们并没有办法向前⾯翻, 只能往后⾯
看但若使⽤了 less 时,就可以使⽤ pageup pagedown 等按键的功能来往前往后翻看⽂件,更
容易⽤来查看⼀个⽂件的内容除此之外,在 less ⾥头可以拥有更多的搜索功能,不⽌可以向下搜,也可以向上搜。
语法: less 参数 ⽂件
功能:less与more类似,但使⽤less可以随意浏览⽂件,⽽more仅能向前移动,却不能向后移动,⽽且less在查看之前不会加载整个⽂件。
选项:
-i 忽略搜索时的⼤⼩写
-N 显⽰每⾏的⾏号
/字符串:向下搜索"字符串"的功能
?字符串:向上搜索"字符串"的功能
n:重复前⼀个搜索(与 / 或 ? 有关)
N:反向重复前⼀个搜索(与 / 或 ? 有关)
q:quit
举例:
less -N test.txt

grep 指令

语法: grep 选项 搜寻字符串 ⽂件
功能:在⽂件中搜索字符串,将找到的⾏打印出来
常⽤选项:
-i :忽略⼤⼩写的不同,所以⼤⼩写视为相同
-n :顺便输出⾏号
-v :反向选择,亦即显⽰出没有 '搜寻字符串' 内容的那⼀⾏
举例:

head 指令

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

tail 指令

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

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的午夜)开始所经过的秒数,不考虑闰秒
    举例:
cpp 复制代码
[root@VM-8-16-centos mydir]# date #获取当前时间
Fri May 23 13:18:28 CST 2025
[root@VM-8-16-centos mydir]# date +%Y-%m-%d-%H:%M:%S #以特定格式获取当前时间
2025-05-23-13:19:43
[root@VM-8-16-centos mydir]# date +%s #获取当前时间戳
1747977614
[root@VM-8-16-centos mydir]# date -d @10000000 #获取时间戳时间
Mon Apr 27 01:46:40 CST 1970

find 指令

Linux下find命令在⽬录结构中搜索⽂件,并执⾏指定的操作。
Linux下find命令提供了相当多的查找条件,功能很强⼤。由于find具有强⼤的功能,所以它的选
项也很多,其中⼤部分选项都值得我们花时间来了解⼀下。
即使系统中含有⽹络⽂件系统( NFS),find命令在该⽂件系统中同样有效,只要你具有相应的权
限。
在运⾏⼀个⾮常消耗资源的find命令时,很多⼈都倾向于把它放在后台执⾏,因为遍历⼀个⼤的
⽂件系统可能会花费很⻓的时间(这⾥是指30G字节以上的⽂件系统)。
语法: find pathname -options
功能:⽤于在⽂件树中查找⽂件,并作出相应的处理(可能访问磁盘)
常⽤选项
-name 按照⽂件名查找⽂件

which 指令

功能:搜索系统指定的命令

alias 指令

功能:设置命令的别名

相关推荐
无忧.芙桃6 小时前
Linux系统编程(四):用户与权限管理
linux·运维·服务器
最强小杰6 小时前
某 OpenAI 兼容网关上的 `kimi-k3.1-pro`:Cline、Zed 接入与 SSE 排错示例
java·服务器·网络·ai
谙弆悕博士8 小时前
多智能体架构详解:层级树状架构(分级主从型)
运维·架构·大模型·agent·多智能体·langgraph·层级树状
戊本菌9 小时前
进程(三)进程优先级:谁先上 CPU
linux·c语言·硬件架构
aningx9 小时前
Arch 系用 greetd 实现开机自动进 Plasma Wayland,注销自动回桌面
linux
张小姐的猫10 小时前
【基于 WebSocket 的多用户网页五子棋】—— 整合封装服务器模块设计实现2️⃣
服务器·websocket·网络协议
江无行者10 小时前
Linux 服务器 top 命令详解
linux
HMS工业网络10 小时前
工业远程访问的五项安全部署原则
运维·服务器·安全
吴建旭 智宅焕12 小时前
智能家居全国交付中执行端能力评估与分级调度架构设计:从注册数量到交付确定性的系统实现
运维·智能家居·devops
嵌入式 Linux手记12 小时前
一块内存,三种解释:union 的真实用法
linux·c语言·驱动开发·嵌入式