主页 > 开源代码  > 

【Linux】Linux的常见指令详解(下)

【Linux】Linux的常见指令详解(下)

目录

前言

head/tail

命令行管道

date

sort

cal

搜索指令

find

which

whereis

alias

grep

zip

tar

file 

bc

history 

热键


前言

之前讲了Linux的常见指令详解(上),这次终于把下也补齐了。如果对你有帮助还麻烦给博主一个三连。💖💖

head/tail

🐾顾名思义,这两个指令是用于打印文件头尾(目标行)的。

语法: head(tail) -行数

若为未加行数则默认是打印前十行。还可以加上输出重定向将限定范围内的数据存储到一个新文件当中去。

[root@VM-12-9-centos file]# head -5 print.c > tmp.c //把前五行传入新文件中 [root@VM-12-9-centos file]# cat tmp.c #include<stdio.h> int main() { printf("hello world\n");

🐾 如下通过 head 与 tail 的混合使用我们可以做到目标行的提取。

[root@VM-12-9-centos file]# cat tmp.c //上一步提取的前五行数据 #include<stdio.h> int main() { printf("hello world\n"); [root@VM-12-9-centos file]# tail -3 tmp.c //提取尾三行的数据 int main() //输出2~5行的数据 { printf("hello world\n"); 命令行管道

🐾就像管道一样起承接的作用,既有输入也有输出。比如:

[root@VM-12-9-centos file]# cat print.c //打印出文件内容 #include<stdio.h> int main() { printf("hello world\n"); return 0; } [root@VM-12-9-centos file]# cat print.c | wc -l //通过管道来统计行数 7

🐾可以这么理解,cat命令把文件输入到管道里之后让wc命令接收并统计输出了文件的行数。

通过命令行管道的使用,我们可以不用创建一个新文件并实现目标行的提取。

[root@VM-12-9-centos file]# head -5 print.c | tail -3 //提取前五行传给管道 int main() //经过后三行提取后再输出 { printf("hello world\n"); [root@VM-12-9-centos file]# date

 date 是与时间相关的指令,根据后接指令的不同可以实现不同的效果。

🐾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

[root@VM-12-9-centos file]# date +%Y:%m:%d //当前年月日的打印 2022:12:09 [root@VM-12-9-centos file]# date +%X //当前时间 10:56:27 PM [root@VM-12-9-centos ~]# date +%Y-%m-%d:%H:%M:%S //结合打印 2022-12-10:09:42:27 [root@VM-12-9-centos file]# date +%s //当前时间转化成时间戳 1670597887 [root@VM-12-9-centos file]# date -d@1670597887 //时间戳转化成时间 Fri Dec 9 22:58:07 CST 2022 sort

🐾该命令可以将文件内的数据进行排序,类似于( strcmp )按行比较,默认以升序的方式输出加上 -r 则是进行降序排序。

[root@VM-12-9-centos ~]# cat text.c //原文件内的数据 hello print access abs banana text banana text [root@VM-12-9-centos ~]# sort text.c //升序排序 abs access banana banana hello print text text [root@VM-12-9-centos ~]# sort -r text.c //降序排序 text text print hello banana banana access abs

 🐾同时,使用命令行管道,可以实现不打印重复行。

[root@VM-12-9-centos ~]# sort text.c | uniq abs access banana hello print text cal

🐾与 nano 类似的小组件,用于日历的打印。

语法: cal [参数][月份][年份] 功能:用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份。常用选项: -3 显示系统前一个月,当前月,下一个月的月历 -j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数) -y 显示当前年份的日历

[root@VM-12-9-centos ~]# cal 10 2022 October 2022 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 搜索指令 find

🐾使用-name的选项可以对目录下文件名称进行查找。

[root@VM-12-9-centos ~]# find /root/ -name text.c //路径+选项+名称 /root/text.c //这个路径下叫text.c的文件有两个 /root/file/text.c which

🐾用于对命令存储位置的查找。

[root@VM-12-9-centos ~]# which man /usr/bin/man [root@VM-12-9-centos ~]# which find /usr/bin/find [root@VM-12-9-centos ~]# which cat /usr/bin/cat whereis

🐾主要用于在系统默认路径下搜索指定名称的文件、程序、或归档文件(压缩包)。

[root@VM-12-9-centos ~]# whereis find //对find的搜索 find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/man/man1p/find.1p.gz //指令的位置 //对应man手册的位置 alias

🐾就跟#typedef一样,可以给一个命令起别名从而达到使用起来更加方便的效果。

语法:alias [别名]= ‘[原指令]’

[root@VM-12-9-centos ~]# alias myls='ls -a -l' //通过取别名的方式 [root@VM-12-9-centos ~]# myls //使得myls跟ls -a -l是一样的效果 total 108 dr-xr-x---. 10 root root 4096 Dec 9 21:19 . dr-xr-xr-x. 20 root root 4096 Dec 10 10:35 .. -rwxr-xr-x 1 root root 8400 Dec 1 15:50 a.out -rw------- 1 root root 18743 Dec 10 10: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 3 root root 4096 Mar 7 2019 .cache drwxr-xr-x 3 root root 4096 Mar 7 2019 .config -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwxr-xr-x 3 root root 4096 Dec 3 20:53 dir drwxr-xr-x 3 root root 4096 Dec 10 09:51 file -rw------- 1 root root 130 Dec 3 20:47 .lesshst drwxr-xr-x 2 root root 4096 Nov 24 10:25 nfile drwxr-xr-x 2 root root 4096 Jul 20 2021 .pip drwxr----- 3 root root 4096 Jul 20 2021 .pki -rw-r--r-- 1 root root 73 Jul 20 2021 .pydistutils.cfg drwx------ 2 root root 4096 Nov 2 08:43 .ssh -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc -rw-r--r-- 1 root root 0 Dec 3 17:21 .text -rw-r--r-- 1 root root 48 Dec 9 23:52 text.c -rw-r--r-- 1 root root 0 Dec 3 21:12 tmp.c -rw------- 1 root root 0 Jul 20 2021 .viminfo -rw-r--r-- 1 root root 312 Nov 22 19:10 .zip grep

🐾特定关键字的文本筛选。

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

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

选项

-n 输出目标行的行号。

[root@VM-12-9-centos ~]# grep 'te' text.c //普通筛选 text text [root@VM-12-9-centos ~]# grep -n 'te' text.c //输出带行号 6:text 8:text zip

🐾 zip跟tar都是用于压缩文件的命令,但使用方式上还是略有差异。

语法:zip [选项] [目标] [源] 

选项

-r       递归压缩

[root@VM-12-9-centos ~]# tree dir //dir是这样的树型结构 dir └── dir1 └── dir2 └── dir3 └── dir4 [root@VM-12-9-centos ~]# zip -r dir.zip dir //对dir进行递归压缩到dir.zip里 adding: dir/ (stored 0%) adding: dir/dir1/ (stored 0%) adding: dir/dir1/dir2/ (stored 0%) adding: dir/dir1/dir2/dir3/ (stored 0%) adding: dir/dir1/dir2/dir3/dir4/ (stored 0%) [root@VM-12-9-centos ~]# ll //在当前目录下就可以看到压缩完的压缩包 total 32 -rwxr-xr-x 1 root root 8400 Dec 1 15:50 a.out drwxr-xr-x 3 root root 4096 Dec 3 20:53 dir -rw-r--r-- 1 root root 802 Dec 10 10:51 dir.zip drwxr-xr-x 3 root root 4096 Dec 10 09:51 file drwxr-xr-x 2 root root 4096 Nov 24 10:25 nfile -rw-r--r-- 1 root root 48 Dec 9 23:52 text.c -rw-r--r-- 1 root root 0 Dec 3 21:12 tmp.c

🐾值得注意的是,若对一个内部有文件的目录进行压缩,就要使用-r选项进行递归压缩。否则压缩进去的就只有一个目录而已。 

[root@VM-12-9-centos ~]# zip dir.zip dir //没有进行递归压缩 adding: dir/ (stored 0%) [root@VM-12-9-centos file]# unzip dir.zip //解压成功 Archive: dir.zip creating: dir/ [root@VM-12-9-centos file]# tree dir //内容中只有目录名 dir

unzip

🐾用于解压的命令。

语法 :unzip [源]

选项

-d       可以解压到指定路径里。

[root@VM-12-9-centos ~]# unzip dir.zip -d ./nfile //解压到指定目录下 Archive: dir.zip creating: ./nfile/dir/ creating: ./nfile/dir/dir1/ creating: ./nfile/dir/dir1/dir2/ creating: ./nfile/dir/dir1/dir2/dir3/ creating: ./nfile/dir/dir1/dir2/dir3/dir4/ [root@VM-12-9-centos ~]# ll nfile //观测到该目录下已有解压后的文件 total 4 drwxr-xr-x 3 root root 4096 Dec 3 20:53 dir [root@VM-12-9-centos ~]# cd nfile [root@VM-12-9-centos nfile]# tree dir dir └── dir1 └── dir2 └── dir3 └── dir4 4 directories, 0 files tar

🐾同为压缩的操作,与zip不同tar是根据选项的不同来调整操作。

语法:tar -[选项]  [目标] [源]

选项:

-c :建立一个压缩文件的参数指令(create 的意思); -x :解开一个压缩文件的参数指令! -t :查看 tarfile 里面的文件! -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩? -j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩? -v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程! -f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数! -C : 解压到指定目录

解压之后的格式:tgz

[root@VM-12-9-centos ~]# tar -czf file.tgz file //用-czf命令压缩文件 [root@VM-12-9-centos ~]# ll //观察到当前目录下已有目标压缩包 total 32 -rwxr-xr-x 1 root root 8400 Dec 1 15:50 a.out drwxr-xr-x 3 root root 4096 Dec 3 20:53 dir drwxr-xr-x 2 root root 4096 Dec 10 11:22 file -rw-r--r-- 1 root root 333 Dec 10 11:42 file.tgz drwxr-xr-x 2 root root 4096 Dec 10 11:25 nfile -rw-r--r-- 1 root root 48 Dec 9 23:52 text.c -rw-r--r-- 1 root root 0 Dec 3 21:12 tmp.c [root@VM-12-9-centos ~]# tar -ztvf file.tgz //用-ztcf只查看不解压目标压缩包 drwxr-xr-x root/root 0 2022-12-10 11:22 file/ -rw-r--r-- root/root 24 2022-12-03 21:54 file/hello -rw-r--r-- root/root 7 2022-12-10 09:51 file/text.c -rw-r--r-- root/root 71 2022-12-01 15:45 file/print.c -rw-r--r-- root/root 17 2022-11-22 17:00 file/test [root@VM-12-9-centos ~]# tar -xzf file.tgz -C ./nfile //使用-xzf和-C将该压缩包解压到目标目录下 [root@VM-12-9-centos ~]# ll nfile //观测到解压成功 total 4 drwxr-xr-x 2 root root 4096 Dec 10 11:22 file [root@VM-12-9-centos ~]# cd nfile [root@VM-12-9-centos nfile]# tree file file ├── hello ├── print.c ├── test └── text.c 0 directories, 4 files file 

🐾值得一提的是,Linux中是不以文件后缀区分文件类型的,在使用 ll 查看目录下文件时,最左边那一列就表示的是文件类型。

- 普通文件

d : 目录

b : 块设备 磁盘

c : 字符设备

p : 管道文件

s : 网络socket文件

 l : 链接文件

 

🐾如果追求更加细致的话,可以使用file命令进行查询。

语法: file [文件名]

[root@VM-12-9-centos ~]# file dir //目录 dir: directory [root@VM-12-9-centos ~]# file text.c //文本文件 text.c: ASCII text [root@VM-12-9-centos ~]# file tmp.c //空文件 tmp.c: empty [root@VM-12-9-centos file]# file print.c //源文件 print.c: C source, ASCII text

🐾由此便可以清楚地看出来所查询的文件,到底是什么类型的文件了。

看待文件后缀

🐾虽然前面说Linux并不以文件后缀来区分文件类型,那文件类型是否就变成不需要的存在了?答案是否定的。在Linux下使用文件后缀即便系统不进行识别,但是可以将其看作文件名的一部分,使使用者可以直观地区分其文件类型的不同。不仅如此,有些软件是会识别文件的后缀的。

[root@VM-12-9-centos file]# gcc print.c //使用gcc编译源文件 [root@VM-12-9-centos file]# ll //编译成功生成可执行文件 total 28 -rwxr-xr-x 1 root root 8400 Dec 10 13:49 a.out -rw-r--r-- 1 root root 24 Dec 3 21:54 hello -rw-r--r-- 1 root root 71 Dec 1 15:45 print.c -rw-r--r-- 1 root root 17 Nov 22 17:00 test -rw-r--r-- 1 root root 7 Dec 10 09:51 text.c [root@VM-12-9-centos file]# mv print.c print.txt //修改文件后缀 [root@VM-12-9-centos file]# ll total 28 -rwxr-xr-x 1 root root 8400 Dec 10 13:49 a.out -rw-r--r-- 1 root root 24 Dec 3 21:54 hello -rw-r--r-- 1 root root 71 Dec 1 15:45 print.txt -rw-r--r-- 1 root root 17 Nov 22 17:00 test -rw-r--r-- 1 root root 7 Dec 10 09:51 text.c [root@VM-12-9-centos file]# gcc print.txt //编译失败 print.txt: file not recognized: File format not recognized //无法识别文件 collect2: error: ld returned 1 exit status bc

🐾类似于计算器的小组件,可以直接地进行计算。

[root@VM-12-9-centos file]# 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+2 3 2*2 4 8/2 4

🐾借助命令行管道,我们可以实现命令行计算。 

[root@VM-12-9-centos file]# echo "1+3" | bc 4 history 

🐾用于查看历史输入过的所有指令,同时可以通过输出重定向保存到文件之中。

[root@VM-12-9-centos file]# history > history.txt [root@VM-12-9-centos file]# ll total 56 -rw-r--r-- 1 root root 24 Dec 3 21:54 hello -rw-r--r-- 1 root root 38703 Dec 10 14:10 history.txt -rw-r--r-- 1 root root 71 Dec 1 15:45 print.c -rw-r--r-- 1 root root 17 Nov 22 17:00 test -rw-r--r-- 1 root root 7 Dec 10 09:51 text.c [root@VM-12-9-centos file]# less history.txt 热键

ctrl + c

🐾用于终止当前程序。

[root@VM-12-9-centos file]# ' //有时候会一不小心按成这个样子 > //怎么按也没有效果 > > > > > ^C //按下ctrl+c就可以解决了 [root@VM-12-9-centos file]#

ctrl + d

🐾可以取代exit,用于用户的退出,或者连按两次关闭xshell。

[root@VM-12-9-centos file]# whoami //当前用户是root root [root@VM-12-9-centos file]# su Alpaca //更换用户 [Alpaca@VM-12-9-centos file]$ whoami Alpaca [Alpaca@VM-12-9-centos file]$ exit //按ctrl+d [root@VM-12-9-centos file]# whoami //变回root root

ctrl + r

🐾用于对历史命令的查找,相当于history的查找版本。

(reverse-i-search)`zip': rm -rf dir.zip

TAB

🐾具有『命令补全』和『档案补齐』的功能,比如说你在准备进入目录的时候,但忘记有哪些目录可供选择了。这时候就可以按下几次TAB键系统便会自动为你补齐当前目录下可以选择的目录。而当你选择一个目录之后再次按下TAB键系统便会再次显示出你已选择的目录下有那些选项可供你选择。

[root@VM-12-9-centos /]# cd bin/ data/ etc/ lib/ lost+found/ mnt/ opt/ public/ run/ srv/ tmp/ var/ boot/ dev/ home/ lib64/ media/ nfile/ proc/ root/ sbin/ sys/ usr/ [root@VM-12-9-centos /]# cd /home/ Alpaca/ lighthouse/

🐾那讲到这里,Linux的常见指令到这里就正是结束了,关注博主一起进步,如果对你有帮助还请一健三连💕💕。

       

 

标签:

【Linux】Linux的常见指令详解(下)由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【Linux】Linux的常见指令详解(下)