Linux属性及文件压缩

64 阅读5分钟

Linux文件属性压缩.png

1. 硬链接

语法结构

ln filename1 filename2

root@linux [20:24:13] [~] 
-> # ln a.txt b.txt
67161033 a.txt  67161033 b.txt
root@linux [20:24:28] [~] 
-> # ls -il
total 0
67161033 -rw-r--r-- 2 root root 0 Jul 10 20:24 a.txt
67161033 -rw-r--r-- 2 root root 0 Jul 10 20:24 b.txt

硬链接的特点

  1. 两个文件互为链接文件,inode号相同,指向内存的同一处
  2. 常用在备份,同时备份,修改其中任何一个都会影响另一个
  3. 删除其中任何一个互不影响
  4. 目录不能硬链接(防止目录出现环形,进入死循环)
  5. 目录不能跨文件系统硬链接
  6. 相当于开门,门之间互不影响
  7. 最好用绝对路径做硬链接

为什么目录不能做硬链接?

答:如果引入了对目录的硬连接就有可能在目录中引入循环,那么在目录遍历的时候系统就会陷入无限循环当中

为什么不能跨文件系统做硬链接

答:硬链接文件之间指向同一个inode号,不同分区有不同的索引节点结构和编号,如果跨分区做硬链接会导致链接文件冲突失效

2. 软链接

语法结构

ln -s filename1 filename2
root@linux [15:48:09] [~] 
-> # ln -s a.txt c.txt
root@linux [15:48:17] [~] 
-> # ll -i
total 0
67161033 -rw-r--r-- 2 root root 0 Jul 10 20:24 a.txt
67161033 -rw-r--r-- 2 root root 0 Jul 10 20:24 b.txt
67161037 lrwxrwxrwx 1 root root 5 Jul 11 15:48 c.txt -> a.txt

软连接特点

  1. 软链接区分源文件与目标文件
  2. 删除源文件链接文件失效,删除链接文件源文件无事发生
  3. 源文件和链接文件的inode号不同
  4. 目录可以做软链接,跨文件系统可以做软链接
  5. 软链接相当于windows下的快捷方式
  6. 软链接常用于解决磁盘空间不足的问题
  7. 做快速代码变更

面试题:软链接和硬链接的区别

  1. 硬链接文件之间inode号相同,软链接inode号不同
  2. 目录能软链接但是不能硬链接
  3. 不能跨文件系统做硬链接但是能做软链接
  4. 硬链接删除互不影响,软链接删除源文件链接文件失效

文件详细信息

root@linux [16:17:27] [~] 
-> # stat a.txt 
  File: ‘a.txt’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 67161033    Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-07-10 20:24:12.818114177 +0800
Modify: 2024-07-10 20:24:12.818114177 +0800
Change: 2024-07-10 20:24:21.425201375 +0800
 Birth: -

文件时间

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-07-10 20:24:12.818114177 +0800
Modify: 2024-07-10 20:24:12.818114177 +0800
Change: 2024-07-10 20:24:21.425201375 +0800

Access:访问时间,cat,vim等等

Modify:修改内容时间,vim,echo等等

Change:文件属性修改时间

-rw-r--r-- 2 root root 0 Jul 10 20:24 a.txt

这一部分变动都会影响文件属性的时间

3. 打包压缩

1. tar

语法结构

tar -zcvf filename.tar.gz filename1 filename2

-z:使用gzip压缩
-c:create创建压缩包
-v:显示进度
-f:指定文件名

root@linux [16:32:09] [~] 
-> # tar -zvcf file.tar.gz 1.txt 2.txt 3.txt 
root@linux [16:34:24] [~] 
-> # tar -zvcf file.tar.gz /opt/{a..c}.txt
tar: Removing leading `/' from member names
/opt/a.txt
tar: Removing leading `/' from hard link targets
/opt/b.txt
/opt/c.txt


-xf: 解压缩

-C:指定解压目录

root@linux [16:36:50] [~] 
-> # tar -xf file.tar.gz 
root@linux [16:37:02] [~] 
-> # ls
file.tar.gz  opt
root@linux [16:37:03] [~] 
-> # cd opt 
root@linux [16:37:06] [~/opt] 
-> # ls
a.txt  b.txt  c.txt

root@linux [16:38:10] [~] 
-> # tar -xf file.tar.gz -C /tmp

-tf:查看压缩包内容
root@linux [16:38:57] [~] 
-> # tar -tf file.tar.gz 
opt/a.txt
opt/b.txt
opt/c.txt

2. zip

语法结构

压缩打包: zip  all.zip file1  file2
压缩包的位置: zip /opt/all.zip file1 file2
解压:unzip all.zip
指定位置解压: unzip all.zip -d /opt/

压缩
root@linux [16:43:13] [~] 
-> # zip all.zip {1..3}.txt

压缩指定目录
root@linux [16:44:19] [~] 
-> # zip /opt/all.zip {1..3}.txt
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
  adding: 3.txt (stored 0%)
root@linux [16:44:45] [~] 
-> # ll /opt 
total 4.0K
-rw-r--r-- 1 root root 436 Jul 11 16:44 all.zip

解压到当前目录
root@linux [16:47:58] [~] 
-> # unzip all.zip 
Archive:  all.zip
 extracting: 1.txt                   
 extracting: 2.txt                   
 extracting: 3.txt                   
root@linux [16:48:02] [~] 
-> # ls
1.txt  2.txt  3.txt  all.zip  file.tar.gz  opt


解压缩指定目录
root@linux [16:46:18] [~] 
-> # unzip all.zip -d /opt 
Archive:  all.zip
 extracting: /opt/1.txt              
 extracting: /opt/2.txt              
 extracting: /opt/3.txt              
root@linux [16:46:46] [~] 
-> # ls /opt 
1.txt  2.txt  3.txt  all.zip

4. 分隔符

; 命令的分隔符 不管前面的命令执行是否成功,都会继续往后执行

[root@oldboy:~]# touc 1.txt;mkdir oldboy;ls -l
-bash: touc: command not found
total 0
drwxr-xr-x 2 root root 6 Jul 11 11:53 oldboy

&& 命令的分隔符 前面的命令必须执行成功,才会执行后面的命令

[root@oldboy:~]# touc 1.txt && mkdir oldboy && ls -l
-bash: touc: command not found
[root@oldboy:~]# touch 1.txt && mkdir oldboy && ls -l
total 0
-rw-r--r-- 1 root root 0 Jul 11 11:55 1.txt
drwxr-xr-x 2 root root 6 Jul 11 11:55 oldboy

ifdown ens33 && ifup ens33

|| 命令的分隔符 前面的命令必须执行失败,才会执行后面的命令

[root@oldboy:~]# touc 1.txt || mkdir oldboy 
-bash: touc: command not found
[root@oldboy:~]# ll
total 0
drwxr-xr-x 2 root root 6 Jul 11 11:56 oldboy

目录不存在则创建:

[root@oldboy:~]# cd oldboy || mkdir oldboy
-bash: cd: oldboy: No such file or directory
[root@oldboy:~]# ll
total 0
drwxr-xr-x 2 root root 6 Jul 11 11:57 oldboy