Linux基础命令二
压缩,解压缩命令
压缩格式:
gz,bz2,xz,tar
gzip //压缩后的文件以.gz结尾
[root@hwf ~]# gzip 1
[root@hwf ~]# ls
1.gz 2 3 anaconda-ks.cfg
-d //解压缩,解压完成后会删除原文件
[root@hwf ~]# gzip -d 1
[root@hwf ~]# ls
1 2 3 anaconda-ks.cfg
bzip2 //压缩后的文件以.bz2结尾
[root@hwf ~]# bzip2 2
[root@hwf ~]# ls
1 2.bz2 3 anaconda-ks.cfg
-d //解压缩,解压完成后会删除原文件
[root@hwf ~]# bzip2 -d 2.bz2
[root@hwf ~]# ls
1 2 3 anaconda-ks.cfg
xz //压缩后的文件以.xz结尾
[root@hwf ~]# xz 3
[root@hwf ~]# ls
1 2 3.xz anaconda-ks.cfg
-d //解压缩,解压完成后会删除原文件
[root@hwf ~]# xz -d 3.xz
[root@hwf ~]# ls
1 2 3 anaconda-ks.cfg
tar //归档工具,只归档不压缩
-c //创建归档文件
-f file.tar //操作的归档文件
[root@hwf ~]# tar zcf 1.tar.gz 1
[root@hwf ~]# ls
1 1.tar.gz 2 3 anaconda-ks.cfg
-C //将展开的归档文件保存至指定目录下
[root@hwf ~]# tar zcf /tmp/2.tar.gz 2 //直接跟目录名即可
[root@hwf ~]# ls /tmp/
1.tar.gz vmware-root_936-2697532681
2.tar.gz vmware-root_939-4022308693
ks-script-htn3bwki vmware-root_949-4021784396
ks-script-s10ge1er vmware-root_960-2999133023
vmware-root_932-2722632322
-zcf //归档并调用gzip压缩
[root@hwf ~]# tar zcf 1.tar.gz 1 //压缩后保存原文件
[root@hwf ~]# ls
1 1.tar.gz 2 3 anaconda-ks.cfg
-zxf //调用gzip解压缩并展开归档
[root@hwf ~]# tar -zxf 1.tar.gz //解压后保存压缩文件
[root@hwf ~]# ls
1 1.tar.gz 2 3 anaconda-ks.cfg
-jcf //归档并调用bzip2压缩
[root@hwf ~]# tar jcf 2.tar.bz2 2
[root@hwf ~]# ls
1 1.tar.gz 2 2.tar.bz2 3 anaconda-ks.cfg
-jxf //调用bzip2解压缩并展开归档
[root@hwf ~]# tar -jxf 2.tar.bz2
[root@hwf ~]# ls
1 1.tar.gz 2 2.tar.bz2 3 anaconda-ks.cfg
-Jcf //归档并调用xz压缩
[root@hwf ~]# tar Jcf 3.tar.xz 3
[root@hwf ~]# ls
1 1.tar.gz 2 2.tar.bz2 3 3.tar.xz anaconda-ks.cfg
Jxf //调用xz解压缩并展开归档
[root@hwf ~]# ls
1 1.tar.gz 2 2.tar.bz2 3 3.tar.xz anaconda-ks.cfg
查看文本
cat //拼接文件内容并输出至标准输出(屏幕)
-n 显示行号
[root@hwf a]# cat -n 1
1 jkasdhasdashdssdasuyhdi asjdi hasiud ajkd hiua dhoiqa hdwqoii dhoasn doia hdo oi
2 asda
3 asd
4 asd
5 a
6 das
more //全屏查看文本文件内容,只能从前往后,不能从后往前。文件内容显示完后自动退出
[root@hwf ~]# more anaconda-ks.cfg
#version=RHEL8
Use graphical install
graphical
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-00
00-cdrom/AppStream
***省略较多内容***
less //全屏查看文本文件内容,可从前往后亦可从后往前。推荐使用
less [需要查看的文件名]
head //从头部开始打印文件内容,默认打印10行
-n //指定要打印的行数,可以是-n 15也可以是-15
[root@hwf ~]# head -5 anaconda-ks.cfg
#version=RHEL8
Use graphical install
graphical
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
tail //查看文本文件尾部内容
-n //指定要打印的行数,可以是-n 15也可以是-15
[root@hwf ~]# tail -3 anaconda-ks.cfg
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
文本处理
cut //本内容
-d //指定字段分隔符,默认是空格
-f //指定要显示的字段
-f 1,3 显示1和3
-f 1-3 显示1到3
[root@hwf ~]# cat /etc/passwd | cut -d ":" -f 1,3
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
sed //基于行的过滤和转换文本的流编辑器
[root@hwf ~]# sed 's/0/1/' /etc/passwd |head -2 修改的内容是临时的
root:x:1:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
/g //修改全部需要修改的内容
-i //修改的文件内容永久保存,写入配置文件
[root@hwf a]# cat 1
jkasdhasdashdssdasuyhdi asjdi hasiud ajkd hiua dhoiqa hdwqoii dhoasn doia hdo oi
asda
asd
asd
a
das
[root@hwf a]# sed -i 's/a/b/g' 1
[root@hwf a]# cat 1
jkbsdhbsdbshdssdbsuyhdi bsjdi hbsiud bjkd hiub dhoiqb hdwqoii dhobsn doib hdo oi
bsdb
bsd
bsd
b
dbs
awk //基于列的文本报告工具
-F ‘’ //指定分隔符,可以使用正则表达式[]指定多个分隔符
执行动作:
{print $#}’ //选择打印第#列,
0
代表所有列,
0代表所有列,
0代表所有列,NF代表最后1列
[root@hwf ~]# awk -F : '{print $1,$6}' /etc/passwd |head -2
root /root
bin /bin
文件排序
sort //默认升序排序,不是按数值大小排序的
-n //根据数值大小进行排序
[root@hwf a]# sort -n 2
1 2 3
1 5 3
3 2 4
6 7 4
7 5 2
8 4 2
-r //逆序排序
[root@hwf a]# sort -r 2
8 4 2
7 5 2
6 7 4
3 2 4
1 5 3
1 2 3
-k //以哪个字段为关键字进行排序
[root@hwf a]# sort -k 1 2
1 2 3
1 5 3
3 2 4
6 7 4
7 5 2
8 4 2
-u /去重,排序后相同的行只显示一次
[root@hwf a]# sort -u 2
1 2 3
1 2 3
1 5 3
3 2 4
6 7 4
7 5 2
8 4 2
-f //排序时忽略字符大小写
[root@hwf a]# sort -f 2
1 2 3
1 5 3
3 2 4
6 7 4
7 5 2
8 4 2
A B C
a b c
uniq //报考重复的行(连续且完全相同方为重复)
-c //显示文件中行重复的次数
[root@hwf a]# uniq -c 2
1 1 2 3
1 3 2 4
1 1 5 3
1 6 7 4
1 7 5 2
1 8 4 2
1 a b c
1 A B C
-u //只显示未重复的行
[root@hwf a]# uniq -u 2
1 2 3
3 2 4
1 5 3
6 7 4
7 5 2
8 4 2
a b c
A B C
1 2 3
3 2 4
文本统计
wc (word count)
-c //显示字节数
[root@hwf a]# wc -c 2
24 2
-l //显示行数
[root@hwf a]# wc -l 2
4 2
-w //显示单词数
[root@hwf a]# wc -w 2
12 2
大小统计
du //查看文件或目录占用的磁盘空间大小
-h //以人类友好的方式显示大小
-s //显示总的占用空间大小
[root@hwf a]# du -hs 2
4.0K 2
df //报告文件系统磁盘空间使用情况
-h //以人类友好的方式显示大小
-i //显示inode信息
[root@hwf a]# df -hi
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 460K 384 459K 1% /dev
tmpfs 464K 1 464K 1% /dev/shm
tmpfs 464K 640 464K 1% /run
tmpfs 464K 17 464K 1% /sys/fs/cgroup
/dev/mapper/cl-root 35M 35K 35M 1% /
/dev/sda1 512K 310 512K 1% /boot
/dev/mapper/cl-home 23M 3 23M 1% /home
tmpfs 464K 5 464K 1% /run/user/0
主机名管理
hostname //查看或临时修改主机名,重开终端有效,重启失效
[root@hwf ~]# hostname 123
[root@hwf ~]# hostname
123
hostnamectl //查看或永久修改主机名,重开终端生效,重启依然有效
[root@hwf ~]# hostnamectl set-hostname huang
[root@hwf ~]# hostname
huang
其他
time //显示命令的执行时间,例如time ls /etc
[root@hwf ~]# time sleep 3
real 0m3.004s
user 0m0.001s
sys 0m0.000s
clear //快捷键:CTRL+l清屏
whoami //显示当前登录用户
[root@hwf ~]# whoami
root
w //显示当前在线用户并显示其在运行的命令
[root@hwf ~]# w
21:22:17 up 11:01, 3 users, load average: 0.05, 0.08, 0.08
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 09:05 12:16m 0.06s 0.06s -bash
root pts/0 192.168.159.1 19:10 0.00s 0.38s 0.00s w
root pts/1 192.168.159.1 19:10 2:11m 8.86s 8.86s top
who //查看当前在线用户
[root@hwf ~]# who
\root tty1 Jul 1 09:05
root pts/0 Jul 1 19:10 (192.168.159.1)
root pts/1 Jul 1 19:10 (192.168.159.1)
which //显示指定命令的绝对路径
[root@hwf ~]# which echo
/usr/bin/echo
date //显示或设置日期与时间
[root@hwf ~]# date '+%Y-%m-%d %H:%M:%S'
2022-07-01 21:26:28
ldd //查看指定程序有哪些依赖库文件
[root@hwf ~]# ldd /bin/chown
linux-vdso.so.1 (0x00007ffffa3fb000)
libc.so.6 => /lib64/libc.so.6 (0x00007f0a30d4c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0a31322000)
绝对路径
```bash
[root@hwf ~]# which echo
/usr/bin/echo
date //显示或设置日期与时间
[root@hwf ~]# date '+%Y-%m-%d %H:%M:%S'
2022-07-01 21:26:28
ldd //查看指定程序有哪些依赖库文件
[root@hwf ~]# ldd /bin/chown
linux-vdso.so.1 (0x00007ffffa3fb000)
libc.so.6 => /lib64/libc.so.6 (0x00007f0a30d4c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0a31322000)
2320

被折叠的 条评论
为什么被折叠?



