【Linux】Linux常见的指令与操作(which/find/grep/zip/unzip/tar/bc命令)

197 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第30天,点击查看活动详情

✨欢迎关注🖱点赞🎀收藏⭐留言✒

🔮本文由京与旧铺原创,csdn首发!

😘系列专栏:linux学习

💻首发时间:🎞2022年12月24日🎠

🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦

🎧作者是一个新人,在很多方面还做的不好,欢迎大佬指正,一起学习哦,冲冲冲

1.which命令

which命令用于查找可执行文件所在位置。

语法:

which [选项] 文件名

常见选项(很少用到):
-n 文件名长度 指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
-p 文件名长度 与-n参数相同,但此处的文件名长度包括了文件的路径。
-w  指定输出时栏位的宽度

示例:

[wjhw@VM-4-15-centos tfile]$ which date
/usr/bin/date

2.find命令

find 命令可以在指定目录下寻找文件。

语法:

find 目录 选项 条件

常用选项:
-name 按名称查找
-iname 按名称查找(不区分大小写)
-a:and 必须满足两个条件才显示
-o:or 只要满足一个条件就显示
-maxdepth 查看多深的文件,不能超过所限制的目录下的内容
-mindepth 查看不小于多深的文件,不低于所限制内容
-perm 按照权限查找(mode为完全匹配,-mode为包含即可)
-user 按照所有者查找
-group 按照所有组查找
-mtime -n +n 根据修改内容的时间进行查找(-n指n天以内,+n指n天以前)
-atime -n +n 根据访问文件的时间查找(-n指n天以内,+n指n天以前)
-ctime -n +n 根据修改文件权限的时间查找(-n指n天以内,+n指n天以前)
-nouser 查找无所有者的文件
-nogroup 查找无所有组的文件
-newer f1 !f2 查找比文件f1新但比f2旧的文件
-type b/d/c/p/l/f 按照文件类型查找(后面的字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
-size 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-prune 忽略某个目录
-exec …… {}; 后面可跟用于进一步处理搜索结果的命令

示例1:根据文件名进行查找(区分大小写)

[wjhw@VM-4-15-centos files]$ find -name aaa
./hhh/jjj/aaa
./aaa
##相当于$ find . -name aaa 

示例2:根据文件名进行查找(不区分大小写)

[wjhw@VM-4-15-centos files]$ find . -iname aaa
./hhh/jjj/aaa
./aaa
./AAA

示例3:查找某个后缀结尾的文件

[wjhw@VM-4-15-centos files]$ find . -name "*.txt"
./hello/test.txt
./3.txt
./tfile/test.txt
./1.txt
./aaa/file2.txt
./aaa/file1.txt
./2.txt

示例4:查找文件名test并且txt后缀结尾的文件

[wjhw@VM-4-15-centos files]$ find . -name test.txt -a -name "*.txt"
./hello/test.txt
./tfile/test.txt

示例5:只在根目录查找

[wjhw@VM-4-15-centos files]$ find . -maxdepth 1 -iname aaa
./aaa
./AAA

3.grep命令

grep 命令可以搜索文件的内容,就像你在windows中打开word文件,使用快捷键 “Ctrl+F” 在文本中查找某个字符串一样。Linux下grep指令是支持正则表达式的。

语法:

grep 选项 字符串 文件

常用选项:
-i:忽略大小写
-n:显示结果所在行号
-c:统计匹配到的行数,注意,是匹配到的总行数,不是匹配到的次数
-o:只显示符合条件的字符串,但是不整行显示,每个符合条件的字符串单独显示一行
-v:输出不带关键字的行(反向查询,反向匹配)
-w:匹配整个单词,如果是字符串中包含这个单词,则不作匹配
-e:实现多个选项的匹配,逻辑或关系

示例1:简单查找

[wjhw@VM-4-15-centos files]$ grep "aaa" ggg.txt
aaa

示例2:不区分大小写查找

[wjhw@VM-4-15-centos files]$ grep -i "aaa" ggg.txt
aaa
AAA

示例3:可以与其他命令搭配使用

[wjhw@VM-4-15-centos files]$ cat ggg.txt | grep -i "aaa"
aaa
AAA

示例4:显示行号输出

[wjhw@VM-4-15-centos files]$ grep -in "ccc" ggg.txt
3:cccxjhdcnikshfujdshsjhdsjehkfjfksjnkdsfg
29:CCCdskjfkdnfknvgjfd

示例5:查找内容不包含非匹配符

[wjhw@VM-4-15-centos files]$ grep -ino "ccc" ggg.txt
3:ccc
29:CCC

示例6:查找不带关键字的行

[wjhw@VM-4-15-centos files]$ grep -inv "ccc" ggg.txt
1:aaa
2:bbb
4:ddd
5:eee
6:fff
7:ggg
...

4.zip/unzip命令

zip/unzip 可以实现zip压缩文件的压缩与解压缩。

语法:

zip 选项 压缩文件目录以及压缩文件名 源文件
unzip 选项 文件

zip命令常用选项:
-r 递归处理,将指定目录下的所有文件和子目录一并处理
-q 不显示指令执行过程
-z 替压缩文件加上注释
-v 显示指令执行过程或显示版本信息
-d 更新压缩包内文件
-n<字尾字符串> 不压缩具有特定字尾字符串的文件

unzip命令常用选项:
-l 显示压缩文件内所包含的文件
-v 执行时显示详细的信息
-c 将解压缩的结果显示到屏幕上,并对字符做适当的转换
-n 解压缩时不要覆盖原有的文件
-j 不处理压缩文件中原有的目录路径
-d 指定文件解压缩后所要存储的目录。

示例1:不使用递归压缩

[wjhw@VM-4-15-centos files]$ zip ./testzip/tf.zip tfile
  adding: tfile/ (stored 0%)
[wjhw@VM-4-15-centos testzip]$ unzip tf.zip -d .
Archive:  tf.zip
   creating: ./tfile/
[wjhw@VM-4-15-centos testzip]$ ll
total 8
drwxrwxr-x 2 wjhw wjhw 4096 Dec 23 21:21 tfile
-rw-rw-r-- 1 wjhw wjhw  162 Dec 24 22:19 tf.zip
[wjhw@VM-4-15-centos testzip]$ tree tfile
tfile

0 directories, 0 files

示例2:使用递归压缩

[wjhw@VM-4-15-centos files]$ zip -r ./testzip/tf.zip tfile
  adding: tfile/ (stored 0%)
  adding: tfile/test.txt (stored 0%)
  adding: tfile/d1/ (stored 0%)
  adding: tfile/d1/d2/ (stored 0%)
  adding: tfile/d1/d2/d3/ (stored 0%)
  adding: tfile/d1/d2/d3/d4/ (stored 0%)
  adding: tfile/d1/d2/d3/d4/d5/ (stored 0%)
[wjhw@VM-4-15-centos testzip]$ unzip tf.zip -d .
Archive:  tf.zip
   creating: ./tfile/
 extracting: ./tfile/test.txt        
   creating: ./tfile/d1/
   creating: ./tfile/d1/d2/
   creating: ./tfile/d1/d2/d3/
   creating: ./tfile/d1/d2/d3/d4/
   creating: ./tfile/d1/d2/d3/d4/d5/
[wjhw@VM-4-15-centos testzip]$ tree tfile
tfile
|-- d1
|   `-- d2
|       `-- d3
|           `-- d4
|               `-- d5
`-- test.txt

5.tar命令

tar 命令可以实现文件和目录的打包/解包/压缩/解压。

语法:

tar 选项 操作

常用选项:
-c :建立一个压缩文件的参数指令
-x :解开一个压缩文件的参数指令
-t :列出备份文件的内容
-z :通过gzip指令压缩/解压缩文件,文件名最好为*.tar.gz(tgz)
-j :通过bzip2指令压缩/解压缩文件,文件名最好为*.tar.bz2
-v :显示指令执行过程
-f<备份文件> : 指定备份文件
-C <目录> :仅压缩指定目录里的内容或解压缩到指定目录

示例1:使用tar压缩打包

[wjhw@VM-4-15-centos testzip]$ tar -czf tf.tgz tfile
[wjhw@VM-4-15-centos testzip]$ ll
total 12
drwxrwxr-x 3 wjhw wjhw 4096 Dec 23 21:21 tfile
-rw-rw-r-- 1 wjhw wjhw  241 Dec 24 22:39 tf.tgz
-rw-rw-r-- 1 wjhw wjhw 1120 Dec 24 22:24 tf.zip

使用tar命令解压:

[wjhw@VM-4-15-centos testzip]$ tar -xzvf tf.tgz -C test
tfile/
tfile/test.txt
tfile/d1/
tfile/d1/d2/
tfile/d1/d2/d3/
tfile/d1/d2/d3/d4/
tfile/d1/d2/d3/d4/d5/
[wjhw@VM-4-15-centos testzip]$ tree test
test
`-- tfile
    |-- d1
    |   `-- d2
    |       `-- d3
    |           `-- d4
    |               `-- d5
    `-- test.txt

6.其他命令

bc 计算器

语法:

bc
输入表达式回车计算

示例:

[wjhw@VM-4-15-centos files]$ 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+3
7
2.0+333
335.0
4.5*23.7
106.6
scale=2
122/100
1.22

uname 查看Linux信息

语法:

uname 选项 

示例:

[wjhw@VM-4-15-centos files]$ uname -a
Linux VM-4-15-centos 3.10.0-1160.62.1.el7.x86_64 #1 SMP Tue Apr 5 16:57:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

hostname 显示主机名

示例:

[wjhw@VM-4-15-centos files]$ hostname
VM-4-15-centos

shutdown 关机 (云服务器永远不关机)

7.常用的热键

热键 tab 自动补齐 Ctrl+r 检索历史命令 Ctrl+c 终止命令 Ctrl+d 直接离开命令行,相当于exit