Linux基本命令之文件及目录命令03

在这里插入图片描述

1、 链接命令

在liunx中链接一般分为两种,分别是硬链接(hard link)和软链接(symbolic link),他们之间有相同的地方(比如 占用内存小)也有不同的地方(链接的方式不同)。

硬链接(Hard Link):A—B,假设B是A的硬链接,那么它们两个指向同一个文件,可以允许有多个路径,一般都一些重要的文件都采用这种链接,防止误删。

特点:

  • 1.以文件副本的形式存在,但不占用实际空间。
  • 2.不允许给目录创建硬链接。
  • 3.硬链接只有在同一个文件系统中才能创建。
  • 4.源文件删除,链接不失效。

软链接(Symbolic Link):类似于Windows操作系统中的快捷方式,删除源文件就失效。

  • 1.以路径的形式存在,类似于Windows操作系统中的快捷方式
  • 2.软链接可以 跨文件系统 。
  • 3.软链接可以对一个不存在的文件名进行链接。
  • 4.软链接可以对目录进行链接。
  • 5.源文件删除,链接失效。

语法

ln [参数][源文件或目录][目标文件或目录]

参数

  • -s 软链接(符号链接)
  • -f 强制执行
  • -d 允许超级用户制作目录的硬链接
  • -i 交互模式,文件存在则提示用户是否覆盖
  • -n 把符号链接视为一般目录
  • -b 删除,覆盖以前建立的链接
  • -v 显示详细的处理过程

案例

1.创建/home/itbestboy/test01/hello.java的链接(硬链接)/home/itbestboy/test02/helloTwo.java 。

#分别查看test01 和 test02目录下的文件
[root@root itbestboy]# cd test01
[root@root test01]# ls
hello.java
[root@root itbestboy]# cd test02
[root@root test02]# ls
#创建硬链接
[root@root itbestboy]# ln test01/hello.java test02/helloTwo.java
[root@root itbestboy]# cd test02
[root@root test02]# ls -l
total 4
-rw-r--r-- 2 root root 1214 Jul 27 13:18 helloTwo.java
[root@root test02]# cd ..
#删除源文件,用来测试硬链接是否失效
[root@root test01]# rm hello.java 
rm: remove regular file ‘hello.java’? y
[root@root test01]# ls
#进入test02查看,可以看出没有失效
[root@root itbestboy]# cd test02
[root@root test02]# ls
helloTwo.java
[root@root test02]# cat helloTwo.java 
hello everyone!!!!!
HELLOW   JAVA    PYTHON


HELLO

GOOGL

2.创建/home/itbestboy/test01/helloTwo.java的链接(软链接)/home/itbestboy/test02/hello01.java 。

[root@root itbestboy]# ln -s test01/helloTwo.java test02/hello01.java
[root@root itbestboy]# cd test02
[root@root test02]# ll
total 4
lrwxrwxrwx 1 root root   20 Jul 27 14:11 hello01.java -> test01/helloTwo.java

注:

  • 软连接前面用 l表示,并且权限为777,即所属主和所属组拥有所有权限,也就是说软连接都为lrwxrwxrwx
  • 硬链接前面用-表示,即文件,权限不确定。
  • 软连接有一个箭头表示,-->,指向链接的源文件。
  • 硬链接除了文件名不一样之外,其他的信息一样,类似于cp,但是有和cp不一样,他可以动态更新内容,即源文件的内容更新后,目的文件的内容也随之变化。

2、文件目录解压缩

2.1 压缩解压缩格式为.zip

可以将文件或目录压缩解压缩成.zip格式

zip:将文件或目录压缩成.zip格式

语法

zip [参数] [压缩后的名字] [被压缩的文件或目录]

其中被压缩的名字一般为××××.zip

命令所在路径

/usr/bin/zip

参数

  • -r 递归处理,压缩目录。

案例

将/home/itbestboy/test02目录下的helloTwo.java压缩成hello.zip

[root@root /]# zip hellow.zip  /home/itbestboy/test02/helloTwo.java 
  adding: home/itbestboy/test02/helloTwo.java (deflated 51%)
[root@root /]# ls
9000  bin  boot  dev  etc  hellow.zip  home  lib  lib64  lost+found  media  mnt  opt  patch  proc  root  run  sbin  srv  sys  test01  tmp  usr  var  www
[root@root /]#

将/home/itbestboy/目录下的test02目录压缩成test02.zip

[root@root itbestboy]# zip -r test02.zip test02/
  adding: test02/ (stored 0%)
  adding: test02/helloTwo.java (deflated 51%)
[root@root itbestboy]# ls
apache-tomcat-9.0.48  apache-tomcat-9.0.48.tar.gz  hello.java  jdk-8u221-linux-x64.rpm  test01  test02  test02.zip

unzip:将格式为.zip的压缩文件解压缩

语法

unzip [参数] [压缩文件] 

命令所在路径

/usr/bin/unzip

参数

  • -t 检查压缩文件是否正确。

案例

将上面压缩的文件解压

[root@root /]# unzip -t hellow.zip 
Archive:  hellow.zip
    testing: home/itbestboy/test02/helloTwo.java   OK

将上面压缩的目录解压

[root@root itbestboy]# unzip test02.zip
Archive:  test02.zip
replace test02/helloTwo.java? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: test03
  inflating: test03                  
[root@root itbestboy]# ls
apache-tomcat-9.0.48  apache-tomcat-9.0.48.tar.gz  hello.java  jdk-8u221-linux-x64.rpm  test01  test02  test02.zip  test03
[root@root itbestboy]# 

**注:**可以看出在压缩和解压缩之后源文件和源目录还存在。

2.2 压缩解压缩格式为.bz2

可以将文件压缩解压缩成.bz格式,并且bzip2采用新的压缩演算法,压缩效果比传统的LZ77/LZ78压缩演算法好。而且在不加参数时会删除原始的文件

bzip2:将文件压缩成.bz2格式

语法

bzip2 [参数] [压缩等级] [压缩的文件]

命令所在路径

/usr/bin/bzip2

参数

  • -k 保留原始文件
  • -d 执行解压缩
  • -z 强制执行压缩

案例

将/home/itbestboy/test02目录下的helloTwo.java压缩

#不加参数,不保留源文件
[root@root itbestboy]# bzip2 hello.bz2 test02/helloTwo.java 
[root@root test02]# ls
helloTwo.java.bz2


#加参数k 保留源文件
[root@root test02]# bzip2 -k helloTwo.java 
[root@root test02]# ls
helloTwo.java  helloTwo.java.bz2

bunzip2:将格式为.bz2格式的文件解压缩

语法

bunzip2 [参数] [压缩文件]

功能与bzip2 -d效果相同

命令所在路径

/usr/bin/bunzip2

参数

  • -f 解压缩时,覆盖源文件。

  • ***-*k ** 保留压缩文件

案例

将上面压缩的文件解压

#不带参数,默认不保留压缩文件
[root@root test02]# bunzip2 helloTwo.java.bz2 
[root@root test02]# ls
helloTwo.java

#带参数k,保留压缩文件
[root@root test02]# bunzip2 -k helloTwo.java.bz2 
[root@root test02]# ls
helloTwo.java  helloTwo.java.bz2

在不加参数时可以看出在压缩和解压缩之后源文件不存在。

当加上参数 k 时,压缩和解压缩之后源文件存在。

2.3 压缩解压缩格式为.gz

可以将文件压缩解压缩成.gz格式,压缩和解压缩后不保留源文件

gzip:将文件压缩成.gz格式

语法

gzip [参数] [被压缩的源文件]

命令所在路径:

/bin/gzip

参数

  • -d  解开压缩文件。
  • -f 强行压缩文件。
  • -l 列出压缩文件的相关信息。
  • -v 显示指令执行过程。

案例

将/home/itbestboy/test02目录下的helloTwo.java压缩

[root@root test02]# gzip helloTwo.java 
[root@root test02]# ls
helloTwo.java.gz

#使用参数 d 进行解压缩
[root@root test02]# gzip -d helloTwo.java.gz 
[root@root test02]# ls
helloTwo.java

gunzip:将.gz格式的压缩文件解压

语法

gunzip [参数] [压缩文件]

命令所在路径:

/bin/gunzip

参数

  • -f 强行解开压缩文件。

案例

将上面压缩的文件解压缩

[root@root test02]# gunzip helloTwo.java.gz 
[root@root test02]# ls
helloTwo.java


#类似于
[root@root test02]# gzip -d helloTwo.java.gz 
[root@root test02]# ls
helloTwo.java

可以看出在压缩和解压缩之后源文件不存在。

2.4 压缩解压缩格式为.tar.gz

将文件或目录压缩成.tar.gz格式。又叫做备份

tar:将文件或目录压缩成.tar.gz格式:

语法

tar [参数] [压缩后的名字] [需要压缩的文件或目录]

命令路径

/bin/tar

参数

  • -z 打包同时压缩。
  • -c 打包
  • -v 显示详细信息
  • -f 指定文件名

案例

将/home/itbestboy/test02目录下的helloTwo.java压缩成hello.tar.gz

#一般情况下 -zcvf 四个参数连着用
[root@root test02]# tar -zcvf hello.tar.gz helloTwo.java 
helloTwo.java
[root@root test02]# ls
hello.tar.gz  helloTwo.java

tar:将.tar.gz格式的压缩文件解压

语法

tar [参数] [压缩的文件或目录]

命令路径

/bin/tar

参数

  • -z 解压缩。
  • -x 解包
  • -v 显示详细信息
  • -f 指定解压文件

案例

将上面压缩的文件解压缩

[root@root test02]# tar -zxvf hello.tar.gz 
helloTwo.java
[root@root test02]# ls
hello.tar.gz  helloTwo.java
[root@root test02]# 

可以看出在压缩和解压缩之后源文件存在。

总结

命令解释路径应用于文件应用于目录是否保留源文件或目录
zip压缩/usr/bin/zipYYY
unzip解压缩/usr/bin/unzipYYY
bzip2压缩/usr/bin/bzip2YNN
bunzip2解压缩/usr/bin/bunzip2YNN
gzip压缩/bin/gzipYN不加参数 k,不保留
加参数k,保留
gunzip解压缩/bin/gunzipYN不加参数k,不保留
加参数k,保留
tar(c)压缩/bin/tarYYY
tar(x)解压缩/bin/tarYYY

Linux基本命令之文件命令02:------------------------------------------------------------------------《加载完成》--------------------------------------------------------
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值