Linux之文件进阶管理

文件进阶管理

文件查找

一、命令文件

which ls //PATH环境变量 (echo $PATH)

whereis vim

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin

二、任意文件

[root@centos-9 ~]# yum -y install mlocate

A. locate (查询数据库,查找速度比较快: /var/lib/mlocate/mlocate.db)

计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron

手动更新数据库会大量消耗IO:updatedb

locate ifcfg-eth0

B. find

find options expression

查找条件 作用
-name 匹配名称。例1:find /etc -name host* 例2:find /dev -name std*
-perm 匹配权限(mode为完全匹配,-mode为包含即可)。例1:find / -perm 000
-user 匹配所有者。例:useradd sky ; find / -user sky
-group 匹配所有组。例:find / -group sky
-mtime -n +n 匹配修改内容(modify)的时间(-n指n天以内,+n指n天以前) 例:在根目录下查找mtime为3天以前的文件: find / -mtime +3 例:在根目录下查找mtime为3天以内的文件: find / -mtime -3
-atime -n +n 匹配访问(access)文件的时间-n指n天以内,+n指n天以前
-ctime -n +n 匹配修改权限(change)的时间-n指n天以内,+n指n天以前
-nouser 匹配无所有者的文件 [这种情况通常是文件属主的用户名被删除了,只剩下uid]
-nogroup 匹配无所有组的文件 [这种情况通常是文件属组的账号被删除了,只剩下gid]
-newer f1 !f2 匹配比文件f1新却比f2旧的文件
-type b/d/c/p/l/f 匹配文件类型(b块设备、d目录、c字符设备、p管道、l链接文件、f普通文件) 例1:find /dev -type b 例2:find /dev -type c 例3:find /dev -type p
-size 匹配文件的大小(+50k查找超过50k的文件,而-50k则代表查找小于50k的文件) 例:在/boot目录下查找大小为大于6M的文件:find /boot -size +6M
-prune 忽略某个目录
-exec 操作 {} ; 自动对找到的文件执行“某个操作”,如ls、cp、rm -rfv等。例:在/etc目录中查找以.conf文件结尾的所有文件,并复制到/tmp/目录中。 find /etc -name *.conf -exec cp -rv {} /tmp/ ; 大括号{}表示用find找到的所有文件
-ok 操作 {} ; 手动交互(手动yes确认)对找到的文件执行“某个操作”,如ls、cp等。 例:find /etc -name *.conf -ok cp -v {} /tmp/ ;

[root@xiaochen ~]# find /home/

按文件名:

[root@xiaochen ~]# find /etc /tmp -name "ifcfg-eth0"
[root@xiaochen ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小写
[root@xiaochen ~]# find /etc -iname "ifcfg-eth*"

按文件大小:

[root@xiaochen ~]# find /etc -size +5M //大于5M
[root@xiaochen ~]# find /etc -size 5M
[root@xiaochen ~]# find /etc -size -5M -print
[root@xiaochen ~]# find /etc -size +5M -ls

指定查找的目录深度:

-maxdepth levels
-mindepth levels
[root@xiaochen ~]# find / -maxdepth 3 -a -name "ifcfg-eth0"

按时间找(atime,mtime,ctime):

[root@xiaochen ~]# find /etc -mtime +5 //修改时间超过5天
[root@xiaochen ~]# find /etc -mtime 5 //修改时间等于5天
[root@xiaochen ~]# find /etc -mtime -5 //修改时间5天以内
注:修改时间5天是按照小时算的,即从此刻往前120h

按文件属主、属组找:

[root@xiaochen ~]# find /home -user jack //属主是jack的文件
[root@xiaochen ~]# find /home -group hr //属组是hr组的文件
[root@xiaochen ~]# find /home -user jack -group hr
[root@xiaochen ~]# find /home -user jack -a -group hr //-a即为and且
[root@xiaochen ~]# find /home -user jack -o -group hr //-o即为or或
[root@xiaochen ~]# find /home -nouser
[root@xiaochen ~]# find /home -nogroup
[root@xiaochen ~]# find /home -nouser -o -nogroup

按文件类型:

[root@xiaochen ~]# find /dev -type f //f普通文件
[root@xiaochen ~]# find /dev -type d //d目录
[root@xiaochen ~]# find /dev -type l //l链接
[root@xiaochen ~]# find /dev -type b //b块设备
[root@xiaochen ~]# find /dev -type c //c字符设备
[root@xiaochen ~]# find /dev -type s //s套接字
[root@xiaochen ~]# find /dev -type p //p管道文件

按文件权限:

[root@xiaochen ~]# find . -perm 644 //权限为644
[root@xiaochen ~]# find . -perm -644 -ls //-ls找到的处理动作,权限大于等于644
[root@xiaochen ~]# find . -perm -600 -ls
[root@xiaochen ~]# find /sbin -perm -4000 -ls //包含set uid
[root@xiaochen ~]# find /sbin -perm -2000 -ls //包含set gid
[root@xiaochen ~]# find /sbin -perm -1000 -ls //包含sticky

按正则表达式:

-regex pattern
[root@xiaochen ~]# find /etc -regex '.*ifcfg-eth[0-9]'
.* 任意多个字符
[0-9] 任意一个数字
==找到后处理的动作 ACTIONS: (默认动作-print)==
-print
-ls
-delete 直接删除找到的文件
-exec 后面跟自定义的shell命令,没有交互,直接执行
112/214
-ok 后面跟自定义的shell命令,会询问
[root@xiaochen ~]# find /etc -name "ifcfg*" //默认就是打印
[root@xiaochen ~]# find /etc -name "ifcfg*" -print
[root@xiaochen ~]# find /etc -name "ifcfg*" -ls
[root@xiaochen ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; //红色的是语法结构
[root@xiaochen ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
[root@xiaochen ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@xiaochen ~]# find /etc -name "ifcfg*" -delete

扩展知识:

find结合xargs
xargs可以帮助不是管道的命令接收前面进程的输出,然后拿到最后给这些命令做参数
[root@xiaochen ~]# find /etc -name "ifcfg*" |xargs ls -lh
[root@xiaochen ~]# find . -name "*.txt" |xargs rm -rf
[root@xiaochen ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp //I表示替换

stat

查看文件的状态信息

例:stat /etc/hosts

文件的三个时间:

名词 功能 解释
atime 访问时间access time 是指用ls、cat、head等命令访问过文件的时间
mtime 更改时间modifye time 是指用vim、>覆盖、>>追加等方式修改过文件内容的时间
ctime 改动时间change time 是指用chown、chmod命令改动过文件权限的时间

alias命令别名

应用场合:通常会将比较长的命令定义一个别名(即快捷键),当输入别名时就等同于输入这条命令。

alias查看命令别名,或给命令临时设置别名(即快捷键),命令格式:alias 别名=“命令”

实例:临时给rm -rf /tmp/*设置一个别名为rt,并测试,最后删除rt这个命令别名。

alias 查看所有的命令别名

alias rt='rm -rf /tmp/* ; ls /tmp/ ’ 定义命令别名(即快捷键)

rt 使用rt命令别名

unalias rt 删除rlt这个命令别名

设置永久的别名**:vi /etc/bashrc 在文件最后添加如下内容,然后保存并退出。

alias rt=rm -rf /tmp/*;ls /tmp/

然后exit退出登录,再重新登录,此命令别名就会生效(用alias命令查看)。

任务:

执行以下任务,临时设置命令别名并测试。

设置head -3命令别名为h3 alias h3=‘head -3’
设置tail -3命令别名为t3 alias t3=‘tail -3’
用命令别名h3查看passwd文件的前3行内容 h3 /etc/passwd
用命令别名t3查看passwd文件的最后3行内容 t3 /etc/passwd

文件打包压缩及解压

文件打包及压缩

打包tar:将多个文件包装(也称封装)成一个文件。打包后的文件容量不发生变化。压缩zip:将多个文件包装(也称封装)并压缩成一个文件。压缩后的文件容量会变小。

打包压缩的应用场合:当有100个文件要通过网络发给对方式时,我们可以将这100个文件打包、压缩成一个文件发给对方。对方收到文件后解包解压缩后可正常使用文件。

tar命令用于对文件打包压缩或解压,格式为:“tar 选项”。

打包并压缩文件:“tar -czvf 压缩包名.tar.gz 文件名”

解压并展开压缩包:“tar -xzvf 压缩包名.tar.gz”

选项 作用
-c 创建tar包文件。create
-x 解开tar包文件。extract
-t 查看tar包内有那些文件
-z 调用Gzip压缩或解压
-j 调用bzip2压缩或解压
-v 显示压缩或解压的过程
-f 目标文件名(即tar包文件名)
-r 在tar包中追加新的文件
-p 即–preserve-permissions,保留原始的权限与属性
-P 即Path,使用绝对路径来压缩
-C 即change,指定解压到的目录(警告,-C后面的目录必须已存在)
-delete 删除包中的某个文件
-A 将某个文件增加到包中

Demo1:

复制未打包的文件到远程主机

[root@xiaochen ~]# du -sh /etc
39M /etc
[root@xiaochen ~]# rsync -va /etc/ 192.168.5.32:/tmp //将/etc下的所有文件...

[root@xiaochen ~]# rsync -va /etc 192.168.5.32:/tmp //将/etc目录...

Demo2:

将各个文件分别压缩

将当前目录下所有的文件全部压缩

[root@server ~]# mkdir /tmp/dir1
[root@server ~]# cd /tmp/dir1
[root@server dir1]# gzip *

Demo3:

将当前目录下所有的文件全部解压

[root@server dir1]# gunzip *

=打包(归档),压缩=

c即create创建,f打包后的文件名
[root@xiaochen ~]# tar -czf etc1.tar.gz /etc //-z 调用gzip工具进行压缩
[root@xiaochen ~]# tar -cjf etc2.tar.bz2 /etc //-j 调用bzip2工具进行压缩
[root@xiaochen ~]# tar -cJf etc3.tar.xz /etc //-J 调用xz工具进行压缩
[root@xiaochen ~]# ll -h etc*
-rw-r--r--. 1 root root 8.7M 3月 12 00:08 etc1.tar.gz
-rw-r--r--. 1 root root 7.5M 3月 12 00:08 etc2.tar.bz2
-rw-r--r--. 1 root root 4.8M 3月 12 00:09 etc3.tar.xz

=查询=

[root@xiaochen ~]# file a.txt.gz
a.txt.gz: gzip compressed data, was "a.txt", from Unix, last modified: Wed Nov 8 15:23:10 2017
[root@xiaochen ~]# tar -ztvf etc1.tar.gz //查询gzip压缩的文件
[root@xiaochen ~]# tar -jtvf etc2.tar.bz2 //查询bzip2压缩的文件
[root@xiaochen ~]# tar -Jtvf etc3.tar.xz //查询xz压缩的文件

=解压,解包=

[root@xiaochen ~]# tar -xzvf etc1.tar.gz //x解包
[root@xiaochen ~]# tar -xvf etc1.tar.gz //无需指定解压工具,tar会自动判
断
[root@xiaochen ~]# tar -xvf etc2.tar.bz2 -C /tmp //-C重定向到//tmp目录
[root@xiaochen ~]# tar xf etc3.tar.xz

==解压zip

[root@xiaochen ~]# unzip xxx.zip

tar实战案例

[root@localhost ~]# yum -y install mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mkdir /backup

案例1:

mysql物理备份及恢复

[root@localhost ~]# tar -cJf /backup/mysql.tar.xz /var/lib/mysql
[root@localhost ~]# tar -tvf /backup/mysql.tar.xz
[root@localhost ~]# rm -rf /var/lib/mysql/*
[root@localhost ~]# tar -xf /backup/mysql.tar.xz -C /

案例2:

mysql物理备份及恢复

[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]# tar -cJf /backup/mysql.tar.xz *
[root@localhost mysql]# tar -tvf /backup/mysql.tar.xz
[root@localhost mysql]# tar -xf /backup/mysql.tar.xz -C /var/lib/mysql

案例3:

host A /etc (海量小文件) --------> 本地主机host A /tmp

[root@localhost ~]# time tar -czf - /etc |tar -xzf - -C /tmp
注:- 表示不会真的写到硬盘中去,而是写在内存中去

案例4:

host A /etc (海量小文件) --------> 远程主机host B /tmp

常规方法:scp直接远程拷贝或者是tar打包压缩之后远程拷贝

建议方法(不落地即不经过硬盘的方法):

host B 监听端口

如果host B的防火墙开着,需要添加新端口;如果防火墙是关着的不需要下面操作

[root@hostb ~]# firewall-cmd --permanent --add-port=8888/tcp
[root@hostb ~]# firewall-cmd --reload

在主机B上开启监听端口,接收到数据就进行解包的操作

[root@hostb ~]# nc -l -t 8888 |tar -xzf - -C /tmp

==host A ==

[root@localhost ~]# tar -czf - /etc | nc 172.16.20.21 8888
tar: Removing leading `/' from member names
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值