文件管理 (redhat 8.0)

文件管理

文件的压缩与解压

常见的压缩文件后缀
.tar.gz
.tar.bz2
.tar.xz
以上所有文件类型都可以用tar解压
.zip (zip) 跨平台

tar 选项
-c 创建归档
-z 压缩成gz格式
-j 压缩成bz2格式
-J 压缩成xz格式
-t 不解压查看压缩包内容
-f 指定要压缩或解压缩的文件
-x 解压缩
-C 将压缩包解压至指定目录

-zcf 创建一个gz格式的文件压缩包

[root@wcluser share]# ll
总用量 8
-rw-r--r-- 1 root root 116 8月   7 21:49 1
-rw-r--r-- 1 root root 159 8月   7 21:38 10.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 1.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 2.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 3.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 4.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 5.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 6.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 7.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 8.txt
-rw-r--r-- 1 root root   0 8月   7 21:38 9.txt
[root@wcluser share]# tar -zcf share.tar.gz {1..10}.txt
[root@wcluser share]# ll
-rw-r--r-- 1 root root 377 8月   7 21:54 share.tar.gz
[root@wcluser share]# tar -tvf share.tar.gz 
-rw-r--r-- root/root         0 2020-08-07 21:38 1.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 2.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 3.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 4.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 5.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 6.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 7.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 8.txt
-rw-r--r-- root/root         0 2020-08-07 21:38 9.txt
-rw-r--r-- root/root       159 2020-08-07 21:38 10.txt

-jcf 创建一个bz2格式的文件压缩包

[root@wcluser share]# tar -jcf share.tar.bz2 {1..110}.txt
[root@wcluser share]# ll
-rw-r--r-- 1 root root 464 8月   7 21:58 share.tar.bz2
[root@wcluser share]# tar -tvf share.tar.bz2 | tail -10
-rw-r--r-- root/root         0 2020-08-07 21:57 101.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 102.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 103.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 104.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 105.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 106.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 107.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 108.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 109.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 110.txt

-Jcf 创建一个xz格式的文件压缩包

[root@wcluser share]# tar -Jcf share.tar.xz {20..50}.txt
[root@wcluser share]# ll
-rw-r--r-- 1 root root 284 8月   7 22:00 share.tar.xz
[root@wcluser share]# tar -tvf share.tar.xz | tail -10
-rw-r--r-- root/root         0 2020-08-07 21:57 41.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 42.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 43.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 44.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 45.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 46.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 47.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 48.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 49.txt
-rw-r--r-- root/root         0 2020-08-07 21:57 50.txt

-xf 解压指定文件
-xvf 解压指定文件并显示解压过程
-xvf 文件名 -C 指定目录

[root@wcluser share]# tar -xvf share.tar.gz -C /home/fyj
./13
./18
./22
./24
./33
./5
./7
./share.tar.gz
[root@wcluser share]# tar -xvf share.tar.xz
20.txt
21.txt
22.txt
23.txt
24.txt
25.txt
26.txt
27.txt
28.txt
29.txt
30.txt
31.txt
32.txt
33.txt
34.txt
35.txt
36.txt
37.txt
38.txt
39.txt
40.txt
41.txt
42.txt
43.txt
44.txt
45.txt
46.txt
47.txt
48.txt
49.txt
50.txt

解压到指定目录

[root@wcluser share]# tar -xvf share.tar.gz -C /home/fyj
./13
./18
./22
./24
./33
./5
./7
./share.tar.gz

-tvf 不解压查看压缩包中的内容

[root@wcluser share]# tar -tvf share.tar.gz 
-rw-r--r-- root/root         0 2020-08-07 22:09 ./13
-rw-r--r-- root/root         0 2020-08-07 22:09 ./18
-rw-r--r-- root/root         0 2020-08-07 22:09 ./22
-rw-r--r-- root/root         0 2020-08-07 22:09 ./24
-rw-r--r-- root/root         0 2020-08-07 22:09 ./33
-rw-r--r-- root/root         0 2020-08-07 22:09 ./5
-rw-r--r-- root/root         0 2020-08-07 22:09 ./7
-rw-r--r-- root/root       877 2020-08-07 22:18 ./share.tar.gz

f参数必须放在最后

/dev/cdrom 实际上是/dev/sr0的软链接

文本排序去重

sort
不带参数 以首字符的优先级排序(字母的顺序和数字的大小)
-n 按数字的大小排序
向test文件中写入一些乱序数字

[root@wcluser share]# cat > test <<EOF
> 93
> 5
> 62
> 4
> 55
> 59
> 465
> 2
> 89
> 44
> 431
> 33
> 1
> EOF

通过sort -n命令将乱序数字排序

[root@wcluser share]# sort -n test 
1
2
4
5
33
44
55
59
62
89
93
431
465

uniq
-c 显示重复的次数
-u 显示唯一的不显示重复的
-d 显示重复的不显示唯一的

[root@wcluser share]# cat > test <<EOF
> 8
> 45
> 6
> 1
> 56
> 77
> 8
> 54
> 6
> 21
> 45
> 77
> 92
> 1
> 28
> EOF

sort -n 文件名 | uniq 先排序再去重

在test文件中写入多个重复数字

[root@wcluser share]# sort -n test | uniq 
1
6
8
21
28
45
54
56
77
92

用uniq -c查看重复次数

[root@wcluser share]# sort -n test | uniq -c
      2 1
      2 6
      2 8
      1 21
      1 28
      2 45
      1 54
      1 56
      2 77
      1 92

用uniq -u显示唯一不显示重复

[root@wcluser share]# sort -n test | uniq -u
21
28
54
56
92

用uniq -d显示重复不显示唯一

[root@wcluser share]# sort -n test | uniq -d
1
6
8
45
77

cut 取出列的内容
-d 指定分隔符

截取passwd文件中前十行(head)第一列(-d: -f1)的内容

[root@localhost ~]# cut -d: -f1 passwd|head 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator

cut只能取出比较规则的文件;这是cut的局限性

文件处理三剑客

awk 取出指定内容

-F分隔符 ‘{print $1}’ 文件名

‘{print$1}’ 打印开头第一个字段的内容

[root@localhost ~]# awk -F: '{print $1}' passwd | head -10
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator

分隔符是空格时可以不加-F:分隔符不是空格时必须加

[root@localhost ~]# df -hT | awk '{print $4}'
已用
2.9G
0
140K
8.9M
0
3.5G
119M

取出第三行第四列
NR==3:第三行
$4:第四列

[root@localhost ~]# df -hT | awk 'NR==3{print $4}'
0

对比

[root@localhost ~]# df -hT
文件系统              类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root xfs        48G  2.9G   45G    7% /
devtmpfs              devtmpfs  905M     0  905M    0% /dev
tmpfs                 tmpfs     914M  140K  914M    1% /dev/shm
tmpfs                 tmpfs     914M  8.9M  905M    1% /run
tmpfs                 tmpfs     914M     0  914M    0% /sys/fs/cgroup
/dev/sr0              iso9660   3.5G  3.5G     0  100% /mnt
/dev/sda1             xfs       497M  119M  379M   24% /boot

‘{print $NF}’ NF表示最后一列
‘{print $(NF-2)}’ 倒数第三列

sed 处理行的内容

  • 单引号–变量$PATH会将文本内容原封不动插入,不会解释成路径

  • 双引号–变量$PATH被解析以后在当作文本进行插入

  • 总结:
    如果引号里面是普通字符串的话,你可以任意使用单引号或者双引号:
    如果引号里面是变量或者带反引号的命令的话,你想要变量解析的结果或者命令执行的结果,那就使用双引号;你想要引号内的原样字符串,那就使用单引号。

sed [option] ‘要处理的动作’
sed -n, --quiet, --silent
取消自动打印模式空间

 [root@localhost ~]# sed -n '2p' passwd 			#'2p' :打印第二行
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# sed -n '/root/p' passwd 		#/root/p :打印所有匹配root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

sed -i (确定修改原文件)

s:替换
[root@localhost ~]# sed '1s/://g' passwd | head -1	#1s/://g :1s将第一行的内容替换;’:‘替换的对象;’//‘ 替换成无;g 全局所有
rootx00root/root/bin/bash
[root@localhost ~]# cat passwd | head -1
root:x:0:0:root:/root:/bin/bash
c:修改
[root@wcluser share]# sed -i '3s/never say never/fine/' test 			#将第三行的'never say never'改成'fine'
[root@wcluser share]# cat test 
ok
gogogo
fine
fyj
/6/what the hell/
1
56
77
8
54
6
21
45
77
92
28

a:添加

在某行之前添加

[root@wcluser share]# sed -i '2i gogogo' test 		#在第二行之前添加'gogogo'
[root@wcluser share]# cat test 
ok
gogogo
never say never
fyj
/6/what the hell/
1
56
77
8
54
6
21
45
77
92
28
p:打印

打印第十行

[root@wcluser share]# sed -n '10p' test 
54
d:删除

匹配到’7’的全部删除

[root@wcluser share]# sed -i "/7/d" test 
[root@wcluser share]# cat test 
ok
gogogo
fine
fyj
/6/what the hell/
1
56
8
6
21
45
92
28

删除5-13行

[root@wcluser share]# sed -i '5,13d' test 
[root@wcluser share]# cat test 
ok
gogogo
fine
fyj

删除第十行

 [root@wcluser share]# sed -i '10d' test 
[root@wcluser share]# cat test 
ok
gogogo
fine
fyj
/6/what the hell/
1
56
77
8
6
21
45
77
92
28

echo 标准输出

定向输出 echo ‘ 写入的内容 ‘ > 文件名

[root@wcluser share]# echo '123' > test 
[root@wcluser share]# cat test 
123

使用定向输出会覆盖之前写下的字段,一般使用追加输出。

追加输出echo ‘ 写入的内容 ’ >> 文件名

[root@wcluser share]# echo 'go' >> test 
[root@wcluser share]# cat test 
123
go

echo可配合sed -r一起使用
sed -r #使用扩展正则表达式

[root@wcluser share]# echo 'f y j'
f y j
[root@wcluser share]# echo 'f y j' | sed -r 's/f (.*) (.*)/f \2 \1/g'
f j y
[root@wcluser share]# echo '11 10 02 05'
11 10 02 05
[root@wcluser share]# echo '11 10 02 05' | sed -r 's/11 (.*) 02 (.*)/11 \2 02 \1/g'
11 05 02 10

Linux文件常用命令

grep
根据模式搜索文本,并将符合模式的文本行显示出来。
常用参数
-i //忽略大小写

[root@wcluser home]# cat /root/passwd | grep -i WCL
wcl:x:25:1110::/home/fyj/:/bin/bash

–color //匹配到的内容显示高亮

-v //显示没有被模式匹配到的字符

[root@wcluser home]# tail -3 /root/passwd | grep -v wcl
fyj:x:1110:0:futuregirlfriend:/home/fyj:/bin/bash
nginx:x:990:204:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@wcluser home]# tail -3 /root/passwd 
wcl:x:25:1110::/home/fyj/:/bin/bash
fyj:x:1110:0:futuregirlfriend:/home/fyj:/bin/bash
nginx:x:990:204:Nginx web server:/var/lib/nginx:/sbin/nologin

-o //只显示被匹配的字符

[root@wcluser home]# tail -3 /root/passwd | grep -o wcl
wcl

-q //静默匹配不输出任何信息

[root@wcluser home]# tail -3 /root/passwd | grep -q wcl
[root@wcluser home]# 

fgrep
#不支持正则表达式,执行速度快

find
实时查找,精确性强遍历指定目录中所有文件完成查找

语法:find 大概路径 文件名 [option]
-uid
#根据UID进行查找,当用户被删除后文件 的属主或变成此用户的UID

[root@wcluser ~]# find /root passwd -uid 0
/root
/root/.viminfo
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.bash_history
/root/smbclient
/root/.lesshst
/root/share
/root/share/test
/root/share.tar.gz

-gid
#根据GID进行查找,当用户被删除后文件的属组变为此用户的GID
-nouser
#查找没有属主的文件

[root@wcluser ~]# find /etc/ -nouser
/etc/passwd-
/etc/passwd

-nogroup
#查找没有属组的权限

[root@wcluser ~]# find / -nogroup
find: ‘/proc/2060/task/2060/fd/7’: 没有那个文件或目录
find: ‘/proc/2060/task/2060/fdinfo/7’: 没有那个文件或目录
find: ‘/proc/2060/fd/8’: 没有那个文件或目录
find: ‘/proc/2060/fdinfo/8’: 没有那个文件或目录
/root/passwd
/home/fyj/.bash_logout
/home/fyj/.bash_profile
/home/fyj/.bashrc
/home/fyj/.zshrc
/home/fyj/.bash_history
[root@wcluser ~]# ll /root/

-type
#根据文件类型来查找

[root@wcluser ~]# find /root/ -type d
/root/
/root/smbclient
/root/share

-size
#根据文件大小进行查找;如1k、1M、+10k

[root@wcluser ~]# find /root/ -size 1k
/root/
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/smbclient
/root/.lesshst
/root/share
/root/share/test

find path -name

[root@yukino ~]# find / -name cockpit
/run/cockpit
/etc/pam.d/cockpit
/etc/cockpit
/etc/motd.d/cockpit
/var/lib/selinux/targeted/active/modules/100/cockpit
/var/lib/cockpit
/usr/share/doc/cockpit
/usr/share/selinux/targeted/default/active/modules/100/cockpit
/usr/share/cockpit
[root@yukino ~]# find /etc -name ifcfg-ens160
/etc/sysconfig/network-scripts/ifcfg-ens160
[root@yukino ~]# find /etc -name nginx.conf
/etc/nginx/nginx.conf

文件层级系统

FHS     //文件层级系统
/       //可以单独分区,LVM分区
/boot       //系统启动相关的文件,如内核(vmlinuz)、initrd(initramfs),\
            //以及grub(bootloader)。建议单独分区,基本分区
/dev        //设备文件。不能单独分区
    设备文件    //关联至一个设备驱动程序,进而能够与之对应硬件设备进行通信
        块设备     //随机访问,数据块(比如硬盘)
        字符设备    //也叫线性设备,线性访问,按字符为单位(比如鼠标、显示器)
        设备号     //主设备号(major)和次设备号(minor)
            主设备号标识设备类型
            次设备号标识同一类型下的不同设备
    设备文件只有元数据,没有数据
/etc        //配置文件
/home       //普通用户的家目录,每一个用户的家目录通常默认为/home/USERNAME。 \
            //建议单独分区
/root       //管理员的家目录。不该单独分区
/lib        //库文件
    静态库     //.a
    动态库     //.dll,.so(shared object)
    /lib/modules    //内核模块文件
/media      //挂载点目录,通常用来挂载移动设备
/mnt        //挂载点目录,通常用来挂载额外的临时文件系统,比如另一块硬盘
/opt        //可选目录,早期通常用来安装第三方程序
/proc       //伪文件系统,内核映射文件(伪文件系统实际上里面是没有任何内容的, \
            //开机之后才映射上去的)。不能单独分区
/sys        //伪文件系统,跟硬件设备相关的属性映射文件(伪文件系统实际上里面是没有 \
            //任何内容的,开机之后才映射上去的)。不能单独分区
/tmp        //临时文件,/var/tmp
/var        //可变化的文件,比如log、cache。存放日志信息、pid文件、lock文件,\
            //建议单独分区
/bin        //可执行文件,用户命令
/sbin       //管理命令
/usr        //shared,read-only,全局共享只读文件。提供操作系统核心功能,可以单独分区
    /usr/bin
    /usr/sbin
    /usr/lib
/usr/local      //第三方软件安装路径
    /usr/local/bin
    /usr/local/sbin
    /usr/local/lib
    /usr/local/etc
    /usr/local/man

/etc,/bin,/sbin,/lib内是系统启动就需要用到的程序,这些目录不能挂载额外的分区,\
必须在根文件系统的分区上

/usr/bin,/usr/sbin,/usr/lib提供操作系统核心功能,/usr可以单独分区

/usr/local/bin,/usr/local/sbin,/usr/local/lib,/usr/local/etc, \
/usr/local/man等等在/usr/local目录下的内容都是第三方软件,建议单独分区
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以在Red Hat官网上下载Red Hat Enterprise Linux 8.的ISO镜像文件。具体步骤如下: 1. 访问Red Hat官网(https://www.redhat.com/zh)。 2. 点击“产品和服务”菜单,选择“Red Hat Enterprise Linux”。 3. 在“Red Hat Enterprise Linux”页面中,选择“下载”选项卡。 4. 在“下载”页面中,选择“Red Hat Enterprise Linux 8.”版本。 5. 在“Red Hat Enterprise Linux 8.”页面中,选择“ISO镜像”选项卡。 6. 在“ISO镜像”页面中,选择您需要下载的ISO镜像文件,并点击“下载”。 7. 下载完成后,您可以使用ISO镜像文件进行安装或制作安装盘。 希望对您有帮助! ### 回答2: Redhat8.0是一个非常流行的Linux操作系统版本,它是由Red Hat公司开发的,具有良好的稳定性和安全性,适用于各种场合。如果需要安装Redhat8.0系统,首先需要下载Redhat8.0镜像文件iso,然后通过刻录光盘或者USB安装。 下载Redhat8.0镜像文件iso,最简单的方法是从官方网站进行下载。首先在浏览器中打开Red Hat官方网站https://www.redhat.com,然后选择Downloads选项,找到Redhat8.0版本,点击下载按钮,即可开始下载。在下载过程中,需要提供注册信息,才能下载。 除了官方网站外,还可以使用一些第三方的网站进行下载。这些网站为用户提供了许多软件的下载链接,包括Redhat8.0。其中比较知名的网站有国内的163、腾讯和阿里云等网站,它们提供了较快的下载速度和丰富的软件资源。不过需要注意的是,在下载前最好从多个来源验证文件的完整性和安全性。 当下载完成后,需要使用镜像文件iso进行光盘刻录或者USB安装。光盘刻录的方法较为简单,只需要使用光盘工具将iso镜像写入光盘中即可。如果需要在笔记本电脑或者其他设备上进行安装,则需要使用USB启动盘,需要将iso镜像写入U盘中,然后设置电脑从U盘启动即可。 总之,Redhat8.0镜像文件iso下载很简单,只需要从官方网站或者第三方网站进行下载,并注意验证文件完整性和安全性。下载完成后,将镜像文件写入光盘或者U盘中进行安装即可。 ### 回答3: Red Hat是一个流行的Linux操作系统,最新版本为Red Hat Enterprise Linux 8.0。如果想要安装或升级Red Hat 8.0,就需要下载它的镜像文件ISO。 要下载Red Hat 8.0的镜像文件ISO,首先需要前往官方网站www.redhat.com/register。在该网站注册后,可以免费下载Red Hat 8.0的ISO文件。 当然,也可以通过其他可靠的第三方网站下载Red Hat 8.0的ISO文件。在选择第三方网站时,需要确认其可信度和安全性,以避免下载到不安全的文件。 下载完成后,可以将ISO文件刻录为光盘,或创建一个可启动的USB驱动器,以便进行安装和升级操作。 总之,下载Red Hat 8.0的镜像文件ISO需要确认官方网站或者可信的第三方网站,确保下载的文件是安全可靠的。下载完成后,应该按照官方的安装指引进行操作,以达到最佳的应用效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值