作者:傻响
专栏:《Linux下常用的指令》
格言:你只管努力,剩下的交给时间!
目录
Linux下基本指令
1.ls 指令
2.pwd 指令
3.cd 指令
4.tree的安装
5.whoami指令
6.touch指令
7.stat指令
8.mkdir指令(重要)
9.rmdir指令 && rm 指令(重要):
10.man指令(重要)
11.cp指令(重要)
12.mv指令(重要)
13 cat 指令
14 tac 指令
15.more指令
15.less指令(重要)
16.head指令
17.tail指令
18.时间相关的指令
19.Cal指令
20.find指令:(灰常重要) -name
21.grep指令
22.wc 命令
23.sort命令
24.uniq去重命令
25.zip/unzip指令
26.alias指令
27.tar指令(重要):打包/解包,不打开它直接看内容
28.bc指令
29.uname –r指令
30.重要的几个热键[Tab],[ctrl]-c, [ctrl]-d
31.关机
32.以下命令作为扩展
语法: 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 一行只输出一个文件。
测试/ 演示:
// ls 显示当前路径下的文件或者目录名
[root@VM-8-15-centos /]# ls // ls 命令
bin data etc lib lost+found mnt proc run srv tmp var
boot dev home lib64 media opt root sbin sys usr
// ls -l 显示当前路径下的文件或者目录的更详细的信息
[root@VM-8-15-centos /]# ls -l // ls -l命令
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
//权限 所属用户 大小(byte) 最近修改时间 文件名
dr-xr-xr-x. 5 root root 4096 Sep 7 10:39 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3020 Sep 19 01:53 dev
drwxr-xr-x. 95 root root 12288 Sep 19 01:32 etc
drwxr-xr-x. 3 root root 4096 Sep 7 10:39 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Sep 7 10:40 opt
dr-xr-xr-x 98 root root 0 Sep 19 01:53 proc
dr-xr-x---. 6 root root 4096 Sep 19 01:52 root
drwxr-xr-x 25 root root 880 Sep 19 01:54 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Sep 19 22:35 sys
drwxrwxrwt. 8 root root 4096 Sep 19 03:30 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
// ls -a 列出目录下的所有文件,包括以 . 开头的隐含文件。
[root@VM-8-15-centos /]# ls -a -l // ls -a + ls -l命令
dr-xr-xr-x. 19 root root 4096 Sep 19 23:27 .
dr-xr-xr-x. 19 root root 4096 Sep 19 23:27 ..
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Sep 7 10:39 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3020 Sep 19 01:53 dev
drwxr-xr-x. 95 root root 12288 Sep 19 01:32 etc
drwxr-xr-x. 3 root root 4096 Sep 7 10:39 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Sep 7 10:40 opt
dr-xr-xr-x 98 root root 0 Sep 19 01:53 proc
dr-xr-x---. 6 root root 4096 Sep 19 23:03 root
drwxr-xr-x 25 root root 880 Sep 19 01:54 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Sep 19 22:35 sys
drwxrwxrwt. 8 root root 4096 Sep 19 03:30 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
[root@VM-8-15-centos /]# ls -al // ls -a + ls -l命令
dr-xr-xr-x. 19 root root 4096 Sep 19 23:27 .
dr-xr-xr-x. 19 root root 4096 Sep 19 23:27 ..
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Sep 7 10:39 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3020 Sep 19 01:53 dev
drwxr-xr-x. 95 root root 12288 Sep 19 01:32 etc
drwxr-xr-x. 3 root root 4096 Sep 7 10:39 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Sep 7 10:40 opt
dr-xr-xr-x 98 root root 0 Sep 19 01:53 proc
dr-xr-x---. 6 root root 4096 Sep 19 23:03 root
drwxr-xr-x 25 root root 880 Sep 19 01:54 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Sep 19 22:35 sys
drwxrwxrwt. 8 root root 4096 Sep 19 03:30 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
// 显示所有文件包括隐藏文件
[root@VM-8-15-centos ~]# touch .file // .file 是隐藏文件
[root@VM-8-15-centos ~]# mkdir .mfile // .mfile 是隐藏目录
[root@VM-8-15-centos ~]# ls -l // ls -l不能显示隐藏文件
total 0
[root@VM-8-15-centos ~]# ls -l -a // ls -a可以显示隐藏文件
total 60
dr-xr-x---. 7 root root 4096 Sep 19 23:31 .
dr-xr-xr-x. 19 root root 4096 Sep 19 23:31 ..
-rw------- 1 root root 3479 Sep 19 23:31 .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 3 root root 4096 Sep 7 11:09 .cache
drwxr-xr-x 3 root root 4096 Sep 7 11:09 .config
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r-- 1 root root 0 Sep 19 23:30 .file
drwxr-xr-x 2 root root 4096 Sep 19 23:31 .mfile
drwxr-xr-x 2 root root 4096 Jul 25 17:53 .pip
-rw-r--r-- 1 root root 73 Sep 7 10:39 .pydistutils.cfg
drwx------ 2 root root 4096 Sep 7 10:39 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw------- 1 root root 920 Sep 19 01:52 .viminfo
// 补充:一个点称为当前目录,两个点的称为上一级目录。
// ll命令
[root@VM-8-15-centos /]# ll // ll 相当于ls -l
total 72
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Sep 7 10:39 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3020 Sep 19 01:53 dev
drwxr-xr-x. 95 root root 12288 Sep 19 01:32 etc
drwxr-xr-x. 3 root root 4096 Sep 7 10:39 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Sep 7 10:40 opt
dr-xr-xr-x 99 root root 0 Sep 19 01:53 proc
dr-xr-x---. 6 root root 4096 Sep 19 23:33 root
drwxr-xr-x 25 root root 880 Sep 19 01:54 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Sep 19 22:35 sys
drwxrwxrwt. 8 root root 4096 Sep 19 03:30 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
// ls -ld 不进入目录,直接显示目录
[root@VM-8-15-centos /]# ls // ls 命令
bin data etc lib lost+found mnt proc run srv tmp var
boot dev home lib64 media opt root sbin sys usr
[root@VM-8-15-centos /]# ls -l root // ls -l命令
total 4
drwxr-xr-x 2 root root 4096 Sep 19 23:40 dir
-rw-r--r-- 1 root root 0 Sep 19 23:39 file
[root@VM-8-15-centos /]# ls -ld root // ls -ld命令
dr-xr-x---. 7 root root 4096 Sep 19 23:40 root
// ls-R 列出所有子目录下的文件。 (递归)
这里就不进行演示了,就是递归显示目录下的所有的文件。
语法: pwd 功能:显示用户当前所在的目录 常用选项: 无
测试/ 演示:
[root@VM-8-15-centos home]# pwd // pwd命令
/home // 当前路径
Linux系统中,磁盘上的文件和目录被组成一棵目录树,每个节点都是目录或文件。
语法:cd 目录名 功能:改变工作目录。将当前工作目录改变到指定的目录下。 测试/ 演示:
cd .. // 返回上级目录
cd /home/litao/linux/ // 绝对路径
cd ../day02/ // 相对路径
cd ~ // 进入用户家目
cd - // 返回最近访问目录
// 需要安装 - tree命令
[root@VM-8-15-centos /]# sudo yum install -y tree
测试/ 演示:
[root@VM-8-15-centos ~]# tree // tree命令
.
|-- dir
| `-- file.txt
`-- file.txt
1 directory, 2 files
// 查看当前用户
[root@VM-8-15-centos ~]# whoami //whoami
root
语法:touch [选项]... 文件... (普通文件) 功能: touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件。 常用选项:
-a 或--time=atime或--time=access或--time=use只更改存取时间。
-c 或--no-create 不建立任何文档。
-d 使用指定的日期时间,而非现在的时间。
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m 或--time=mtime或--time=modify 只更改变动时间。
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t 使用指定的日期时间,而非现在的时间 。
测试/ 演示:
[root@VM-8-15-centos ~]# touch file // touch 创建普通文件
[root@VM-8-15-centos ~]# ls // 查看
file
// 补充touch不光可以创建普通的文件,它还会更新文件时间。
// 创建文件的各种方式:
[root@VM-8-14-centos lesson]# echo "" > file1.txt
[root@VM-8-14-centos lesson]# > file2.txt
获取文件的属性时间
[root@VM-8-15-centos ~]# stat file // 获取这个文件的时间
File: ‘file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 393550 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-09-20 00:29:36.550895834 +0800 // 时间:
Modify: 2022-09-20 00:29:36.550895834 +0800 // 时间:
Change: 2022-09-20 00:29:36.550895834 +0800 // 时间:
Birth: -
语法: mkdir [选项] dirname... 功能:在当前目录下创建一个名为 “dirname”的目录 常用选项:
-p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立 好那些尚不存在的目录,即一次可以建立多个目录;
// 创建一个目录
[root@VM-8-15-centos ~]# mkdir dir // midir 指令
[root@VM-8-15-centos ~]# ls
dir file
// 递归的创建一个目录
[root@VM-8-15-centos ~]# ls // ls 指令
dir file
[root@VM-8-15-centos ~]# mkdir -p a/b/c // mkdir -p 指令 递归创建目录
[root@VM-8-15-centos ~]# tree // tree指令
.
|-- a
| `-- b
| `-- c
|-- dir
`-- file
4 directories, 1 file
rmdir是一个与mkdir相对应的命令。 mkdir是建立目录,而rmdir是删除命令。 语法: rmdir -p 适用对象:具有当前目录操作权限的所有使用者 功能:删除空目录 常用选项 :
-p 当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除。
[root@VM-8-14-centos lesson]# tree
.
├── empty // 要删除的目录
├── empty1 // 要删除的目录
│ └── empty2
│ └── empty3
├── empty3
│ └── empty4
│ └── empty5
│ └── empty7
├── my.txt
└── my.txt.c
8 directories, 2 files
[root@VM-8-14-centos lesson]# rmdir empty1 // 无法删除不为空的目录
rmdir: failed to remove ‘empty1’: Directory not empty
[root@VM-8-14-centos lesson]# rmdir empty // 只能删除空目录
[root@VM-8-14-centos lesson]# tree
.
├── empty1 // empty被删除了
│ └── empty2
│ └── empty3
├── empty3
│ └── empty4
│ └── empty5
│ └── empty7
├── my.txt
└── my.txt.c
7 directories, 2 files
rm命令可以同时删除文件或目录 语法: rm -f-i-r-v 适用对象:所有使用者 功能:删除文件或目录
常用选项:
-f 即使文件属性为只读(即写保护),亦直接删除
-i 删除前逐一询问确认
-r 删除目录及其下所有文件
[root@VM-8-14-centos lesson]# tree
.
├── empty
├── empty1
│ └── empty2
└── fail.txt
[root@VM-8-14-centos lesson]# rm fail.txt // rm删除普通文件
rm: remove regular empty file ‘fail.txt’? y
[root@VM-8-14-centos lesson]# rm -r empty // rm删除目录
rm: remove directory ‘empty’? y
[root@VM-8-14-centos lesson]# rm -fr empty1 // rm -rf 没有提示强制删除目录
Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是man
语法: man [选项] 命令 常用选项:
-k 根据关键字搜索联机帮助
num 只在第num章节找
-a 将所有章节的都显示出来,比如 man printf 它缺省从第一章开始搜索,知道就停止,用a选项,当按 下q退出,他会继续往后面搜索,直到所有章节都搜索完毕。
解释一下 - 面手册分为8章
1 是普通的命令。
2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)。
3 是库函数,如printf,fread4是特殊文件,也就是/dev下的各种设备文件。
5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义。
6 是给游戏留的,由各个游戏自己定义。
7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明。
8 是系统管理用的命令,这些命令只能由root使用,如ifconfig 。
man的安装:
yum install -y man-pages // 安装命令。
语法: cp [选项] 源文件或目录 目标文件或目录 功能: 复制文件或目录 说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录, 则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存 在的目录,则会出现错误信息。 常用选项: -f 或 --force 强行复制文件或目录, 不论目的文件或目录是否已经存在 -i 或 --interactive 覆盖文件之前先询问用户 -r递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链 接,则一律视为普通文件处理 -R 或 --recursive递归处理,将指定目录下的文件及子目录一并处理。
[root@VM-8-14-centos lesson]# echo "Hello Linux" > fail.txt // 重定向。
// echo向显示器显示输出,与printf不同的是printf是以格式化的方式进行显示。
[root@VM-8-14-centos lesson]# tree
.
├── cp_dir
└── fail.txt
[root@VM-8-14-centos lesson]# cp fail.txt cp_dir/ // cp拷贝文件到目录下。
[root@VM-8-14-centos lesson]# tree
.
├── cp_dir
│ └── fail.txt
└── fail.txt
// 拷贝目录
[root@VM-8-14-centos lesson]# ls
cp_dir fail.txt file1.txt file2.txt // 当前目录
[root@VM-8-14-centos lesson]# ls ..
lesson // 上级目录
[root@VM-8-14-centos lesson]# cp fail.txt .. // 拷贝文件到上级目录。
[root@VM-8-14-centos lesson]# cp -r cp_dir/ .. // 拷贝目录到上级目录
[root@VM-8-14-centos lesson]# ls ..
cp_dir fail.txt lesson
// 带提示的拷贝
[root@VM-8-14-centos lesson]# cp -i file1.txt .. // 带提示的拷贝。
cp: overwrite ‘../file1.txt’? y
[root@VM-8-14-centos lesson]# ls ..
cp_dir fail.txt file1.txt lesson
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。 语法: mv [选项] 源文件或目录 目标文件或目录 功能:
视mv命令中第二个参数类型的不同(是目标文件还是目标目录), mv命令将文件重命名或将其移至一个新的 目录中。
当第二个参数类型是文件时, mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它 将所给的源文件或目录重命名为给定的目标文件名。
当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个, mv命令将各参数指定的源文件均移至 目标目录中。
常用选项:
-f : force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
[root@VM-8-14-centos lesson]# ls
cp_dir file.txt mv_test
[root@VM-8-14-centos lesson]# tree
.
├── cp_dir
│ └── fail.txt
├── file.txt
└── mv_test
2 directories, 2 files
[root@VM-8-14-centos lesson]# mv file.txt mv_test/ // 将当前目录下的文件移动到mv_test目录下
[root@VM-8-14-centos lesson]# mv cp_dir/ mv_test/ // 将当前目录下的目录移动到mv_test目录下
[root@VM-8-14-centos lesson]# tree
.
└── mv_test
├── cp_dir
│ └── fail.txt
└── file.txt
// 使用mv命令对文件或者目录进行重命名
[root@VM-8-14-centos lesson]# ls
file mv_test
[root@VM-8-14-centos lesson]# mv file file.txt // mv 重命名操作
[root@VM-8-14-centos lesson]# ls
file.txt mv_test
语法: cat 选项 功能: 查看目标文件的内容 常用选项:
-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行
// shell的一段脚本
[root@VM-8-14-centos lesson]# cnt=0; while [ $cnt -le 10000 ] ; do echo "Hello ShaXiang [$cnt]"; let cnt++; done > file.txt // 往文件中写10000行数据。
[root@VM-8-14-centos lesson]# cat file.txt // 查看信息
Hello ShaXiang [9996]
Hello ShaXiang [9997]
Hello ShaXiang [9998]
Hello ShaXiang [9999]
Hello ShaXiang [10000]
[root@VM-8-14-centos lesson]# cat -n file.txt // 代行号查看数据
9997 Hello ShaXiang [9996]
9998 Hello ShaXiang [9997]
9999 Hello ShaXiang [9998]
10000 Hello ShaXiang [9999]
10001 Hello ShaXiang [10000]
// cat命令不适合查看比较大的文件
语法: tac 选项 功能: 查看目标文件的内容
// 与cat正好相反。
Hello ShaXiang [10]
Hello ShaXiang [9]
Hello ShaXiang [8]
Hello ShaXiang [7]
Hello ShaXiang [6]
Hello ShaXiang [5]
Hello ShaXiang [4]
Hello ShaXiang [3]
Hello ShaXiang [2]
Hello ShaXiang [1]
Hello ShaXiang [0]
语法: more 选项 功能: more命令,功能类似 cat 常用选项:
-n 对输出的所有行编号
q 退出more
[root@VM-8-14-centos lesson]# more file.txt
// 内容显示完屏幕位置,按回车键会继续向下移动显示数据,按下q退出。
// more查看固定行数
[root@VM-8-14-centos lesson]# more -10 file.txt
Hello ShaXiang [0]
Hello ShaXiang [1]
Hello ShaXiang [2]
Hello ShaXiang [3]
Hello ShaXiang [4]
Hello ShaXiang [5]
Hello ShaXiang [6]
Hello ShaXiang [7]
Hello ShaXiang [8]
Hello ShaXiang [9]
--More--(0%)
[root@VM-8-14-centos lesson]# more -1000 file.txt // 查看指定的行
// 还差查看一些内容:
// 输入 /1234,会自动找到1234相关的内容
。
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极 其强大。
less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看但若使用了
less 时,就可以使用 pageup 等按键的功能来往前往后翻看文件,更容易用来查看一个文件的内容!
除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜 。
语法: less [参数] 文件 功能: less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前 不会加载整个文件。 选项:
-i 忽略搜索时的大小写
-N 显示每行的行号
/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
n:重复前一个搜索(与 / 或 ? 有关)
N:反向重复前一个搜索(与 / 或 ? 有关)
q:quit
// 直接显示文件内容。
[root@VM-8-14-centos lesson]# less file.txt
Hello ShaXiang [0]
Hello ShaXiang [1]
Hello ShaXiang [2]
Hello ShaXiang [3]
Hello ShaXiang [4]
Hello ShaXiang [5]
Hello ShaXiang [6]
Hello ShaXiang [7]
Hello ShaXiang [8]
Hello ShaXiang [9]
// 带行号显示文件内容。
[root@VM-8-14-centos lesson]# less -N file.txt
1 Hello ShaXiang [0]
2 Hello ShaXiang [1]
3 Hello ShaXiang [2]
4 Hello ShaXiang [3]
5 Hello ShaXiang [4]
6 Hello ShaXiang [5]
7 Hello ShaXiang [6]
8 Hello ShaXiang [7]
9 Hello ShaXiang [8]
10 Hello ShaXiang [9]
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块, head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾。 语法: head [参数]... [文件]... 功能: head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。 选项:
-n<行数> 显示的行数
[root@VM-8-14-centos lesson]# head -3 file.txt // 指定要查看的行数。
Hello ShaXiang [0]
Hello ShaXiang [1]
Hello ShaXiang [2]
[root@VM-8-14-centos lesson]# head file.txt // 默认查看行数。
Hello ShaXiang [0]
Hello ShaXiang [1]
Hello ShaXiang [2]
Hello ShaXiang [3]
Hello ShaXiang [4]
Hello ShaXiang [5]
Hello ShaXiang [6]
Hello ShaXiang [7]
Hello ShaXiang [8]
Hello ShaXiang [9]
tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容. 语法: tail必要参数[文件] 功能: 用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。 选项:
f 循环读取。
-n<行数> 显示行数 。
[root@VM-8-14-centos lesson]# tail -3 file.txt // 指定查看文件末尾的3行
Hello ShaXiang [9998]
Hello ShaXiang [9999]
Hello ShaXiang [10000]
[root@VM-8-14-centos lesson]# tail file.txt // 默认查看文件内容
Hello ShaXiang [9991]
Hello ShaXiang [9992]
Hello ShaXiang [9993]
Hello ShaXiang [9994]
Hello ShaXiang [9995]
Hello ShaXiang [9996]
Hello ShaXiang [9997]
Hello ShaXiang [9998]
Hello ShaXiang [9999]
Hello ShaXiang [10000]
// 拿到中间的行内容 - 这个涉及到了管道(后面在了解)
[root@VM-8-14-centos lesson]# head -1010 file.txt | tail -10
Hello ShaXiang [1000]
Hello ShaXiang [1001]
Hello ShaXiang [1002]
Hello ShaXiang [1003]
Hello ShaXiang [1004]
Hello ShaXiang [1005]
Hello ShaXiang [1006]
Hello ShaXiang [1007]
Hello ShaXiang [1008]
Hello ShaXiang [1009]
// 加料 - 使用管道拿到中间10行,进行逆置,在拿头三行
[root@VM-8-14-centos lesson]# head -1010 file.txt | tail -10 | tac | head -3
Hello ShaXiang [1009]
Hello ShaXiang [1008]
Hello ShaXiang [1007]
date显示 date 指定格式显示时间: date +%Y:%m:%d date 用法: 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
[root@VM-8-14-centos lesson]# date // 打印时间
Sat Oct 1 17:24:49 CST 2022
[root@VM-8-14-centos lesson]# date +%Y/%m/%d/%H/%M/%S // 格式打印
2022/10/01/17/26/33
[root@VM-8-14-centos lesson]# date +%Y-%m-%d/%H:%M:%S // 格式打印
2022-10-01/17:27:54
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@VM-8-14-centos lesson]# date +%s // 打印时间戳
1664616550
// 时间戳-》转真正的时间
[root@VM-8-14-centos lesson]# date +%Y-%m-%d/%H:%M:%S -d@1
1970-01-01/08:00:01
[root@VM-8-14-centos lesson]# date +%Y-%m-%d/%H:%M:%S -d@2
1970-01-01/08:00:02
[root@VM-8-14-centos lesson]# date +%Y-%m-%d/%H:%M:%S -d@3
1970-01-01/08:00:03
[root@VM-8-14-centos lesson]# date +%Y-%m-%d/%H:%M:%S -d@4
1970-01-01/08:00:04
cal命令可以用来显示公历(阳历)日历。公历是现在国际通用的历法,又称格列历,通称阳历。 “阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”。
命令格式: cal 参数[年份] 功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 常用选项:
-3 显示系统前一个月,当前月,下一个月的月历
-j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
-y 显示当前年份的日历
[root@VM-8-14-centos lesson]# cal 2022 // 打印日历
2022
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
// 打印当前年份的前几个日历
[root@VM-8-14-centos lesson]# cal -3
September 2022 October 2022 November 2022
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 1 2 3 4 5
4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30
30 31
Linux下find命令在目录结构中搜索文件,并执行指定的操作。
Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很 多,其中大部分选项都值得我们花时间来了解一下。
即使系统中含有网络文件系统( NFS), find命令在该文件系统中同样有效,只你具有相应的权限。
在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系 统可能会花费很长的时间(这里是指30G字节以上的文件系统 )。
语法: find pathname -options 功能: 用于在文件树种查找文件,并作出相应的处理(可能访问磁盘) 常用选项:
-name 按照文件名查找文件。
[root@VM-8-14-centos lesson]# find /root/lesson -name file.txt // 根据名字查找文件
/root/lesson/mv_test/file.txt // 查找结果
/root/lesson/file.txt // 查找结果
扩展的查找命令:
1- which(查找命令)
// 查找命令的指令
[root@VM-8-14-centos lesson]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@VM-8-14-centos lesson]# which rm
alias rm='rm -i'
/usr/bin/rm
[root@VM-8-14-centos lesson]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/usr/bin/alias
/usr/bin/which
[root@VM-8-14-centos lesson]# which cp
alias cp='cp -i'
/usr/bin/cp
2- whereis(查找命令)
// 在特定的目录下,查找指定的文件名对应的指令文档。
[root@VM-8-14-centos lesson]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@VM-8-14-centos lesson]# whereis rm
rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz
[root@VM-8-14-centos lesson]# whereis cp
cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz
linux grep命令详解 - ggjucheng - 博客园 (cnblogs.com) 语法: grep [选项] 搜寻字符串 文件 功能: 在文件中搜索字符串,将找到的行打印出来 常用选项:
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
[root@VM-8-14-centos lesson]# grep '9999' file.txt // 查找内容与文本匹配。
Hello ShaXiang [9999]
// 带行号
[root@VM-8-14-centos lesson]# grep -n '9999' file.txt // 带行号。
10000:Hello ShaXiang [9999]
过滤演示:
// 过滤了
[root@VM-8-14-centos lesson]# grep 'sha' file.txt // 查找匹配的文字。
shaXiang
shaXIANG
// 加 -i
[root@VM-8-14-centos lesson]# grep -i 'sha' file.txt // 这样全部出来了
Hello ShaXiang [0]
Hello ShaXiang [1]
ShaXiang
shaXiang
ShaXiang
ShaXiang
Shaxiang
shaXIANG
// 加 -n 将不符合条件的打印查找出来
[root@VM-8-14-centos lesson]# grep -v 'Sha' file.txt // 格式、
shaXiang
shaXIANG
统计行数
// 扩展统计行数
[root@VM-8-14-centos lesson]# grep '999' file.txt
Hello ShaXiang [999]
Hello ShaXiang [1999]
Hello ShaXiang [2999]
Hello ShaXiang [3999]
Hello ShaXiang [4999]
Hello ShaXiang [5999]
Hello ShaXiang [6999]
Hello ShaXiang [7999]
Hello ShaXiang [8999]
Hello ShaXiang [9990]
Hello ShaXiang [9991]
Hello ShaXiang [9992]
Hello ShaXiang [9993]
Hello ShaXiang [9994]
Hello ShaXiang [9995]
Hello ShaXiang [9996]
Hello ShaXiang [9997]
Hello ShaXiang [9998]
Hello ShaXiang [9999]
[root@VM-8-14-centos lesson]# grep '999' file.txt | wc -l
19
排序 - 以ASCII码的方式对文本进行排序。
[root@VM-8-14-centos lesson]# cat test.txt
555555
444
3333
222
1111
55
77
89
999
5
[root@VM-8-14-centos lesson]# sort test.txt // 排序
1111
222
3333
444
5
55
555555
77
89
999
去掉重复
// 配合管道
[root@VM-8-14-centos lesson]# sort test.txt | uniq // 去重
1111
222
3333
444
5
55
555555
666666
77
89
999
语法: zip 压缩文件.zip 目录或文件 功能: 将目录或文件压缩成zip格式 常用选项:
-r 递 归处理,将指定目录下的所有文件和子目录一并处理
zip的安装
[root@VM-8-14-centos lesson]# yum install zip unzip // 安装
zip的使用
[root@VM-8-14-centos lesson]# ls
file.txt mv_test MyZip test.txt
[root@VM-8-14-centos lesson]# zip file.zip file.txt // 压缩zip
adding: file.txt (deflated 88%)
[root@VM-8-14-centos lesson]# ls
file.txt file.zip mv_test MyZip test.txt
[root@VM-8-14-centos MyZip]# ls
file.zip
[root@VM-8-14-centos MyZip]# unzip file.zip // 解压zip
Archive: file.zip
inflating: file.txt
[root@VM-8-14-centos MyZip]# ls
file.txt file.zip
[root@VM-8-14-centos lesson]# ls
file.txt mv_test MyZip test.txt
[root@VM-8-14-centos lesson]# zip -r dirZip.zip mv_test/ // 递归式压缩zip
updating: mv_test/ (stored 0%)
adding: mv_test/cp_dir/ (stored 0%)
adding: mv_test/cp_dir/fail.txt (stored 0%)
adding: mv_test/file.txt (stored 0%)
[root@VM-8-14-centos lesson]# ls
dirZip.zip file.txt mv_test MyZip test.txt
[root@VM-8-14-centos MyZip]# ls
dirZip.zip
[root@VM-8-14-centos MyZip]# unzip dirZip.zip // 解压目录的zip
Archive: dirZip.zip
creating: mv_test/
creating: mv_test/cp_dir/
extracting: mv_test/cp_dir/fail.txt
extracting: mv_test/file.txt
[root@VM-8-14-centos MyZip]# ls
dirZip.zip mv_test
[root@VM-8-14-centos MyZip]# tree
.
├── dirZip.zip
└── mv_test
├── cp_dir
│ └── fail.txt
└── file.txt
解压到任何目的下
[root@VM-8-14-centos lesson]# ls
file.txt mv_test MyZip Myzip.zip test.txt
[root@VM-8-14-centos lesson]# unzip Myzip.zip -d /root/lesson/Myzip/ // 解压到指定目录下
[root@VM-8-14-centos lesson]# ls
file.txt mv_test Myzip MyZip Myzip.zip test.txt
[root@VM-8-14-centos lesson]# cd Myzip
[root@VM-8-14-centos Myzip]# ls
mv_test
[root@VM-8-14-centos Myzip]# tree
.
└── mv_test
├── cp_dir
│ └── fail.txt
└── file.txt
语法:alias =
功能:给名作取别名。
[root@VM-8-14-centos lesson]# ls
file.txt mv_test MyZip Myzip.zip test.txt
[root@VM-8-14-centos lesson]# unzip Myzip.zip -d /root/lesson/Myzip/ // 解压到指定目录下
[root@VM-8-14-centos lesson]# ls
file.txt mv_test Myzip MyZip Myzip.zip test.txt
[root@VM-8-14-centos lesson]# cd Myzip
[root@VM-8-14-centos Myzip]# ls
mv_test
[root@VM-8-14-centos Myzip]# tree
.
└── mv_test
├── cp_dir
│ └── fail.txt
└── file.txt
tar [-cxtzjvf] 文件与目录 .... 参数:
-c :建立一个压缩文件的参数指令(create 的意思);
-x :解开一个压缩文件的参数指令!
-t :查看 tarfile 里面的文件!
-z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
-j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩?
-v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
-f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
-C : 解压到指定目录
[root@VM-8-14-centos lesson]# ls
file.txt mv_test test.txt
[root@VM-8-14-centos lesson]# tar -czf My.tgz mv_test/ // 打包压缩文件。
[root@VM-8-14-centos lesson]# ls
file.txt mv_test My.tgz test.txt
[root@VM-8-14-centos test]# ls
My.tgz
[root@VM-8-14-centos test]# tar -xzf My.tgz // 解包压缩文件。
[root@VM-8-14-centos test]# ls
mv_test My.tgz
[root@VM-8-14-centos test]# tree
.
├── mv_test
│ ├── cp_dir
│ │ └── fail.txt
│ └── file.txt
└── My.tgz
// -v 带压缩信息
[root@VM-8-14-centos lesson]# ls
file.txt mv_test
[root@VM-8-14-centos lesson]# tar -czvf My.tgz mv_test/ // 打包。
mv_test/
mv_test/cp_dir/
mv_test/cp_dir/fail.txt
mv_test/file.txt
[root@VM-8-14-centos lesson]# ls
file.txt mv_test My.tgz
[root@VM-8-14-centos test]# ls
My.tgz
[root@VM-8-14-centos test]# tar -xzvf My.tgz // 解包。
mv_test/
mv_test/cp_dir/
mv_test/cp_dir/fail.txt
mv_test/file.txt
[root@VM-8-14-centos test]# ls
mv_test My.tgz
// -t 不进行解包/只查看内容
[root@VM-8-14-centos test]# ls
My.tgz
[root@VM-8-14-centos test]# tar -tf My.tgz // 查看压缩包文件。
mv_test/
mv_test/cp_dir/
mv_test/cp_dir/fail.txt
mv_test/file.txt
[root@VM-8-14-centos test]# ls
My.tgz
// -C解压到其他目录下
[root@VM-8-14-centos lesson]# ls
file.txt mv_test My.tgz test
[root@VM-8-14-centos lesson]# tar -xzvf My.tgz -C ./test // 解压到其他目录下。
mv_test/
mv_test/cp_dir/
mv_test/cp_dir/fail.txt
mv_test/file.txt
[root@VM-8-14-centos lesson]# ls ./test
mv_test
bc命令可以很方便的进行浮点运算 - 相当于计算器
quit :退出
[root@VM-8-14-centos lesson]# bc // bc命令
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+1
2
2.5+3.5
6.0
quit // bc退出。
// bc配合管道实现打印
[root@VM-8-14-centos lesson]# echo "1+2+3+4+5"
1+2+3+4+5
[root@VM-8-14-centos lesson]# echo "1+2+3+4+5" | bc
15
语法: uname [选项] 功能: uname用来获取电脑和操作系统的相关信息。 补充说明: uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息。 常用选项:
-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类 型,硬件平台类型,操作系统名称
[root@VM-8-14-centos lesson]# uname
Linux
[root@VM-8-14-centos lesson]# uname -a // 获取计算机更多信息
Linux VM-8-14-centos 3.10.0-1160.11.1.el7.x86_64 #1 SMP Fri Dec 18 16:34:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@VM-8-14-centos lesson]# uname -r // 获取Liunx内核版本
3.10.0-1160.11.1.el7.x86_64
[Tab]按键---具有『命令补全』和『档案补齐』的功能
[Ctrl]-c按键---让当前的程序『停掉』
[Ctrl]-d按键---通常代表着:『键盘输入结束(End Of File, EOF 戒 End OfInput)』的意思;另外,他也可 以用来取代exit
语法: shutdown [选项]
常见选项:
-h : 将系统的服务停掉后,立马关机。
-r : 在将系统的服务停掉之后就重新启动。
-t : -t 后面加秒数,即【过几秒后关机】的意思。
◆ 安装和登录命令: login、 shutdown、 halt、 reboot、 install、 mount、 umount、 chsh、 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。