📝 字符处理📦 打包压缩🌉 硬/软链接
一、字符处理
1.sort命令
用于将文件内容加以排序
- -n 依照数值大小排序
- -r 以相反的顺序来排序
- -k 以某列进行排序
- -t 指定分隔符,默认是以空格分隔符
准备文件,写入一段无序内容
cat >> file.txt << EOF
[root@localhost ~]# cat >> file.txt <<EOF
>b:3
>c:2
>a:4
>e:5
>d:1
>f:11
>EOF
sort file.txt 按照字符顺序排
[root@localhost ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11
sort -t " : " -n -k2 file.txt 按照数字大小排序
[root@localhost ~]# sort -t ":" -n -k2 file.txt
d:1
c:2
b:3
a:4
e:5
f:11
sort -t " : " -n -r -k2 file.txt 按照数字大小倒叙排序
[root@localhost ~]# sort -t ":" -n -r -k2 file.txt
f:11
e:5
a:4
b:3
c:2
d:1
2.uniq 命令
用于检查及删除文本文件中重复出现的行列,一般与sort结合使用
- -c 在每列旁边显示该行重复出现的次数
- -d 仅现实重复出现的行列
- -u 仅显示出一次的行列
准备文件,写入一段无序的内容
[root@localhost ~]# cat > file.txt <<EOF
>hello
>123
>hello
>123
>func
>EOF
sort file.txt 按照字符顺序排
[root@localhost ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11
sort file.txt | uniq 不显示重复内容
[root@localhost ~]# sort file.txt | uniq
123
func
hello
sort file.txt | uniq -c 显示重复文件次数
[root@localhost ~]# sort file.txt | uniq -c
2 123
1 func
2 hello
sort file.txt | uniq -d 显示重复文件
[root@localhost ~]# sort file.txt | uniq -d
123
hello
3.cut命令
cut命令用来显示行中的指定部分,删除文件指定字段
- -d 指定字段的分隔符,默认的字段分隔符为'TAB'
- -f 显示指定字段的内容
[root@localhost ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6
root:0:0:/root
4.tr命令
替换或删除命令
- -d 删除字符
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT" 将root换为ROOT
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost ~]# head -1 /etc/passwd |tr -d "root" 删除文件中所有的root
:x:0:0::/:/bin/bash
5.wc命令
统计,计算数字
- -c 统计文件的Bytes数
- -l 统计文件的行数
- -w 统计文件中单词的个数,默认以空白字符作为分隔符
wc -c file.txt 显示文件大小25字节
[root@localhost ~]# wc -c file.txt
25 file.txt
wc -l file.txt 显示写入文件的行数
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -l file.txt
5 file.txt
[root@localhost ~]# grep "hello" file.txt |wc -l
2 显示hello的行数
wc -w file.txt 显示文件中有几个单词
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -w file.txt
5 file.txt 文件中有五个单词
二、打包压缩
1.什么是打包压缩
打包:是将多个文件和目录合并为一个特殊文件
压缩:将打包好的特殊文件压缩
最终得到一个压缩包
2.为什么要用压缩包
- 减少占用空间
- 加快网络传输
3.Linux常见的压缩包
4.打包
[root@localhost test] tar cvf etc_bak.tar /etc/ c 创建 v 详细 f打包后文件路径
5..压缩
[root@localhost test]# gzip etc_bak.tar 文件体积变小,并加上后缀.gz
6. >1和2合并<
[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/ 选项 z 代表gzip压缩算法
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/ 选项 j 代表bzip2压缩算法
7.解压
1.针对 xxx.tar.gz 或者 xxx.tar.bz2 同一使用
[root@localhost test]# tar xvf ܴ 压缩包 -C 解压到目录 不需要指定算法,tar会自动判断
2.针对 xxx.zip
[root@localhost test]# unzip xxx.zip
三、硬/软链接
1.什么是硬/软链接
在 Linux 中,元数据中的 inode 号(inode 是文件元数据的一部分但其并不包含文件名,inode 号即索引节点号)才是文件的唯一标识而非文件名。文件名仅是为了方便人们的记忆和使用,系统或程序通过 inode 号寻找正确的文件数据块。
硬链接:与普通文件没什么不同,inode 都指向同一个文件在硬盘中的区块
软链接:保存了其代表的文件的绝对路径,是另外一种文件,在硬盘上有独立的区块,访问时替换自身路径。
2.什么是inode
每个inode都有一个号码,操作系统用inode号码来识别不同的文件
node就是索引节点,它用来存放档案及目录的基本信息,包含时间、档名、使用者及群组等。
3. 结构
对于经验丰富的 UNIX 开发人员或者管理员来说, inode 的结构相对比较简单,但是可能还有一些您尚不了解的、令人惊讶的有关 inode 的内幕。下面的定义仅给出了 inode 中所包含的、UNIX 用户经常使用的一些重要信息:
● inode 编号
● 用来识别文件类型,以及用于 stat C 函数的模式信息
● 文件的链接数目
● 属主的 UID
● 属主的组 ID (GID)
● 文件的大小
● 文件所使用的 磁盘块的实际数目
● 最近一次修改的时间
● 最近一次访问的时间
● 最近一次更改的时间
从根本上讲, inode 中包含有关文件的所有信息(除了文件的实际名称以及实际数据内容之外)。可以在 AIX 的 Header 文件 /usr/include/jf/ino.h 中、或者 Web 页面 中可以找到完整的 inode 结构。
4.硬链接 ln命令可以创建硬链接,语法:
ln source_file target_file
5.软连接 ln -s命令可以创建软链接,语法:
ln -s source_file target_file