Linux基础(二)

Linux基础(二)


1.基础命令


//大小统计
du //查看文件或目录占用的磁盘空间大小
-h //以人类友好的方式显示大小
-s //显示总的占用空间大小

[root@localhost ~]# du anaconda-ks.cfg 
4	anaconda-ks.cfg
[root@localhost ~]# du -sh anaconda-ks.cfg 
4.0K	anaconda-ks.cfg

df //报告文件系统磁盘空间使用情况
-h //以人类友好的方式显示大小

[root@localhost ~]# df 
Filesystem          1K-blocks    Used Available Use% Mounted on
devtmpfs               895572       0    895572   0% /dev
tmpfs                  915628       0    915628   0% /dev/shm
tmpfs                  915628    8844    906784   1% /run
tmpfs                  915628       0    915628   0% /sys/fs/cgroup
/dev/mapper/cs-root  17811456 1685728  16125728  10% /
/dev/sda1             1038336  199320    839016  20% /boot
tmpfs                  183124       0    183124   0% /run/user/0
[root@localhost ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             875M     0  875M   0% /dev
tmpfs                895M     0  895M   0% /dev/shm
tmpfs                895M  8.7M  886M   1% /run
tmpfs                895M     0  895M   0% /sys/fs/cgroup
/dev/mapper/cs-root   17G  1.7G   16G  10% /
/dev/sda1           1014M  195M  820M  20% /boot
tmpfs                179M     0  179M   0% /run/user/0

//主机名管理
hostname //查看或临时修改主机名,重开终端有效,重启失效
hostnamectl //查看或永久修改主机名,重开终端生效,重启依然有效

[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# hostname yhm
[root@localhost ~]# bash
[root@yhm ~]# 
[root@localhost ~]# hostnamectl set-hostname yhm
[root@localhost ~]# bash
[root@yhm ~]# 

//其它
time //显示命令的执行时间,例如time ls /etc
clear //清屏

[root@localhost ~]# clear

whoami //显示当前登录用户
w //显示当前在线用户并显示其在运行的命令
who //查看当前在线用户

[root@localhost ~]# whoami 
root
[root@localhost ~]# w
 09:24:02 up 13 min,  2 users,  load average: 0.00, 0.01, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                09:11   12:45   0.00s  0.00s -bash
root     pts/0    192.168.223.1    09:11    1.00s  0.01s  0.00s w
[root@localhost ~]# who
root     tty1         2022-06-30 09:11
root     pts/0        2022-06-30 09:11 (192.168.223.1)

which //显示指定命令的绝对路径

[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

cal //打印日历

[root@localhost ~]# cal 
      June 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      

ldd //查看指定程序有哪些依赖库文件
程序组成?
二进制程序
库文件
配置文件
帮助文件

[root@localhost ~]# ldd /usr/bin/ls
	linux-vdso.so.1 (0x00007ffe823b9000)
	libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f2c5ef93000)
	libcap.so.2 => /lib64/libcap.so.2 (0x00007f2c5ed8d000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f2c5e9cb000)
	libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007f2c5e747000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f2c5e543000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2c5f3e0000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2c5e323000) 

date //显示或设置日期与时间
//不带参数的date用于显示当前系统日期与时间
-s //以字符串方式设置时间
//格式化输出时间:+
%Y //年
%m //月
%d //日
%H //时
%M //分
%S //秒

[root@localhost ~]# date
Thu Jun 30 09:36:02 CST 2022
[root@localhost ~]# date +%Y
2022
[root@localhost ~]# date +%Y%m
202206
[root@localhost ~]# date +%Y%m%d
20220630
[root@localhost ~]# date +%Y%m%d%H
2022063009
[root@localhost ~]# date +%Y%m%d%H%M
202206300937
[root@localhost ~]# date +%Y%m%d%H%M%S
20220630093754
[root@localhost ~]# date '+%Y-%m-%d %H:%M:%S'
2022-06-30 09:38:40
[root@localhost ~]# date -s '2022-07-01 8:00:53'
Fri Jul  1 08:00:53 CST 2022

cut //截取文本内容
-d //指定字段分隔符,默认是空格
-f //指定要显示的字段
-f 1,3 显示1和3
-f 1-3 显示1到3

[root@localhost ~]# cut -d : -f 1-3 /etc/passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3

[root@localhost ~]# cut -d : -f 1,3 /etc/passwd
root:0
bin:1
daemon:2
adm:3

1.1 如何获取命令帮助

//内部命令
help COMMAND

[root@localhost ~]# help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

//外部命令
COMMAND --help

[root@localhost ~]# tar --help
Usage: tar [OPTION...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

如何区分内部命令和外部命令

[root@localhost ~]# which cd
/usr/bin/cd
[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

//命令手册 manual
man COMMAND

[root@localhost ~]# man ls
LS(1)               User Commands               LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

//man手册注意事项:
[] //可选
<> //必选
… //可以出现多次
| //多选一
{} //分组
NAME //命令名称及功能简要说明
SYNOPSIS //用法说明,包括可用的选项
DESCRIPTION //命令功能的详尽说明,可能包括每一个选项的意义
OPTIONS //说明每一个选项的意义
FILES //此命令相关的配置文件
BUGS //报告bug
EXAMPLES //使用示例
SEE ALSO //另外参照

//man翻屏
向后翻一屏 //SPACE
向前翻一屏 //b
向后翻一行 //enter
向前翻一行 //k

//查找
/KEYWORD //向后
?KEYWORD //向前
n //下一个
N //前一个
q //退出

2.通配符


//文件名通配 globbing
* //匹配任意长度的任意字符
? //匹配任意单个字符
[] //匹配指定范围内的任意单个字符
[abc],[a-m],[0-9]
[[:space:]] //表示空白字符
[[:punct:]] //表示标点符号
[[:lower:]] //表示小写字母
[[:upper:]] //表示大写字母
[[:alpha:]] //表示大小写字母
[[:digit:]] //表示数字
[[:alnum:]] //表示数字和大小写字母

使用man 7 glob命令可以获得以上字符集合的帮助信息!!!

[root@localhost ~]# ls a*
anaconda-ks.cfg
[root@localhost ~]# touch ab abc abcd
[root@localhost ~]# ls a?
ab
[root@localhost ~]# touch a b c d e f
[root@localhost ~]# ls [a-f]
a  b  c  d  e  f
[root@localhost ~]# ls [a-c]
a  b  c
[root@localhost ~]# ls [ac]
a  c
[root@localhost ~]# touch , :
[root@localhost ~]# ls
,  a   abc   anaconda-ks.cfg  c  e
:  ab  abcd  b                d  f
[root@localhost ~]# ls [[:punct:]]
,  :
[root@localhost ~]# ls [[:lower:]]
a  b  c  d  e  f
[root@localhost ~]# touch A B
[root@localhost ~]# ls [[:upper:]]
A  B
[root@localhost ~]# ls [[:alpha:]]
a  A  b  B  c  d  e  f
[root@localhost ~]# touch 1 2
[root@localhost ~]# ls [[:digit:]]
1  2
[root@localhost ~]# ls [[:alnum:]]
1  2  a  A  b  B  c  d  e  f

[^] //匹配指定范围之外的任意单个字符

[root@localhost ~]# ls [a]
a
[root@localhost ~]# ls [^a]
,  :  1  2  A  b  B  c  d  e  f

3.文件归档,压缩


3.1常见的归档与压缩文件格式

  • .gz
  • .bz2
  • .xz
  • .zip
  • .Z

3.2压缩工具

//压缩、解压缩命令
gzip //压缩后的文件以.gz结尾
gzip /path/to/somefile //压缩完成后会删除原文件
-d //解压缩,解压完成后会删除原文件

[root@localhost ~]# gzip abc 
[root@localhost ~]# ls
ab  abc.gz  anaconda-ks.cfg  bb  cd
[root@localhost ~]# gzip -d abc.gz 
[root@localhost ~]# ls
ab  abc  anaconda-ks.cfg  bb  cd

bzip2 //压缩后的文件以.bz2结尾
-d //解压缩,解压完成后会删除原文件
-k //keep,压缩时保留原文件

[root@localhost opt]# rpm -ivh /mnt/BaseOS/Packages/bzip2-1.0.6-26.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/bzip2-1.0.6-26.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:bzip2-1.0.6-26.el8               ################################# [100%]
[root@localhost opt]# ls
abc
[root@localhost opt]# bzip2 abc 
[root@localhost opt]# ls
abc.bz2
[root@localhost opt]# bzip2 -d abc.bz2 
[root@localhost opt]# ls
abc
[root@localhost opt]# bzip2 -k abc 
[root@localhost opt]# ls
abc  abc.bz2

xz //压缩后的文件以.xz结尾
-d //解压缩,解压完成后会删除原文件
-k //keep,压缩时保留原文件

[root@localhost opt]# ls
abc
[root@localhost opt]# xz -k abc
[root@localhost opt]# ls
abc  abc.xz
[root@localhost opt]# rm -rf abc
[root@localhost opt]# xz -d abc.xz 
[root@localhost opt]# ls
abc

zip //既归档又压缩的工具。zip可以压缩目录,zip压缩包要使用unzip工具来解压
//gz、bz2、xz都只能压缩文件,zip压缩后不会删除原文件
zip filename.zip file1 file2 …
zip filename.zip DIR/*
unzip
unzip filename.zip

[root@localhost opt]# ls
abc  www
[root@localhost opt]# rpm -ivh /mnt/BaseOS/Packages/zip-3.0-23.el8.x86_64.rpm --nodeps
warning: /mnt/BaseOS/Packages/zip-3.0-23.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:zip-3.0-23.el8                   ################################# [100%]
[root@localhost opt]# zip 123.zip www
  adding: www/ (stored 0%)
[root@localhost opt]# ls
123.zip  abc  www
[root@localhost opt]# rpm -ivh /mnt/BaseOS/Packages/unzip-6.0-44.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/unzip-6.0-44.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:unzip-6.0-44.el8                 ################################# [100%]
[root@localhost opt]# ls
123.zip  abc  www
[root@localhost opt]# unzip 123.zip 
Archive:  123.zip
[root@localhost opt]# ls
123.zip  abc  www
[root@localhost opt]# unzip 123.zip 
Archive:  123.zip
[root@localhost opt]# ls
123.zip  abc  www
[root@localhost opt]# touch aa bb
[root@localhost opt]# ls
123.zip  aa  abc  bb  www
[root@localhost opt]# zip 1.zip aa bb abc
  adding: aa (stored 0%)
  adding: bb (stored 0%)
  adding: abc (stored 0%)
[root@localhost opt]# ls
123.zip  1.zip  aa  abc  bb  www
[root@localhost opt]# rm -rf aa bb abc
[root@localhost opt]# ls
123.zip  1.zip  www
[root@localhost opt]# unzip 1.zip 
Archive:  1.zip
 extracting: aa                      
 extracting: bb                      
 extracting: abc                     
[root@localhost opt]# ls
123.zip  1.zip  aa  abc  bb  www

compress //不能压缩空文件,解压需要使用uncompress命令
yum provides *bin/compress //查看哪个软件包提供的命令

[root@localhost opt]# yum provides *bin/compress
Last metadata expiration check: 0:37:05 ago on Thu 30 Jun 2022 02:52:45 PM CST.
ncompress-4.2.4.4-13.el8.x86_64 : Fast compression and
     ...: decompression utilities
Repo        : appstream
Matched from:
Other       : *bin/compress

[root@localhost opt]# rpm -ivh /mnt/AppStream/Packages/ncompress-4.2.4.4-13.el8.x86_64.rpm 
warning: /mnt/AppStream/Packages/ncompress-4.2.4.4-13.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:ncompress-4.2.4.4-13.el8         ################################# [100%]
[root@localhost opt]# ls
123.zip  1.zip  aa  abc  bb  www
[root@localhost opt]# cp /etc/passwd .
[root@localhost opt]# ls
123.zip  1.zip  aa  abc  bb  passwd  www
[root@localhost opt]# compress passwd 
[root@localhost opt]# ls
123.zip  1.zip  aa  abc  bb  passwd.Z  www
[root@localhost opt]# uncompress passwd.Z 
[root@localhost opt]# ls
123.zip  1.zip  aa  abc  bb  passwd  www
[root@localhost opt]# 

3.3归档工具

archive //归档,归档本身并不意味着压缩
tar //归档工具,只归档不压缩
-c //创建归档文件
-f file.tar //操作的归档文件
-x //还原归档
-v //显示归档过程
-p //归档时保留权限信息。只有管理员才有权限用此选项
-C //将展开的归档文件保存至指定目录下
-tf /path/to/file.tar //不展开归档,直接查看归档了哪些文件
-zcf //归档并调用gzip压缩
-jcf //归档并调用bzip2压缩
-Jcf //归档并调用xz压缩
xf file.tar.gz|file.tar.bz2|file.tar.xz:还原归档并自动根据压缩方式调用相应的工具解压

[root@localhost opt]# tar -zcf aa.tar.gz aa
[root@localhost opt]# ls
aa  aa.tar.gz  abc  bb  passwd  www
[root@localhost opt]# tar -jcf abc.tar.bz2 abc
[root@localhost opt]# ls
aa  aa.tar.gz  abc  abc.tar.bz2  bb  passwd  www
[root@localhost opt]# tar -Jcf bb.tar.bz2 bb
[root@localhost opt]# ls
aa         abc          bb          passwd
aa.tar.gz  abc.tar.bz2  bb.tar.bz2  www

[root@localhost opt]# ls
aa.tar.gz  abc.tar.bz2  bb.tar.bz2  passwd  www
[root@localhost opt]# tar xf aa.tar.gz 
[root@localhost opt]# ls
aa  aa.tar.gz  abc.tar.bz2  bb.tar.bz2  passwd  www
[root@localhost opt]# tar xf bb.tar.bz2 -C /tmp/
[root@localhost opt]# ls /tmp/
bb                  vmware-root_861-3988621786
ks-script-te4wylvn  vmware-root_890-2722107963

[root@localhost opt]# tar -zcvf aa.tar.gz aa
aa

[root@localhost opt]# tar -tf abc.tar.bz2 
abc

4.文本命令


4.1文本排序命令

sort //默认升序排序,不是按数值大小排序的
-n //根据数值大小进行排序
-r //逆序排序
-t //字段分隔符
-k //以哪个字段为关键字进行排序
-u //去重,排序后相同的行只显示一次
-f //排序时忽略字符大小写

[root@localhost ~]# sort -nr a.txt 
23
9
8
8
7
5
4
3
3
1
0

[root@localhost ~]# sort -n -t : -k 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@localhost ~]# sort -nu a.txt 
0
1
3
4
5
7
8
9
23

[root@localhost ~]# sort -nf a.txt 
0
A
b
1
3
4
5
7
8
8
23

4.2文本去重命令

uniq //报告重复的行(连续且完全相同方为重复)
-c //显示文件中行重复的次数
-d //只显示重复的行
-u //只显示未重复的行

[root@localhost ~]# wc -l a.txt 
12 a.txt
[root@localhost ~]# uniq a.txt |wc -l
11

[root@localhost ~]# uniq -c a.txt 
      1 0
      1 23
      2 3
      1 7
      1 4
      1 A
      1 5
      1 8
      1 b
      1 8
      1 1

[root@localhost ~]# uniq -d a.txt 
3

[root@localhost ~]# uniq -u a.txt 
0
23
7
4
A
5
8
b
8
1

4.3高级命令

sed awk grep 文本处理三剑客

sed //基于行的过滤和转换文本的流编辑器
// sed语法
sed -i ‘s///g’ // 全局修改
sed -i ‘s###g’ //全局修改
sed -i ‘s///2’ //修改第一字段与第二字段相重复的字段

[root@localhost ~]# cat a.txt 
hello world
hello runtime
hello hello hello 123
hello hello 123
hello tom
[root@localhost ~]# sed -i 's/hello/nihao/g' a.txt 
[root@localhost ~]# cat a.txt 
nihao world
nihao runtime
nihao nihao nihao 123
nihao nihao 123
nihao tom

[root@localhost ~]# sed -i 's/nihao/hi/2' a.txt 
[root@localhost ~]# cat a.txt 
nihao world
nihao runtime
nihao hi nihao 123
nihao hi 123
nihao tom

awk //基于列的文本报告工具

[root@localhost ~]# awk 'NR==1' /etc/passwd
root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# awk 'NR==1,NR==2' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost ~]# awk -F ':' 'NR==1 {print$3}' /etc/passwd
0

[root@localhost ~]# awk -F ':' 'NR==1 {print$7}' /etc/passwd
/bin/bash

[root@localhost ~]# awk -F ':' 'NR==1,NR=2 {print$7}' /etc/passwd
/bin/bash

[root@localhost ~]# awk -F ':' '{print$1}' /etc/passwd
root
bin
daemon
adm
lp
sync

[root@localhost ~]# awk -F ':' '{print$1,$3}' /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5

//最后一行
[root@localhost ~]# awk 'END{print}' /etc/passwd
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin

//扩展
[root@localhost ~]# rpm -ivh /mnt/BaseOS/Packages/net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/net-tools-2.0-0.52.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:net-tools-2.0-0.52.20160912git.el################################# [100%]
[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.223.144  netmask 255.255.255.0  broadcast 192.168.223.255
        inet6 fe80::cb72:96c8:9217:7b70  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:20:04:d3  txqueuelen 1000  (Ethernet)
RX packets 41648  bytes 51281572 (48.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22096  bytes 2284220 (2.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
//示例        
[root@localhost ~]# ifconfig | awk -F ' ' 'NR==2 {print$2}' 
192.168.223.144
[root@localhost ~]# ifconfig | awk -F ' ' 'NR==2 {print$4}' 
255.255.255.0
[root@localhost ~]# ifconfig | awk -F ' ' 'NR==3 {print$4}' 
64
[root@localhost ~]# ifconfig | cat -n | awk -F ' ' 'NR==10 {print$5}' 
65536
[root@localhost ~]# ifconfig | awk -F ' ' 'NR==4 {print$2}'
00:0c:29:20:04:d3
[root@localhost ~]# ifconfig | awk -F ' ' 'NR==4 {print$2}' | awk -F ':' '{print$3}'
29
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值