【Linux】笔记01:基础命令

文章目录

1. 新建用户

useradd 用户名:新建用户

passwd 用户名:修改密码

ssh 用户名@公网ip:登录

userdel -r 用户名:删除用户并删除家目录(-r不能省略)

whoami:查看当前用户

2.ls

语法

ls [选项] [目录或文件]

选项可以有 0 个或多个,多个选项中间使用空格隔开

功能

对于目录,该命令列出该目录下所有的子目录和文件;对于文件,该命令列出文件名及其他信息。

常用选项

  • -a列出目录下的所有文件,包括以.开头的隐藏文件
  • -d将目录像文件夹一样显示,而不是显示其下的文件。如:ls -d 指定目录。(只查看目录本身属性)
  • -i输出文件的 i 节点的索引信息。如 ls -ai 指定文件
  • -k以 k 字节的形式表示文件的大小。如 ls -alk 指定文件
  • -l列出文件的详细信息。(ls -l的别名是 ll)
  • -n用数字的 UID, GID 代替名称
  • -F在每个文件名后附上一个字符以说明该文件的类型," * " 表示可执行的普通文件;" / "表示目录," @ "表示符号链接; " | "表示FIFOs; " = " 表示套接字(sockets)。(目录类型识别)
  • -r对目录反向排序
  • -t以时间排序
  • -s在 l 文件名后输出该文件的大小。
  • -R列出所有子目录下的文件(递归)
  • -1一行只输出一个文件

文件 = 内容 + 属性

所以文件操作本质就是对文件内容或文件属性进行操作

ls 就是显示文件的属性,只有 ls 就是只显示 文件名

比较常用的选项:-a -l

举例

shell 复制代码
[zsy@VM-0-8-centos test]$ ls
dir  file
[zsy@VM-0-8-centos test]$ ls -a
.  ..  dir  file
[zsy@VM-0-8-centos test]$ ls -al
total 12
drwxrwxr-x  3 zsy zsy 4096 Jul 12 18:09 .
drwx------ 12 zsy zsy 4096 Jul 12 18:09 ..
drwxrwxr-x  2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r--  1 zsy zsy    0 Jul 12 18:09 file
[zsy@VM-0-8-centos test]$ ls -a -l -F
total 12
drwxrwxr-x  3 zsy zsy 4096 Jul 12 18:09 ./
drwx------ 12 zsy zsy 4096 Jul 12 18:09 ../
drwxrwxr-x  2 zsy zsy 4096 Jul 12 18:09 dir/
-rw-rw-r--  1 zsy zsy    0 Jul 12 18:09 file
[zsy@VM-0-8-centos test]$ ls -alFn
total 12
drwxrwxr-x  3 1002 1002 4096 Jul 12 18:09 ./
drwx------ 12 1002 1002 4096 Jul 12 18:09 ../
drwxrwxr-x  2 1002 1002 4096 Jul 12 18:09 dir/
-rw-rw-r--  1 1002 1002    0 Jul 12 18:09 file
  • Linux中,任何一个目录(包括空目录),系统默认都会自带 ...
    .是当前目录,..是上级目录
  • ls -l第一列的字符表示文件类型:d为目录,-为普通文件

-d 查看目录本身的属性

shell 复制代码
[zsy@VM-0-8-centos ~]$ ls -ld test
drwxrwxr-x 3 zsy zsy 4096 Jul 12 18:09 test
[zsy@VM-0-8-centos ~]$  ls -ald /
dr-xr-xr-x. 19 root root 4096 Jul 12 20:35 /

ls -lll的关系

命令的本质是系统中可执行的普通文件

shell 复制代码
[zsy@VM-0-8-centos test]$ which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[zsy@VM-0-8-centos test]$ which mkdir
/usr/bin/mkdir
[zsy@VM-0-8-centos test]$ which pwd
/usr/bin/pwd

[zsy@VM-0-8-centos test]$ ls /usr/bin/ls -l
-rwxr-xr-x 1 root root 117608 Nov 17  2020 /usr/bin/ls
[zsy@VM-0-8-centos test]$ ls /usr/bin/mkdir -l
-rwxr-xr-x 1 root root 79768 Nov 17  2020 /usr/bin/mkdir
[zsy@VM-0-8-centos test]$ ls /usr/bin/pwd -l
-rwxr-xr-x 1 root root 33232 Nov 17  2020 /usr/bin/pwd
  • alias是一条 可以给其他命令起别名 的命令(用户使用alias给命令起别名是临时的)
  • 所以 llls -l的关系是 "llls -l的别名",两个命令是等价的
shell 复制代码
[zsy@VM-0-8-centos test]$ which ll
alias ll='ls -l --color=auto'
	/usr/bin/ls

3. pwd

语法

pwd

功能

显示用户当前所在的目录

举例

shell 复制代码
[zsy@VM-0-8-centos test]$ pwd
/home/zsy/test

4. cd

Linux理论知识:路径

  • Linux文件系统中,磁盘上的文件和目录被组成一棵目录树,每个节点都是目录或文件
  • 其中普通文件一定是目录树的叶子结点
  • 目录可能是叶子结点(空目录)或路上节点
  • 路径存在的意义 :树状组织方式,为了保证快速定位查找到目标文件,而定位文件需要具有唯一性的方案来进行定位文件。其中任何一个节点都只有一个父节点,所以,从根节点开始,定位指定文件的路径具有唯一性
  • 绝对路径 :一般从 /开始,不依赖其他目录的定位文件的方式
  • 相对路径:相对于当前用户所处的目录定位文件的路径方式
  • 绝对路径一般不会随着用户的路径变化而丧失唯一性,一般咋特定服务的配置文件中经常被使用
  • 相对路径由于它的便捷性,一般在命令行中使用较多

语法

cd 目录名

功能

改变工作目录,将当前工作目录改变到指定的目录下

举例

返回上级目录

shell 复制代码
[zsy@VM-0-8-centos ~]$ pwd
/home/zsy
[zsy@VM-0-8-centos ~]$ cd ..
[zsy@VM-0-8-centos home]$ pwd
/home

绝对路径定位目录或文件

shell 复制代码
[zsy@VM-0-8-centos home]$ pwd
/home
[zsy@VM-0-8-centos home]$ cd /home/zsy/test
[zsy@VM-0-8-centos test]$ pwd
/home/zsy/test

相对路径定位目录或文件

shell 复制代码
[zsy@VM-0-8-centos test]$ pwd
/home/zsy/test
[zsy@VM-0-8-centos test]$ cd ../test
[zsy@VM-0-8-centos test]$ pwd
/home/zsy/test

每一次新建一个用户都会在/home目录下为新用户创建一个新的工作目录,目录以新用户名称命名

root用户在按照操作系统时就已经有了内置工作目录 /root

快速进入自己的家目录

任何一个Linux用户,使用特定用户名登录系统时,默认在家目录下

shell 复制代码
[zsy@VM-0-8-centos test]$ whoami
zsy
[zsy@VM-0-8-centos test]$ pwd
/home/zsy/test
[zsy@VM-0-8-centos test]$ cd ~
[zsy@VM-0-8-centos ~]$ pwd
/home/zsy

返回到最近访问的目录

shell 复制代码
[zsy@VM-0-8-centos ~]$ pwd
/home/zsy
[zsy@VM-0-8-centos ~]$ cd /
[zsy@VM-0-8-centos /]$ pwd
/
[zsy@VM-0-8-centos /]$ cd -
/home/zsy
[zsy@VM-0-8-centos ~]$ pwd
/home/zsy
[zsy@VM-0-8-centos ~]$ cd -
/
[zsy@VM-0-8-centos /]$ pwd
/
[zsy@VM-0-8-centos /]$ cd -
/home/zsy
[zsy@VM-0-8-centos ~]$ pwd
/home/zsy

cd -可以直接回退到最近一次所处的目录

-表示最近所处的路径

5. touch

语法

touch [选项]... [文件]...

功能

touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件

touch不能用来创建目录,只能创建普通文件

新建目录要使用 mkdir

常用选项

  • -a:change only the access time
  • -m:change only the modification time

举例

创建普通文件

shell 复制代码
[zsy@VM-0-8-centos test]$ ls
dir  file
[zsy@VM-0-8-centos test]$ touch newFile.txt
[zsy@VM-0-8-centos test]$ ls
dir  file  newFile.txt

查看文件时间

stat 文件名可以查看文件详细属性

文件有三个时间属性:

  • Access 文件最近被访问的时间
  • Modify 修改时间(文件内容)
  • Change 状态改变时间(文件属性)
shell 复制代码
[zsy@VM-0-8-centos test]$ stat newFile.txt 
  File: 'newFile.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 1049254     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1002/     zsy)   Gid: ( 1002/     zsy)
Access: 2026-07-14 17:21:30.522073775 +0800
Modify: 2026-07-14 17:21:30.522073775 +0800
Change: 2026-07-14 17:21:30.522073775 +0800
 Birth: -

修改文件的 Access 时间

shell 复制代码
[zsy@VM-0-8-centos test]$ touch -a newFile.txt 
[zsy@VM-0-8-centos test]$ stat newFile.txt 
  File: 'newFile.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 1049254     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1002/     zsy)   Gid: ( 1002/     zsy)
Access: 2026-07-14 17:25:25.683205074 +0800
Modify: 2026-07-14 17:21:30.522073775 +0800
Change: 2026-07-14 17:25:25.683205074 +0800
 Birth: -

在修改文件 Access 时间的同时,文件的 Change 时间也被修改

修改文件的 Modify 时间

shell 复制代码
[zsy@VM-0-8-centos test]$ touch -m newFile.txt 
[zsy@VM-0-8-centos test]$ stat newFile.txt 
  File: 'newFile.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d	Inode: 1049254     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1002/     zsy)   Gid: ( 1002/     zsy)
Access: 2026-07-14 17:25:25.683205074 +0800
Modify: 2026-07-14 17:27:49.378784683 +0800
Change: 2026-07-14 17:27:49.378784683 +0800
 Birth: -

在修改文件 Modify 时间的同时,文件的 Change 时间也被修改

原因:Access 和 Modify 属于文件的属性,Change 表示状态改变时间,当文件属性被修改时,Change 也被修改

6. mkdir

语法

mkdir [选项] dirname...

功能

在当前目录下创建一个名为 "dirname"的目录

常用选项

  • -p/--parents可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以创建多个目录

举例

创建普通空目录

shell 复制代码
[zsy@VM-0-8-centos test]$ ls -l
total 4
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
[zsy@VM-0-8-centos test]$ mkdir mydir
[zsy@VM-0-8-centos test]$ ls -l
total 8
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
drwxrwxr-x 2 zsy zsy 4096 Jul 14 17:35 mydir
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt

递归建立多个目录,创建指定路径

shell 复制代码
[zsy@VM-0-8-centos test]$ mkdir -p path1/path2/path3/path4
[zsy@VM-0-8-centos test]$ ls -l
total 12
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
drwxrwxr-x 2 zsy zsy 4096 Jul 14 17:35 mydir
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 17:37 path1
[zsy@VM-0-8-centos test]$ tree path1
path1
`-- path2
    `-- path3
        `-- path4

3 directories, 0 files

tree命令是一个递归以树状结构显示指定目录的命令

安装tree命令:yum install -y tree

7. rmdir

rmdir是一个与mkdir相对应的命令,mkdir是创建目录,而rmdir是删除目录

语法

rmdir [-p] [dirname]

使用对象

具有当前目录操作权限的所有使用者

功能

删除空目录

rmdir默认只能删除空目录,不能删除非空目录和普通文件

常用选项

  • -p当子目录被删除后如果父目录也变成空目录,就连带父目录一起删除

举例

删除空目录

shell 复制代码
[zsy@VM-0-8-centos test]$ ll
total 12
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
drwxrwxr-x 2 zsy zsy 4096 Jul 14 17:35 mydir
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 17:37 path1
[zsy@VM-0-8-centos test]$ tree mydir
mydir

0 directories, 0 files
[zsy@VM-0-8-centos test]$ rmdir mydir/
[zsy@VM-0-8-centos test]$ ll
total 8
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 17:37 path1

删除路径

shell 复制代码
[zsy@VM-0-8-centos test]$ tree path1
path1
`-- path2
    `-- path3
        `-- path4

3 directories, 0 files
[zsy@VM-0-8-centos test]$ rmdir -p path1/path2/path3/path4/
[zsy@VM-0-8-centos test]$ ll
total 4
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt

指定路径中有不为空的路径,无法删除

shell 复制代码
[zsy@VM-0-8-centos test]$ rmdir path1/path2/path3/
rmdir: failed to remove 'path1/path2/path3/': Directory not empty

8. rm

rm命令可以同时删除文件或目录

语法

rm [-f-i-r-v] [dirName/dir]

使用对象

所有使用者

功能

删除文件或目录

常用选项

  • -f强制删除
  • -i删除前逐一询问确认
  • -r删除目录及其下所有文件(递归删除)

rm默认只能删除普通文件

rm -rf *删除当前目录

rm -rf递归并强制删除指定目录

举例

删除普通文件

shell 复制代码
[zsy@VM-0-8-centos test]$ ll
total 8
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 12 18:09 file
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1
[zsy@VM-0-8-centos test]$ rm file
[zsy@VM-0-8-centos test]$ ll
total 8
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1

删除目录文件

rm默认无法删除目录,需要使用 -r选项递归删除目录

shell 复制代码
[zsy@VM-0-8-centos test]$ ll
total 8
drwxrwxr-x 2 zsy zsy 4096 Jul 12 18:09 dir
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1
[zsy@VM-0-8-centos test]$ rm dir
rm: cannot remove 'dir': Is a directory
[zsy@VM-0-8-centos test]$ rm -r dir
[zsy@VM-0-8-centos test]$ ll
total 4
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1

删除普通文件前询问

shell 复制代码
[zsy@VM-0-8-centos test]$ ll
total 4
-rw-rw-r-- 1 zsy zsy    0 Jul 14 17:27 newFile.txt
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1
[zsy@VM-0-8-centos test]$ rm -i newFile.txt
rm: remove regular empty file 'newFile.txt'? y
[zsy@VM-0-8-centos test]$ ll
total 4
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1

删除目录前询问

shell 复制代码
[zsy@VM-0-8-centos test]$ ll
total 4
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1
[zsy@VM-0-8-centos test]$ mkdir -p a/a/a/a
[zsy@VM-0-8-centos test]$ tree a
a
`-- a
    `-- a
        `-- a

3 directories, 0 files
[zsy@VM-0-8-centos test]$ rm -ri a
rm: descend into directory 'a'? y
rm: descend into directory 'a/a'? y
rm: descend into directory 'a/a/a'? y
rm: remove directory 'a/a/a/a'? y
rm: remove directory 'a/a/a'? y
rm: remove directory 'a/a'? y
rm: remove directory 'a'? y
[zsy@VM-0-8-centos test]$ ll
total 4
drwxrwxr-x 3 zsy zsy 4096 Jul 14 19:04 path1

递归并强制删除非空目录

shell 复制代码
[zsy@VM-0-8-centos test]$ tree path1/
path1/
|-- myfile.txt
`-- path2
    `-- path3
        `-- path4

3 directories, 1 file
[zsy@VM-0-8-centos test]$ rm -f path1/
rm: cannot remove 'path1/': Is a directory
[zsy@VM-0-8-centos test]$ rm -rf path1/
[zsy@VM-0-8-centos test]$ ll
total 0

9. man

Linux 的命令有很多参数,可以通过查看联机手册获取帮助

语法

man [选项] 命令

常用选项

  • -k根据关键字搜索联机帮助
  • num只在第 num 章节查找
  • -a将所有章节的都显示出来。比如 man printf默认只会在第一章搜索,找到就停止,用 a 选项,当按下 q 退出,它会继续往后面搜索,直到所有章节都搜索完毕

man 手册分为 9 章(不同系统可能会有差别)

shell 复制代码
1   Executable programs or shell commands(普通命令)
2   System calls (functions provided by the kernel)(系统调用)
3   Library calls (functions within program libraries)(库函数)
4   Special files (usually found in /dev)
5   File formats and conventions eg /etc/passwd
6   Games
7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8   System administration commands (usually only for root)
9   Kernel routines [Non standard]

举例

查看 printf 指令

shell 复制代码
[zsy@VM-0-8-centos ~]$ man printf

查看系统调用

shell 复制代码
[zsy@VM-0-8-centos ~]$ man fork

查看系统调用(指定章节)

shell 复制代码
[zsy@VM-0-8-centos ~]$ man 2 fork

查看 C 库函数

shell 复制代码
[zsy@VM-0-8-centos ~]$ man 3 printf

查看变量

shell 复制代码
[zsy@VM-0-8-centos ~]$ man 7 signal
[zsy@VM-0-8-centos ~]$ man 7 environ

查看 man 手册

shell 复制代码
[zsy@VM-0-8-centos ~]$ man man

10. cp

语法

cp [选项] 源文件或目录 目标文件或目录

功能

复杂文件或目录

说明

  • cp指令用于复制文件或目录
  • 如果同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的文件或目录复制到此目录内部

常用选项

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

举例

cp 普通文件

shell 复制代码
[zsy@VM-0-8-centos test]$ echo '你好' > myfile.txt
[zsy@VM-0-8-centos test]$ cat myfile.txt 
你好
[zsy@VM-0-8-centos test]$ ll
total 4
-rw-rw-r-- 1 zsy zsy 7 Jul 15 11:17 myfile.txt
[zsy@VM-0-8-centos test]$ cp myfile.txt myfile-backup.txt
[zsy@VM-0-8-centos test]$ ll
total 8
-rw-rw-r-- 1 zsy zsy 7 Jul 15 11:18 myfile-backup.txt
-rw-rw-r-- 1 zsy zsy 7 Jul 15 11:17 myfile.txt
[zsy@VM-0-8-centos test]$ cat myfile-backup.txt 
你好

echo命令可以把后续内容当做字符串打印出来(建议使用单引号),会自动加换行符

cat命令可以查看文件内容

>是重定向符号,在 echo "你好" > myfile.txt中将要输出的 "你好" 重定向到文件 "myfile.txt" 中

将多个文件拷贝到指定路径下

shell 复制代码
[zsy@VM-0-8-centos test]$ ll
total 8
-rw-rw-r-- 1 zsy zsy 7 Jul 15 11:18 myfile-backup.txt
-rw-rw-r-- 1 zsy zsy 7 Jul 15 11:17 myfile.txt
[zsy@VM-0-8-centos test]$ mkdir dir
[zsy@VM-0-8-centos test]$ ll
total 12
drwxrwxr-x 2 zsy zsy 4096 Jul 15 11:22 dir
-rw-rw-r-- 1 zsy zsy    7 Jul 15 11:18 myfile-backup.txt
-rw-rw-r-- 1 zsy zsy    7 Jul 15 11:17 myfile.txt
[zsy@VM-0-8-centos test]$ cp *.txt dir
[zsy@VM-0-8-centos test]$ ll
total 12
drwxrwxr-x 2 zsy zsy 4096 Jul 15 11:22 dir
-rw-rw-r-- 1 zsy zsy    7 Jul 15 11:18 myfile-backup.txt
-rw-rw-r-- 1 zsy zsy    7 Jul 15 11:17 myfile.txt
[zsy@VM-0-8-centos test]$ tree dir
dir
|-- myfile-backup.txt
`-- myfile.txt

*是通配符,在 cp *.txt dir中匹配所有以.txt结尾的文件

cp 目标文件如果存在,就覆盖

shell 复制代码
[zsy@VM-0-8-centos test]$ echo 'hello' > myfile.txt
[zsy@VM-0-8-centos test]$ cat myfile.txt 
hello
[zsy@VM-0-8-centos test]$ cp myfile.txt myfile-backup.txt 
[zsy@VM-0-8-centos test]$ cat myfile-backup.txt 
hello

>重定向:

  • 如果后续文件不存在,则新建文件
  • 先清空再写入
    可以使用 > 文件名创建普通文件或清空文件

>>追加重定向

<输入重定向

拷贝前询问

shell 复制代码
[zsy@VM-0-8-centos test]$ cp -i myfile.txt myfile-backup.txt 
cp: overwrite 'myfile-backup.txt'? y

递归强制拷贝整个目录

shell 复制代码
[zsy@VM-0-8-centos ~]$ tree test/
test/
|-- dir
|   |-- myfile-backup.txt
|   `-- myfile.txt
|-- myfile-backup.txt
`-- myfile.txt

1 directory, 4 files
[zsy@VM-0-8-centos ~]$ cp -rf test test-backup
[zsy@VM-0-8-centos ~]$ tree test-backup
test-backup
|-- dir
|   |-- myfile-backup.txt
|   `-- myfile.txt
|-- myfile-backup.txt
`-- myfile.txt

1 directory, 4 files
  • Linux 中一切皆文件
  • 输入输出操作本质是文件操作
  • 文件操作修改文件时,文件必须先被打开,所以设备文件访问前也必须先打开
  • 程序启动时,系统会自动打开 stdinstdoutstderr三个文件,分别是 键盘(标准输入)、显示器(标准输出)、显示器(标准错误)
  • 终端的本质也是 Linux 中的一个文件:/dev/pts/xxx

常用文件类型

  • -普通文件
  • d目录文件
  • c字符文件:键盘、显示器、终端(输入的数据具有顺序性)
  • b块设备文件:磁盘
  • l链接文件
  • p管道文件

在 Linux 中不关心文件后缀,文本、exe、库、图片、视频都是普通文件,但不代表 Linux 禁止使用后缀。一些工具(如gcc)关心后缀。

11. mv

mv命令是 move的缩写,可以用来移动文件将文件改名(move(rename)files,经常用来备份文件或目录)

语法

mv [选项] 源文件或目录 目标文件或目录

功能

  1. mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或将其移至一个新的目录中
  2. 当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名
  3. 当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的文件均移至目标目录中

常用选项

  • -fforce强制的意思,如果目标文件已经存在,不会询问而是直接覆盖
  • -i若目标文件(destination)已经存在时,就会询问是否覆盖

举例

更改名称

myfile.txt改名为file.txt

shell 复制代码
[zsy@VM-0-8-centos test]$ touch myfile.txt
[zsy@VM-0-8-centos test]$ ll
total 0
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:03 myfile.txt
[zsy@VM-0-8-centos test]$ mv myfile.txt file.txt
[zsy@VM-0-8-centos test]$ ll
total 0
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:03 file.txt

如果当前路径存在同名文件,改名即覆盖

file.txt改名为myfile.txt,结果将原有的myfile覆盖

shell 复制代码
[zsy@VM-0-8-centos test]$ touch myfile.txt
[zsy@VM-0-8-centos test]$ ll
total 0
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:03 file.txt
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:04 myfile.txt
[zsy@VM-0-8-centos test]$ mv file.txt myfile.txt 
[zsy@VM-0-8-centos test]$ ll
total 0
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:03 myfile.txt

如果当前路径存在同名文件,改名前可以添加 -i 选项,让系统提出警告供用户做出选择

file.txt改名为myfile.txt,由于已存在myfile.txt,并且mv语句中添加了-i选项,系统提出警告mv: overwrite 'myfile.txt'?

shell 复制代码
[zsy@VM-0-8-centos test]$ touch file.txt
[zsy@VM-0-8-centos test]$ ll
total 0
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:07 file.txt
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:03 myfile.txt
[zsy@VM-0-8-centos test]$ mv -i file.txt myfile.txt 
mv: overwrite 'myfile.txt'? y
[zsy@VM-0-8-centos test]$ ll
total 0
-rw-rw-r-- 1 zsy zsy 0 Jul 17 10:07 myfile.txt

mv整个目录

temp目录mv到上级目录

shell 复制代码
[zsy@VM-0-8-centos test]$ touch myfile.txt
[zsy@VM-0-8-centos test]$ mkdir temp
[zsy@VM-0-8-centos test]$ ll
total 4
-rw-rw-r-- 1 zsy zsy    0 Jul 17 10:09 myfile.txt
drwxrwxr-x 2 zsy zsy 4096 Jul 17 10:09 temp
[zsy@VM-0-8-centos test]$ mv myfile.txt temp
[zsy@VM-0-8-centos test]$ ls
temp
[zsy@VM-0-8-centos test]$ mv temp ../
[zsy@VM-0-8-centos test]$ ls
[zsy@VM-0-8-centos test]$ ls -d ../temp
../temp

12. cat

语法

cat [选项] [文件]

功能

查看目标文件的内容

常用选项

  • -b对非空行输出编号,空行不做编号
  • -n对输出的所有行编号
  • -s不输出多行空行(多行空行合并为一行)

cat默认将文件内容向显示器打印(适合短文件)

tac命令可以反向打印文件内容

举例

命令行构建多行文本

shell 复制代码
[zsy@VM-0-8-centos test]$ cnt=0; while [ $cnt -le 20 ]; do echo "hello $cnt"; let cnt++; done > temp.txt

测试 cat 基本命令

shell 复制代码
[zsy@VM-0-8-centos test]$ cat temp.txt 
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
hello 11
hello 12
hello 13
hello 14
hello 15
hello 16
hello 17
hello 18
hello 19
hello 20

cat 输出携带行号

shell 复制代码
[zsy@VM-0-8-centos test]$ cat -b temp.txt 
     1	hello 0
     2	hello 1
     3	hello 2
     4	hello 3
     5	hello 4
     6	hello 5
     7	hello 6
     8	hello 7
     9	hello 8
    10	hello 9
    11	hello 10
    12	hello 11
    13	hello 12
    14	hello 13
    15	hello 14
    16	hello 15
    17	hello 16
    18	hello 17
    19	hello 18
    20	hello 19
    21	hello 20

修改 temp.txt使其携带多行空行

shell 复制代码
[zsy@VM-0-8-centos test]$ vim temp.txt 

测试 -b对非空行进行编号

shell 复制代码
[zsy@VM-0-8-centos test]$ cat -b temp.txt 
     1	hello 0
     2	hello 1
     3	hello 2






     4	hello 3
     5	hello 4
     6	hello 5



     7	hello 6
     8	hello 7
     9	hello 8
    10	hello 9
    11	hello 10
    12	hello 11
    13	hello 12
    14	hello 13
    15	hello 14
    16	hello 15
    17	hello 16
    18	hello 17
    19	hello 18
    20	hello 19
    21	hello 20

测试 -n对所有行编号

shell 复制代码
[zsy@VM-0-8-centos test]$ cat -n temp.txt 
     1	hello 0
     2	hello 1
     3	hello 2
     4	
     5	
     6	
     7	
     8	
     9	
    10	hello 3
    11	hello 4
    12	hello 5
    13	
    14	
    15	
    16	hello 6
    17	hello 7
    18	hello 8
    19	hello 9
    20	hello 10
    21	hello 11
    22	hello 12
    23	hello 13
    24	hello 14
    25	hello 15
    26	hello 16
    27	hello 17
    28	hello 18
    29	hello 19
    30	hello 20

测试 -s不输出多行空行,把多行空行合并成一行空行

shell 复制代码
[zsy@VM-0-8-centos test]$ cat -s temp.txt 
hello 0
hello 1
hello 2

hello 3
hello 4
hello 5

hello 6
hello 7
hello 8
hello 9
hello 10
hello 11
hello 12
hello 13
hello 14
hello 15
hello 16
hello 17
hello 18
hello 19
hello 20

13. more

语法

more [选项] 文件

功能

查看文件内容,默认打满全屏,使用 Enter 可以逐行下翻(不能向上翻)

常用选项

  • -n指定输出行数
  • q退出more
  • /搜索字符串

举例

命令行输出多行文本

shell 复制代码
[zsy@VM-0-8-centos test]$ cnt=0; while [ $cnt -le 1000 ]; do echo "hello $cnt"; let cnt++; done > temp.txt

指定输出行数

shell 复制代码
[zsy@VM-0-8-centos test]$ more -10 temp.txt 
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
--More--(0%)

14. less

语法

less [选项] 文件

功能

lessmore类似,但使用 less可以随意浏览文件,而 more只能向前移动,不能向后移动,且 less 在查看之前不会加载整个文件

选项

  • -i忽略搜索时的大小写
  • -N显示每行的行号
  • /字符串 向下搜索"字符串"
  • ?字符串 向上搜索"字符串"
  • n重复前一个搜索
  • N反向重复前一个搜索
  • qquit退出

15. head

headtail用来显示开头或结尾某个数量的文字区块,head将文件开头输出至标准输出中,tail将文件结尾输出至标准输出中

语法

head [参数]... [文件]...

功能

head用来显示档案的开头至标准输出中,默认head命令打印其响应文件的开头10行

选项

  • -n<行数>显示的行数

举例

shell 复制代码
[zsy@VM-0-8-centos test]$ head temp.txt 
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
shell 复制代码
[zsy@VM-0-8-centos test]$ head -5 temp.txt 
hello 0
hello 1
hello 2
hello 3
hello 4

16. tail

tail命令从指定点开始将⽂件写到标准输出.使⽤tail命令的-f选项可以⽅便的查阅正在改变的⽇志⽂件,tail -f filename会把filename⾥最尾部的内容显⽰在屏幕上,并且不断刷新,使你看到最新的⽂件内容

语法

tail 必要参数 [文件]

功能

用于显示指定文件末尾内容,不指定文件时作为输入信息进行处理。常用查看日志文件。

选项

  • -f循环读取
  • -n<行数>显示行数

举例

基本功能

shell 复制代码
[zsy@VM-0-8-centos test]$ tail temp.txt 
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
shell 复制代码
[zsy@VM-0-8-centos test]$ tail -3 temp.txt 
hello 998
hello 999
hello 1000

显示文件 180, 200 行的内容

shell 复制代码
[zsy@VM-0-8-centos test]$ head -200 temp.txt | tail -20
hello 180
hello 181
hello 182
hello 183
hello 184
hello 185
hello 186
hello 187
hello 188
hello 189
hello 190
hello 191
hello 192
hello 193
hello 194
hello 195
hello 196
hello 197
hello 198
hello 199

|是管道,管道左边的指令经过管道交给右边指令

17. date

指定格式显示时间

date +%Y:%m:%d

用法

date [OPTION]... [+FORMAT]

  1. 试用者可以设定欲显示的格式,格式设定为一个加号就接数个标记,其中常用的标记列表如下
  • %H小时
  • %M分钟
  • %S
  • %X相当于 %H:%M:%S
  • %d
  • %m月份
  • %Y完整年份
  • %F相当于 %Y-%m-%d
  1. 在设定时间方面

date -s// 设置当前时间,只有 root 权限才能设置,其他只能查看

date -s 20051003// 设置成 20051003,这样会把具体时间设置成空00:00:00

date -s 01:01:01// 设置具体时间,不会对日期进行更改

date -s "01:01:01 2005-10-03"// 这样可以设置全部时间

date -s "01:01:01 20051003"// 这样可以设置全部时间

date -s "2005-10-03 01:01:01"// 这样可以设置全部时间

date -s "20051003 01:01:01"// 这样可以设置全部时间

  1. 时间戳

时间 -> 时间戳 :date +%s

时间戳 -> 时间 :date -d@时间戳

时间戳是指从1970年1月1日开始所经过的秒数(不考虑闰秒)

举例

显示常规时间

shell 复制代码
[zsy@VM-0-8-centos test]$ date +%Y:%m:%d
2026:07:17
[zsy@VM-0-8-centos test]$ date -d@0
Thu Jan  1 08:00:00 CST 1970
[zsy@VM-0-8-centos test]$ date
Fri Jul 17 14:12:56 CST 2026
[zsy@VM-0-8-centos test]$ date +%Y/%m/%d
2026/07/17
[zsy@VM-0-8-centos test]$ date +%Y/%m/%d-%H:%M:%S
2026/07/17-14:13:26

显示时间戳

shell 复制代码
[zsy@VM-0-8-centos test]$ date +%s
1784268855

时间戳转换为时间

shell 复制代码
[zsy@VM-0-8-centos test]$ date +%Y/%m/%d-%H:%M:%S -d @0
1970/01/01-08:00:00
[zsy@VM-0-8-centos test]$ date +%Y/%m/%d-%H:%M:%S -d @100000
1970/01/02-11:46:40

18. cal

cal命令可以用来显示公历(阳历)日历

命令格式

cal 参数 [年份]

功能

⽤于查看日历等时间信息,如只有⼀个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份

常用选项

  • -3显示系统前一个月,当前月,下一个月的月历
  • -j显示在当年中的第几天
  • -y显示当前年份的日历

举例

shell 复制代码
[zsy@VM-0-8-centos test]$ cal
      July 2026     
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
shell 复制代码
[zsy@VM-0-8-centos test]$ cal 2005
                               2005                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31
shell 复制代码
[zsy@VM-0-8-centos test]$ cal -3
      June 2026             July 2026            August 2026    
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6            1  2  3  4                     1
 7  8  9 10 11 12 13   5  6  7  8  9 10 11   2  3  4  5  6  7  8
14 15 16 17 18 19 20  12 13 14 15 16 17 18   9 10 11 12 13 14 15
21 22 23 24 25 26 27  19 20 21 22 23 24 25  16 17 18 19 20 21 22
28 29 30              26 27 28 29 30 31     23 24 25 26 27 28 29
                                            30 31         

19. find

Linuxfind命令在目录结构下搜索文件,并执行指定的操作

语法

find pathname -options

功能

用于在文件树中查找文件,并做出相应的处理

常用选项

  • -name按照文件名查找文件

20. which

功能

搜索系统指定的命令

举例

shell 复制代码
[zsy@VM-0-8-centos test]$ which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[zsy@VM-0-8-centos test]$ which pwd
/usr/bin/pwd

21. whereis

功能

用于找到程序的源、二进制文件或手册

shell 复制代码
[zsy@VM-0-8-centos test]$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[zsy@VM-0-8-centos test]$ whereis libc.so
libc: /usr/lib64/libc.so /usr/lib64/libc.a /usr/share/man/man7/libc.7.gz

22. grep

行文本过滤工具

语法

grep [选项] 搜寻字符串 文件

功能

在文件中搜索字符串,将找到的行打印出来

常用选项

  • -i忽略大小写
  • -n输出带行号
  • -v反向选择,即显示出不包含"搜寻字符串"的行

23. 打包/压缩

什么是 打包/压缩

打包:文件合并

压缩:减小体积

为什么要进行 打包/压缩

打包:防止文件丢失

压缩:①减小体积,节省存储空间②在网络传送可以有效减少网络传送的时间

如何在Linux中进行打包/压缩(两种常用命令)

zip/unzip

语法

zip 压缩文件.zip 目录或文件

功能

将目录或文件压缩成zip格式

常用选项
  • -r 递归处理,将指定目录下的所有文件和子目录一并处理
举例

zip -r dst.zip src

unzip xxx.zip -d 指定目录

将 test2 目录压缩: zip test2.zip test2/*

解压到tmp目录:unzip test2.zip -d /tmp

解压到指定目录,如果目标目录是当前目录,可以省略目标目录

tar

打包/解包

语法

tar [选项] 文件与目录

选项
  • -c建立一个压缩文件(create)
  • -x解开一个压缩文件
  • -t查看一个 tarfile 里面的文件
  • -z同时具有 gzip 的属性
  • -j同时具有 bzip2 的属性
  • -v压缩过程中显示文件
  • -f使用档名
  • -C解压到指定目录
使用

打包: tar czf xxx.tgz src

解包: tar xzf xxx.tgz -C 指定目录

rzsz工具

这个工具用于 windows机器和远端的 Linux机器通过 XShell传输文件。安装后可以通过拖拽的方式将 windows的文件传入 Linux

安装

sudo yum/apt install -y lrzsz

sz 文件名将文件发送到 Windows

rz发送文件到 Linux

24. bc

bc 命令可以进行浮点运算(相当于计算器)

echo "..." | bc也可以进行计算

uname -r

语法

uname [选项]

功能

uname用来获取电脑和操作系统的基本信息

可以显示Linux主机所用的操作系统的版本、硬件的名称等基本信息

常用选项

  • -a-all详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称

lsb_release -a查看操作系统版本

25. 常用快捷键

  1. ctrl + C在命令行中,如果出现异常情况,可以尝试使用 ctrl + C来终止
  2. 可以使用上下键寻找历史命令
  3. Tab快速按两下,可以进行命令搜索命令补全
  4. ctrl + r搜素历史命令,会自动匹配到历史命令
  5. ctrl + d退出当前用户(相当于exit)

26. 以下命令作为扩展

  • 安装和登录命令:login、shutdown、halt、reboot、install、mount、umount、cash、exit、last;
  • 文件处理指令:file、mkdir、grep、dd、find、mv、ls、diff、cat、ln;
  • 系统管理相关指令:df、top、free、quota、at、lp、adduser、groupadd、kill、crontab;
  • 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、nslookup;
  • 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who;
  • 其他命令:tar、unzip、gunzip、unarj、mtools、man、unendcode、uudecode
相关推荐
砍材农夫20 小时前
运维|devops|nginx|基本配置拆解
运维·nginx·devops
糖果店的幽灵20 小时前
【langgraph 从入门到精通graphApi 篇】Memory 与长期记忆
运维·服务器·人工智能·langgraph
phltxy1 天前
LangChain从模型输出到RAG数据管道实战
服务器·人工智能·深度学习·语言模型·langchain
我星期八休息1 天前
网络编程—应用层HTTP协议
linux·运维·开发语言·前端·网络·网络协议·http
一个天蝎座 白勺 程序猿1 天前
从电网改造踩坑说起:深度拆解时序大模型TimechoAI的自主可控与安全合规底气
大数据·运维·服务器·大模型·timechoai
ShineWinsu1 天前
对于Linux:UDPsocket编程基础的解析
linux·运维·网络协议·udp·ip·端口号·进程间通信
hehelm1 天前
AI大模型接入SDK—通用模块设计
linux·开发语言·c++
Spider Cat 蜘蛛猫1 天前
Anthropic的skill-creator使用分享
运维·服务器
liulilittle1 天前
论分布式系统的半开闭问题
服务器·网络·分布式·系统架构·竞态