linux自学笔记(二)

磁盘和文件系统管理

#查看磁盘使用情况
[root@192 ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  886M     0  886M   0% /dev
tmpfs               tmpfs     904M     0  904M   0% /dev/shm
tmpfs               tmpfs     904M  8.7M  895M   1% /run
tmpfs               tmpfs     904M     0  904M   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        26G  1.5G   25G   6% /
/dev/sda1           ext4      976M  136M  774M  15% /boot
tmpfs               tmpfs     181M     0  181M   0% /run/user/0
tmpfs               tmpfs     181M     0  181M   0% /run/user/1000
[root@192 ~]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
devtmpfs               907128       0    907128   0% /dev
tmpfs                  924780       0    924780   0% /dev/shm
tmpfs                  924780    8856    915924   1% /run
tmpfs                  924780       0    924780   0% /sys/fs/cgroup
/dev/mapper/cl-root  27245572 1547844  25697728   6% /
/dev/sda1              999320  138464    792044  15% /boot
tmpfs                  184956       0    184956   0% /run/user/0
tmpfs                  184956       0    184956   0% /run/user/1000
[root@192 ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             886M     0  886M   0% /dev
tmpfs                904M     0  904M   0% /dev/shm
tmpfs                904M  8.7M  895M   1% /run
tmpfs                904M     0  904M   0% /sys/fs/cgroup
/dev/mapper/cl-root   26G  1.5G   25G   6% /
/dev/sda1            976M  136M  774M  15% /boot
tmpfs                181M     0  181M   0% /run/user/0
tmpfs                181M     0  181M   0% /run/user/1000
[root@192 ~]#
​
#查看文件占用情况
[root@192 ~]# du -h
44K     .
[root@192 ~]# du -ah
4.0K    ./.bash_logout
4.0K    ./.bash_profile
4.0K    ./.bashrc
4.0K    ./.cshrc
4.0K    ./.tcshrc
4.0K    ./anaconda-ks.cfg
4.0K    ./.bash_history
4.0K    ./.lesshst
4.0K    ./test
4.0K    ./ll
4.0K    ./test1
44K     .
[root@192 ~]#

硬链接与符号连接:ln

#不加参数创建硬链接、-s 创建软连接
[root@192 ~]# ll
total 16
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-r--r--. 1 root root  611 Mar 26 03:19 ll
-rw-r--r--. 1 root root  266 Mar 26 01:17 test
-rw-r--r--. 1 root root   81 Mar 26 03:36 test1
drwxr-xr-x. 2 root root    6 Mar 26 03:43 testdir
lrwxrwxrwx. 1 root root    7 Mar 26 03:47 testdirln -> testdir
lrwxrwxrwx. 1 root root    4 Mar 26 03:44 testln -> test
[root@192 ~]# ll -i
total 16
34072132 -rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
34072184 -rw-r--r--. 1 root root  611 Mar 26 03:19 ll
34072183 -rw-r--r--. 1 root root  266 Mar 26 01:17 test
34072186 -rw-r--r--. 1 root root   81 Mar 26 03:36 test1
16808713 drwxr-xr-x. 2 root root    6 Mar 26 03:43 testdir
34072185 lrwxrwxrwx. 1 root root    7 Mar 26 03:47 testdirln -> testdir
34072182 lrwxrwxrwx. 1 root root    4 Mar 26 03:44 testln -> test
[root@192 ~]# ln test1 testln1
[root@192 ~]# ll -i
total 20
34072132 -rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
34072184 -rw-r--r--. 1 root root  611 Mar 26 03:19 ll
34072183 -rw-r--r--. 1 root root  266 Mar 26 01:17 test
34072186 -rw-r--r--. 2 root root   81 Mar 26 03:36 test1
16808713 drwxr-xr-x. 2 root root    6 Mar 26 03:43 testdir
34072185 lrwxrwxrwx. 1 root root    7 Mar 26 03:47 testdirln -> testdir
34072182 lrwxrwxrwx. 1 root root    4 Mar 26 03:44 testln -> test
34072186 -rw-r--r--. 2 root root   81 Mar 26 03:36 testln1
[root@192 ~]#
​

观察磁盘分区情况

[root@192 ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             886M     0  886M   0% /dev
tmpfs                904M     0  904M   0% /dev/shm
tmpfs                904M  8.7M  895M   1% /run
tmpfs                904M     0  904M   0% /sys/fs/cgroup
/dev/mapper/cl-root   26G  1.5G   25G   6% /
/dev/sda1            976M  136M  774M  15% /boot
tmpfs                181M     0  181M   0% /run/user/0
​
[root@192 ~]# lsblk
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda           8:0    0  30G  0 disk
├─sda1        8:1    0   1G  0 part /boot
└─sda2        8:2    0  29G  0 part
  ├─cl-root 253:0    0  26G  0 lvm  /
  └─cl-swap 253:1    0   3G  0 lvm  [SWAP]
sr0          11:0    1   7G  0 rom
[root@192 ~]#
​
​
[root@192 ~]# parted /dev/sda print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
​
Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1075MB  1074MB  primary  ext4         boot
 2      1075MB  32.2GB  31.1GB  primary               lvm
​
[root@192 ~]# blkid
/dev/sda1: UUID="64ec4d3a-d3ea-4aa6-931e-1fe06bc6a11c" TYPE="ext4" PARTUUID="5d0c8e2f-01"
/dev/sda2: UUID="y4NDvp-3WGy-oAPF-ceTb-OP7n-BeiW-RxcP3F" TYPE="LVM2_member" PARTUUID="5d0c8e2f-02"
/dev/sr0: UUID="2020-01-03-21-42-40-00" LABEL="CentOS-8-1-1911-x86_64-dvd" TYPE="iso9660" PTUUID="087a73cc" PTTYPE="dos"
/dev/mapper/cl-root: UUID="489bc07d-91d5-4c42-bea8-7ed0f59c9cf7" TYPE="xfs"
/dev/mapper/cl-swap: UUID="5c75ef5e-64d4-4eeb-b152-4fad4e711f73" TYPE="swap"
​

磁盘分区

[root@192 ~]# fdisk /dev/sda
​
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
​
​
Command (m for help): m
​
Help:
​
  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag
​
  Generic
   d   delete a partition   #删除一个分区
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition  #新增一个分区
   p   print the partition table    #在屏幕上显示分区表
   t   change a partition type
   v   verify the partition table
   i   print information about a partition
​
  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)
​
  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file
​
  Save & Exit
   w   write table to disk and exit #保存
   q   quit without saving changes  #不保存推退出
​
  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table
​
​
Command (m for help):

磁盘格式化

xfs文件格式化

[root@192 ~]# grep  'processor' /proc/cpuinfo
processor       : 0
processor       : 1
[root@192 ~]# mkfs.xfs -f -d agcount=2 /dev/sda3
meta-data=/dev/sda3              isize=512    agcount=2, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@192 ~]#

ext4文件格式化

[root@192 ~]# mkfs.ext4  /dev/sda3
mke2fs 1.44.6 (5-Mar-2019)
/dev/sda3 contains a ext4 file system
        created on Sat Mar 26 18:26:53 2022
Proceed anyway? (y,N) y
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: d418717d-a381-405d-a996-8d0a8efb39ef
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736
​
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
​
[root@192 ~]#

文件系统检验

#xfs文件系统
[root@192 ~]# xfs_repair /dev/sda3
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
Phase 5 - rebuild AG headers and trees...
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done
[root@192 ~]#
#ext4文件系统检测#
[root@192 ~]# fsck.ext4 /dev/sda3
e2fsck 1.44.6 (5-Mar-2019)
/dev/sda3: clean, 11/327680 files, 42078/1310720 blocks
[root@192 ~]# fsck.ext4 -f /dev/sda3
e2fsck 1.44.6 (5-Mar-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda3: 11/327680 files (0.0% non-contiguous), 42078/1310720 blocks
[root@192 ~]#
​

文件系统挂载

[root@192 ~]# mount UUID="aabdb654-abcc-460f-bd36-42d4122db3e1" /root/mounttest/

[root@192 ~]# mkdir  mounttest
[root@192 ~]# ls
anaconda-ks.cfg  ll  mounttest  test  test1  testdir  testdirln  testln  testln1
[root@192 ~]# blkid
/dev/sr0: UUID="2020-01-03-21-42-40-00" LABEL="CentOS-8-1-1911-x86_64-dvd" TYPE="iso9660" PTUUID="087a73cc" PTTYPE="dos"
/dev/sda1: UUID="64ec4d3a-d3ea-4aa6-931e-1fe06bc6a11c" TYPE="ext4" PARTUUID="5d0c8e2f-01"
/dev/sda2: UUID="y4NDvp-3WGy-oAPF-ceTb-OP7n-BeiW-RxcP3F" TYPE="LVM2_member" PARTUUID="5d0c8e2f-02"
/dev/sda3: UUID="aabdb654-abcc-460f-bd36-42d4122db3e1" TYPE="xfs" PARTUUID="5d0c8e2f-03"
/dev/mapper/cl-root: UUID="489bc07d-91d5-4c42-bea8-7ed0f59c9cf7" TYPE="xfs"
/dev/mapper/cl-swap: UUID="5c75ef5e-64d4-4eeb-b152-4fad4e711f73" TYPE="swap"
[root@192 ~]# mount UUID="aabdb654-abcc-460f-bd36-42d4122db3e1" /root/mounttest/
[root@192 ~]#   lsblk
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda           8:0    0  35G  0 disk
├─sda1        8:1    0   1G  0 part /boot
├─sda2        8:2    0  29G  0 part
│ ├─cl-root 253:0    0  26G  0 lvm  /
│ └─cl-swap 253:1    0   3G  0 lvm  [SWAP]
└─sda3        8:3    0   5G  0 part /root/mounttest
sr0          11:0    1   7G  0 rom
[root@192 ~]# cd mounttest/
[root@192 mounttest]# touch test
[root@192 mounttest]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 26 18:44 test
[root@192 mounttest]#
#卸载目录
[root@192 mounttest]# umount /root/mounttest/
umount: /root/mounttest/: target is busy.  #在这个目录位置无法卸载
[root@192 mounttest]# cd ../
[root@192 ~]# umount /root/mounttest/
​

设置启动挂载

设置开机自动挂载/dev/sda3挂载到 /root/mounttest

​
[root@192 ~]# lsblk
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda           8:0    0  35G  0 disk
├─sda1        8:1    0   1G  0 part /boot
├─sda2        8:2    0  29G  0 part
│ ├─cl-root 253:0    0  26G  0 lvm  /
│ └─cl-swap 253:1    0   3G  0 lvm  [SWAP]
└─sda3        8:3    0   5G  0 part /root/mounttest
sr0          11:0    1   7G  0 rom
[root@192 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Mar 22 05:09:47 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=64ec4d3a-d3ea-4aa6-931e-1fe06bc6a11c /boot                   ext4    defaults        1 2
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
/dev/sda3               /root/mounttest         xfs     defaults        0 0
[root@192 ~]#
​

文件与文件系统的压缩

#单一文件
[root@192 ~]# gzip fiel.txt #压缩
[root@192 ~]# ll
total 12
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-r--r--. 1 root root   35 Mar 26 19:45 fiel.txt.gz
drwxr-xr-x. 2 root root   18 Mar 26 18:44 mounttest
-rw-r--r--. 1 root root  266 Mar 26 01:17 test
[root@192 ~]# zcat fiel.txt.gz  #查看压缩文件内容
sdasdasdas
[root@192 ~]# gzip -d fiel.txt  #解压缩
[root@192 ~]# ll
total 12
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-r--r--. 1 root root   11 Mar 26 19:45 fiel.txt
drwxr-xr-x. 2 root root   18 Mar 26 18:44 mounttest
-rw-r--r--. 1 root root  266 Mar 26 01:17 test
[root@192 ~]#
​

tar命令

#多个文件
tar {A|c|d|r|t|u|x}[GnSkUWOmpsMBiajJzZhPlRvwo] [ARG...]
-c 打包文件-    t 查看包文件     -x 解包  -j bz2文件后缀 -J xz文件后缀 -z gz文件后缀
​
[root@192 ~]# tar -zcv -f testfile3.tar.gz testfile3
testfile3
[root@192 ~]# ll
total 20
-rw-r--r--. 1 root root    0 Mar 26 19:51 testfile3
-rw-r--r--. 1 root root  115 Mar 26 20:11 testfile3.tar.gz
[root@192 ~]# tar -ztv -f testfile3.tar.gz testfile3
-rw-r--r-- root/root         0 2022-03-26 19:51 testfile3
[root@192 ~]# rm testfile3
rm: remove regular empty file 'testfile3'? yes
[root@192 ~]# ll
total 20
-rw-r--r--. 1 root root  115 Mar 26 20:11 testfile3.tar.gz
[root@192 ~]# tar -zxv -f testfile3.tar.gz
testfile3
[root@192 ~]# ll
total 20
-rw-r--r--. 1 root root    0 Mar 26 19:51 testfile3
-rw-r--r--. 1 root root  115 Mar 26 20:11 testfile3.tar.gz
[root@192 ~]#
​

XFS文件备份还原 xfsdump 备份 xfsrestor 还原

vi和vim编辑器

命令说明
0一行最前面
$一行最后面
H、M、L屏幕第一个字符、中间、对后一个
G文章最后一行
:set nu / nonu= 设置行号/ 取消行号
gg文章第一行
1::::
/word查找字符
n/N向下朝朝/向上查找
x/X后删除/前删除
dd删除行
yy复制
p粘贴
J光标所在行和下一行结合成1行
o/O在光标所在前插入一行输入数据/在光标所在后插入一行输入数据
i/i光标前后输入数据
w、q 、!保存、退出、强制
r file_name在光标后插入另一个文件内容
n1:n2 w file_name将n1到n2行的内容保存到另一个文件
Ctrl V区块选择
sp file_name
ctrl w +j切换窗口

 

 

shell

shell

[root@192 ~]# echo $SHELL
/bin/bash

 

变量设置规则

变量名称只能是数字字母,不能以数字开头

用 ”=“ 连接

[root@192 ~]# myname=lxf
[root@192 ~]# echo ${myname}
lxf
[root@192 ~]# myinfo="myname is $myname"
[root@192 ~]# echo ${myinfo}
myname is lxf
#命令作为变量名称
[root@192 ~]# version=$(uname -r)
[root@192 ~]# echo $version
4.18.0-147.el8.x86_64
#追加变量内容
[root@192 ~]# myname=${myname}:jzj
[root@192 ~]# echo $myname
lxf:jzj
[root@192 ~]# myname=${myname}jzj
[root@192 ~]# echo $myname
lxf:jzjjzj
#取消变量
[root@192 ~]# unset myname
​

环境变量

[root@192 ~]# env
SSH_CONNECTION=192.168.1.2 54800 192.168.1.8 22
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=192.168.1.8
OLDPWD=/root/root
XDG_SESSION_ID=1
USER=root
SELINUX_ROLE_REQUESTED=
PWD=/root
HOME=/root
SSH_CLIENT=192.168.1.2 54800 22
SELINUX_LEVEL_REQUESTED=
SSH_TTY=/dev/pts/0
MAIL=/var/spool/mail/root
TERM=xterm
SHELL=/bin/bash
SELINUX_USE_CURRENT_RANGE=
SHLVL=1
LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
HISTSIZE=1000
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env
[root@192 ~]#

read 从键盘输入
[root@192 / 22:08]#read myname
lxf
[root@192 / 22:10]#echo $myname
lxf
#数组

[root@192 / 22:12]#arr[2]="2"
[root@192 / 22:12]#arr[1]="1"
[root@192 / 22:12]#echo $arr[2]
[2]
[root@192 / 22:12]#echo "${arr[1]},${arr[2]}"
1,2
[root@192 / 22:13]#

变量内容的删除、取代、替换

root@192 / 22:19]#echo $path
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@192 / 22:19]#echo ${path#/*usr/local/sbin:}
/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@192 / 22:20]#echo ${path##/*:}
/root/bin
[root@192 / 22:21]#

[root@192 ~ 22:28]#alias vim=/bin/vi #设置别名
[root@192 ~ 22:28]#alias  #查看别名
alias rm='rm -i'
alias vim='/bin/vi'
alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-
[root@192 ~ 22:28]#unalias vim  #取消别名
[root@192 ~ 22:28]#alias
alias rm='rm -i'
alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-
[root@192 ~ 22:28]#

#history 相关操作
  886  ll
  887  history
[root@192 ~ 22:32]#!886
ll
total 20
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
drwxr-xr-x. 2 root root   18 Mar 26 19:47 mounttest
-rw-r--r--. 1 root root  135 Mar 26 21:18 test.test
-rw-r--r--. 1 root root  135 Mar 26 21:23 test1.test
-rw-r--r--. 1 root root  135 Mar 26 21:23 test2.test
[root@192 ~ 22:32]#!!  #执行上一条命令
ll
total 20
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
drwxr-xr-x. 2 root root   18 Mar 26 19:47 mounttest
-rw-r--r--. 1 root root  135 Mar 26 21:18 test.test

命令判断依据

cmd1 || cmd2 或 cmd1 && cmd2 且

[root@192 ~]# ll /root/dir || touch dir.txt
ls: cannot access '/root/dir': No such file or directory
[root@192 ~]# ll
-rw-r--r--. 1 root root    0 Mar 26 23:47 dir.txt
[root@192 ~]# ll /root/dir &&  touch dir1.txt
ls: cannot access '/root/dir': No such file or directory
[root@192 ~]# ll
total 24
-rw-r--r--. 1 root root    0 Mar 26 23:47 dir.txt
[root@192 ~]# ll /root/mounttest &&  touch /root/mounttest/dir1.txt
total 0
-rw-r--r--. 1 root root 0 Mar 26 18:44 test
[root@192 ~]#
[root@192 ~]# cd mounttest/
[root@192 mounttest]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 26 23:48 dir1.txt
[root@192 mounttest]#

数据流重定向

标准输入:< 、<<

标准输出:>、>>

标准错误输出:2>、2>>

标准输出和标准错误输出一起输出:&>等价 2>&1

[root@192 ~]# ll /root/ls &> ll.log
[root@192 ~]# cat ll.log
ls: cannot access '/root/ls': No such file or directory
[root@192 ~]# ll /root &> ll.log
[root@192 ~]# cat ll.log
total 8
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
-rw-r--r--. 1 root root    0 Mar 26 23:55 ll.log
drwxr-xr-x. 2 root root   34 Mar 26 23:48 mounttest
[root@192 ~]#

管道符[ | ]

管道符后第一个数据必定是命令 ,且这个命令能够接受标准输入的数据。

例如:less、more、head、tail等都是管道符命令

例如:ls、cp、mv等就不是

#cut 分割数据
[root@192 ~]# cat ll.log
total 8
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
-rw-r--r--. 1 root root    0 Mar 27 00:20 ll.log
drwxr-xr-x. 2 root root   50 Mar 27 00:18 mounttest
[root@192 ~]# cat ll.log | cut -d ' ' -f 1,2,3,4
total 8
-rw-------. 1 root root
-rw-------. 1 root root
-rw-r--r--. 1 root root
drwxr-xr-x. 2 root root
#grep 支持正则表达式
[root@192 ~]# cat ll.log | grep root
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
-rw-r--r--. 1 root root    0 Mar 27 00:20 ll.log
drwxr-xr-x. 2 root root   50 Mar 27 00:18 mounttest
[root@192 ~]#
#sort 排序的使用
[root@192 ~]# cat ll.log | cut -d ' ' -f 2 | sort -n
1
1
1
2
8
[root@192 ~]# cat ll.log | sort -t ' ' -n -k 2
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
-rw-r--r--. 1 root root    0 Mar 27 00:20 ll.log
drwxr-xr-x. 2 root root   50 Mar 27 00:18 mounttest
total 8
#列出重复的数据此时
[root@192 ~]# cat ll.log | cut -d ' ' -f 2 | uniq -c
      1 8
      3 1
      1 2
#统计多少行
[root@192 ~]# cat ll.log | cut -d ' ' -f 2 | wc
      5       5      10
[root@192 ~]# cat ll.log | cut -d ' ' -f 2 | wc -l   #行数
5
[root@192 ~]# cat ll.log | cut -d ' ' -f 2 | wc -w	 #多少字
5
[root@192 ~]# cat ll.log | cut -d ' ' -f 2 | wc -m	 #多少字符		
10

双向重定向

#tee 输入到文件同时输出到屏幕
[root@192 ~]# ping 192.168.1.1 | tee ping.log
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.54 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.08 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=1.95 ms
^C
[root@192 ~]# ll
total 16
-rw-------. 1 root root 1377 Mar 22 05:13 anaconda-ks.cfg
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
-rw-r--r--. 1 root root  228 Mar 27 00:20 ll.log
drwxr-xr-x. 2 root root   50 Mar 27 00:18 mounttest
-rw-r--r--. 1 root root  227 Mar 27 00:35 ping.log
[root@192 ~]#

字符转换命令 tr、col、join、paste、expand

#tr
[root@192 ~]# cat ping.log
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms
64 bytes from 192.168.1.1: icmp_seq=206 ttl=64 time=1.52 ms
#删除指定字符串
[root@192 ~]# cat ping.log | tr -d from
64 bytes  192.168.1.1: icp_seq=1 ttl=64 tie=1.75 s
64 bytes  192.168.1.1: icp_seq=2 ttl=64 tie=1.86 s
64 bytes  192.168.1.1: icp_seq=206 ttl=64 tie=1.52 s
#将小写换成大写
[root@192 ~]# cat ping.log | tr '[a-z]' '[A-Z]'
64 BYTES FROM 192.168.1.1: ICMP_SEQ=1 TTL=64 TIME=1.75 MS
64 BYTES FROM 192.168.1.1: ICMP_SEQ=2 TTL=64 TIME=1.86 MS
64 BYTES FROM 192.168.1.1: ICMP_SEQ=206 TTL=64 TIME=1.52 MS
[root@192 ~]#
#join 两个文件要用共同点
[root@192 ~]# head -n 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
[root@192 ~]# head -n 3 /etc/shadow
root:$6$pu3H2LauUZyuWUto$0xWrLZgEbT2zegn0zGINhmqdJfyawY.Ta0D8TpxRh5LCMeXJVlNEnei2eJWw4KZnDMfx3z9/ItDcOMP9QL9Em/::0:99999:7:::
bin:*:18078:0:99999:7:::
daemon:*:18078:0:99999:7:::
[root@192 ~]# join -t ':' /etc/passwd /etc/shadow | head -n 3
root:x:0:0:root:/root:/bin/bash:$6$pu3H2LauUZyuWUto$0xWrLZgEbT2zegn0zGINhmqdJfyawY.Ta0D8TpxRh5LCMeXJVlNEnei2eJWw4KZnDMfx3z9/ItDcOMP9QL9Em/::0:99999:7:::
bin:x:1:1:bin:/bin:/sbin/nologin:*:18078:0:99999:7:::
daemon:x:2:2:daemon:/sbin:/sbin/nologin:*:18078:0:99999:7:::
[root@192 ~]# join -t ':' -1 4 /etc/passwd -2 4 /etc/shadow | head -n 3
0:root:x:0:root:/root:/bin/bash:root:$6$pu3H2LauUZyuWUto$0xWrLZgEbT2zegn0zGINhmqdJfyawY.Ta0D8TpxRh5LCMeXJVlNEnei2eJWw4KZnDMfx3z9/ItDcOMP9QL9Em/::99999:7:::
join: 0:root:x:0:root:/root:/bin/bash:bin:*:18078:99999:7:::
0:root:x:0:root:/root:/bin/bash:daemon:*:18078:99999:7:::
/etc/passwd:6: is not sorted: sync:x:5:0:sync:/sbin:/bin/sync
[root@192 ~]#
#paste 将第二个文件内容与第一个文件内容 tab 键隔开 
[root@192 ~]# paste anaconda-ks.cfg anaconda-ks.cfg

#expand 将tab 转换成空格
expand -t 5 file_name    #默认tab 是8个空格 可以用 -t 修改

split 将大文件拆分成小文件

[root@192 ~]# split -b 300b anaconda-ks.cfg.bk anac
[root@192 ~]# ll
total 24
-rw-r--r--. 1 root root 1377 Mar 27 00:58 anacaa
-rw-------. 1 root root 1377 Mar 26 20:38 anaconda-ks.cfg.bk
[root@192 ~]# ll

正则表达式

[root@192 ~]# grep -n '20' ping.log  #指定字符所在行
3:64 bytes from 192.168.1.1: icmp_seq=206 ttl=64 time=1.52 ms
[root@192 ~]# grep -vn '20' ping.log #不含指定字符的行
1:64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms
2:64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms
[root@192 ~]# grep -in 'a' ping.log  #不区分大小写
2:64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms  A
3:64 bytes from 192.168.1.1: icmp_seq=206 ttl=64 time=1.52 ms a
[root@192 ~]# grep -n 'f[sb]om' ping.log   #[]或的关系s或b 不能是sb
1:64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms  fbom
2:64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms  fsom

RE字符说明
^word查找特定字符开头的行:grep -n '^#' anaconda-ks.cfg
word$查找特定字符结尾的行:grep -n 'end$' anaconda-ks.cfg
.通配符 1个任意字符
\转义符 grep -n \" anaconda-ks.cfg
*重复前一个字符 grep -n 'add*' anaconda-ks.cfg #d*代表0或多个
[list]#[]或的关系s或b 不能是sb grep -n 'f[sb]om' ping.log
[n1-n2]两个字符之间的连续字符 [a-z] 、[A-Z]、[1-9]
[^list]非指定字符 [^A-Z]
\{n,m\}连续n到m个字符 grep -n 'ab\{2,\3}' anaconda-ks.cfg #abb、abbb 为结果
+前一个字符1个或1个以上
前一个字符0个或1个
|
()找出群组字符串 god gld = g(o|l)d
()+多个重复的字符串 AasdasdasdQ =A(asd)+Q

sed

   sed -option [操作]

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

option说明
-nsed操作过的行才会显示
-e直接在命令行模式进行sed编辑
-i直接修改读取文件内容
n1,n2在指定n1,n2 行直接操作
操作说明
a在下一行新增
c替换cword
d删除
i在前一行新增
p打印
s替换 正则表达式

#显示2、3行
[root@192 ~]# nl ping.log | sed -n '2,3p'
     2  64 bytes fsbom 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms  fsom
     3  64 bytes from 192.168.1.1: icmp_seq=206 ttl=64 time=1.52 ms a
#删除2-22行
[root@192 /]# nl /etc/passwd | sed '2,21d'
     1  root:x:0:0:root:/root:/bin/bash
    22  centos8_user:x:1000:1000:centos8_user:/home/centos8_user:/bin/bash
#在第2行前后(i\a)插入指定字符
[root@192 ~]# nl ping.log | sed '2a drink tea'
     1  64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms  fbom
     2  64 bytes fsbom 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms  fsom
drink tea
  [root@192 ~]# nl ping.log | sed '2i drink tea'
     1  64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms  fbom
drink tea
     2  64 bytes fsbom 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms  fsom
#\n换行 插入2行
[root@192 ~]# nl ping.log | sed '2i drink tea \n aaaaaa'
     1  64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms  fbom
drink tea
 aaaaaa
     2  64 bytes fsbom 192.168.1.1: icmp_seq=2 ttl=64 time=1.86 ms  fsom
#c替换 将第二行替换成aaaaaaaaa
[root@192 ~]# nl ping.log | sed '2c aaaaaaaa '
     1  64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.75 ms  fbom
aaaaaaaa
     3  64 bytes from 192.168.1.1: icmp_seq=206 ttl=64 time=1.52 ms a
#提取IP地址
[root@192 ~]# cat ping.log | grep -n fsbom | sed 's/^.*fsbom //g' | sed 's/:.*$//g'
192.168.1.1
​
​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待未来的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值