基础命令2

基础命令

压缩、解压缩命令

gzip //压缩后的文件以.gz结尾

[root@zwl ~]# ls
abc  anaconda-ks.cfg  test.sh
[root@zwl ~]# gzip abc
[root@zwl ~]# ls
abc.gz  anaconda-ks.cfg  test.sh

gunzip //解压

[root@zwl ~]# ls
abc.gz  anaconda-ks.cfg  test.sh
[root@zwl ~]# gunzip abc.gz
[root@zwl ~]# ls
abc  anaconda-ks.cfg  test.sh

bzip2 //压缩后的文件以.bz2结尾

[root@zwl ~]# ls
123  abc  anaconda-ks.cfg  test.sh
[root@zwl ~]# bzip2 abc
[root@zwl ~]# ls
123  abc.bz2  anaconda-ks.cfg  test.sh

bunzip2 //解压

[root@zwl ~]# ls
123  abc.bz2  anaconda-ks.cfg  test.sh
[root@zwl ~]# bunzip2 abc.bz2
[root@zwl ~]# ls
123  abc  anaconda-ks.cfg  test.sh

xz //压缩后的文件以.xz结尾

[root@zwl ~]# ls
123  abc  anaconda-ks.cfg  test.sh
[root@zwl ~]# xz abc
[root@zwl ~]# ls
123  abc.xz  anaconda-ks.cfg  test.sh

unxz //解压

[root@zwl ~]# ls
123  abc.xz  anaconda-ks.cfg  test.sh
[root@zwl ~]# unxz abc.xz
[root@zwl ~]# ls
123  abc  anaconda-ks.cfg  test.sh

zip //既归档又压缩的工具。zip可以压缩目录,\

[root@zwl ~]# ls
123  abc  anaconda-ks.cfg  test.sh
[root@zwl ~]# zip abc.zip abc
  adding: abc (deflated 10%)
[root@zwl ~]# ls
123  abc  abc.zip  anaconda-ks.cfg  test.sh

tar //归档工具,只归档不压缩

-c //创建归档文件
-f file.tar //操作的归档文件
-x //还原归档
-v //显示归档过程
-p //归档时保留权限信息。只有管理员才有权限用此选项
-C //将展开的归档文件保存至指定目录下
–delete //从归档文件中删除文件
–xattrs //在归档时保留文件的扩展属性信息
-tf /path/to/file.tar //不展开归档,直接查看归档了哪些文件

-zcf //归档并调用gzip压缩

[root@zwl ~]# ls
123  abc  abc.zip  anaconda-ks.cfg  test.sh
[root@zwl ~]# tar zcf abc.tar.gz abc
[root@zwl ~]# ls
123  abc  abc.tar.gz  abc.zip  anaconda-ks.cfg  test.sh

-zxf //调用gzip解压缩并展开归档

[root@zwl ~]# tar -zxf abc.tar.gz
[root@zwl ~]# ls
123  abc  abc.tar.gz  abc.zip  anaconda-ks.cfg  test.sh

查看文本

cat //拼接文件内容并输出至标准输出(屏幕)

[root@zwl ~]# cat abc
1 2 3
3 2 1
4 5 6
6 5 4
7 8 9
9 8 7
[root@zwl ~]# cat -n abc       //显示行号
     1  1 2 3
     2  3 2 1
     3  4 5 6
     4  6 5 4
     5  7 8 9
     6  9 8 7

head //从头部开始打印文件内容,默认打印10行

[root@zwl ~]# head anaconda-ks.cfg
#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
kexec-tools

[root@zwl ~]# head -n 5 anaconda-ks.cfg    //指定要打印的行数,可以是-n 5也可以是-5
#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

tail //查看文本文件尾部内容

[root@zwl ~]# tail anaconda-ks.cfg

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@zwl ~]# tail -n 5 anaconda-ks.cfg    //指定要打印的行数,可以是-n 5也可以是-5
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
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@zwl ~]# cat passwd | cut -d ":" -f1,3  
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:65534
dbus:81
systemd-coredump:999
systemd-resolve:193
tss:59
polkitd:998
unbound:997
sssd:996
chrony:995
sshd:74
admin:1000

sed //基于行的过滤和转换文本的流编辑器

[root@zwl ~]# cat passwd | head -n 1
root:x:0:0:root:/root:/bin/bash
[root@zwl ~]# sed 's/0/2/g' passwd | head -n 1
root:x:2:2:root:/root:/bin/bash
[root@zwl ~]#

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

[root@zwl ~]# awk -F ":" '{print $1,$5}' passwd | head -n 5
root root
bin bin
daemon daemon
adm adm
lp lp

文本排序

sort //默认升序排序,不是按数值大小排序的

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

[root@zwl ~]# sort -n abc   //根据数值大小进行排序
2 2 3
4 5 6
5 8 7
6 5 4
7 2 1
9 8 9
[root@zwl ~]# sort -r abc   //逆序排序
9 8 9
7 2 1
6 5 4
5 8 7
4 5 6
2 2 3

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

[root@zwl ~]# sort abc |uniq
2 2 3
4 5 6
5 8 7
6 5 4
7 2 1
9 8 9
[root@zwl ~]# sort abc | uniq -c
      1 2 2 3
      1 4 5 6
      1 5 8 7
      1 6 5 4
      1 7 2 1
      1 9 8 9
[root@zwl ~]# sort abc | uniq -d
[root@zwl ~]#
[root@zwl ~]# sort abc | uniq -u
2 2 3
4 5 6
5 8 7
6 5 4
7 2 1
9 8 9

文本统计

wc(word count)
-c //显示字节数
-l //显示行数
-w //显示单词数

[root@zwl ~]# wc anaconda-ks.cfg
  48  104 1248 anaconda-ks.cfg
[root@zwl ~]# wc -c anaconda-ks.cfg
1248 anaconda-ks.cfg
[root@zwl ~]# wc -l anaconda-ks.cfg
48 anaconda-ks.cfg
[root@zwl ~]# wc -w anaconda-ks.cfg
104 anaconda-ks.cfg

大小统计

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

[root@zwl ~]# du -hs abc
4.0K    abc

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

[root@zwl ~]# df abc
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/mapper/cs-root  17811456 1835752  15975704  11% /
[root@zwl ~]# df -h abc
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cs-root   17G  1.8G   16G  11% /
[root@zwl ~]# df -i abc
Filesystem           Inodes IUsed   IFree IUse% Mounted on
/dev/mapper/cs-root 8910848 34134 8876714    1% /

其它

time //显示命令的执行时间,例如time ls /etc

[root@zwl ~]# time ls |etc
-bash: etc: command not found

real    0m0.004s
user    0m0.001s
sys     0m0.003s

whoami //显示当前登录用户

[root@zwl ~]# whoami
root

w //显示当前在线用户并显示其在运行的命令

[root@zwl ~]# w
 18:04:01 up  1:42,  1 user,  load average: 0.87, 0.34, 0.13
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.159.1    16:25    1.00s  0.49s  0.01s w

who //查看当前在线用户

[root@zwl ~]# who
root     pts/0        2022-07-02 16:25 (192.168.159.1)

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

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

date //显示或设置日期与时间

[root@zwl ~]# date
Sat Jul  2 18:05:40 CST 2022

-s //以字符串方式设置时间
//格式化输出时间:+
%Y //年
%m //月
%d //日
%H //时
%M //分
%S //秒

[root@zwl ~]# date "+%Y,%m,%d,%H,%M,%S"
2022,07,02,18,08,19

ldd //查看指定程序有哪些依赖库文件

[root@zwl ~]# ldd /bin/logger
        linux-vdso.so.1 (0x00007ffc5110e000)
        libsystemd.so.0 => /lib64/libsystemd.so.0 (0x00007f4ba7abe000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f4ba76f9000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f4ba74f1000)
        liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f4ba72ca000)
        liblz4.so.1 => /lib64/liblz4.so.1 (0x00007f4ba70ad000)
        libcap.so.2 => /lib64/libcap.so.2 (0x00007f4ba6ea5000)
        libmount.so.1 => /lib64/libmount.so.1 (0x00007f4ba6c4b000)
        libgcrypt.so.20 => /lib64/libgcrypt.so.20 (0x00007f4ba692d000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f4ba6715000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4ba64f5000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4ba8014000)
        libblkid.so.1 => /lib64/libblkid.so.1 (0x00007f4ba62a2000)
        libuuid.so.1 => /lib64/libuuid.so.1 (0x00007f4ba609a000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f4ba5e70000)
        libgpg-error.so.0 => /lib64/libgpg-error.so.0 (0x00007f4ba5c4f000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f4ba5a4b000)
        libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007f4ba57c7000)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值