文件查找和压缩
一、文件查找
简介
which:命令查找
🥇find:文件查找针对文件名
locate:文件查找,依赖数据库
命令文件查找
which
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# which ll
alias ll='ls -l --color=auto'
/usr/bin/ls
[root@localhost ~]# which pwd
/usr/bin/pwd
[root@localhost ~]# which cd
/usr/bin/cd
whereis
[root@localhost ~]# whereis vim
vim: /usr/bin/vim /usr/share/vim /usr/share/man/man1/vim.1.gz
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
alias别名系统
[root@localhost ~]# alias xie='ls --color=auto -l'
[root@localhost ~]# xie
总用量 8
-rw-------. 1 root root 1514 8月 14 23:48 anaconda-ks.cfg
-rw-r--r--. 1 root root 1562 8月 14 23:53 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 8月 15 00:04 公共
drwxr-xr-x. 2 root root 6 8月 15 00:04 模板
drwxr-xr-x. 2 root root 6 8月 15 00:04 视频
drwxr-xr-x. 2 root root 6 8月 15 00:04 图片
drwxr-xr-x. 2 root root 18 8月 23 20:01 文档
drwxr-xr-x. 2 root root 6 8月 17 11:28 下载
drwxr-xr-x. 2 root root 6 8月 15 00:04 音乐
drwxr-xr-x. 2 root root 179 9月 4 16:41 桌面
任意文件
find
语法
find path options expression action
命令 路径 选项 表达式 动作
按文件名
[root@localhost ~]# find /etc -name 'hosts'
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc -name "hosts"
/etc/hosts
/etc/avahi/hosts
单引号强引,双引号弱引
[root@localhost ~]# find /etc -name "HOSTS"
[root@localhost ~]# find /etc -iname "HOSTS"
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc -iname "HOS*"
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/etc/avahi/hosts
-i忽视大小写
按文件大小
[root@localhost ~]# find /etc -size +5M
/etc/udev/hwdb.bin
[root@localhost ~]# ls -l /etc/udev/hwdb.bin -h
-r--r--r--. 1 root root 8.6M 8月 14 23:50 /etc/udev/hwdb.bin
其中-h把内存变成了以M为单位人性化显示
[root@localhost ~]# dd if=/dev/zero of=/tmp/5M.txt bs=1M count=5
记录了5+0 的读入
记录了5+0 的写出
5242880字节(5.2 MB)已复制,0.00316349 秒,1.7 GB/秒
[root@localhost ~]# find /tmp -size 5M
/tmp/5M.txt
[root@localhost ~]# ls -l /tmp/5M.txt -h
-rw-r--r--. 1 root root 5.0M 9月 4 17:25 /tmp/5M.txt
[root@localhost ~]# find /tmp -size -5M
指定查找的目录深度
[root@localhost ~]# find / -maxdepth 3 -a -name "ifcfg-en*"
[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*"
/etc/sysconfig/network-scripts/ifcfg-ens33
其中4表示4级目录,-a表示and
按文件属主、属组找
[root@localhost ~]# find /home -user aaa
[root@localhost ~]# find /home -group aaa
按文件类型
[root@localhost ~]# find /dev -type d b块设备文件
[root@localhost ~]# find /tmp -type f 普通文件
按文件权限
[root@localhost ~]# find ./ -perm 714
./714.txt
[root@localhost ~]# find ./ -perm 714 -ls
33829873 0 -rwx--xr-- 1 root root 0 9月 7 09:36 ./714.txt
-ls是find的动作之一,精确权限
找到后处理的动作ACTIONS
[root@localhost ~]# find ./ -perm 714 -delete 找到后删除
[root@localhost ~]# find /etc/ -name ifcfg* -ok cp -rvf {} /tmp \; 找到后复制 -ok连接符,{}引用符,\;结束符
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? yes
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? yes
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
二、文件打包及压缩
简介:ta命令是unix/linux系统中备份文件的可靠方法。几乎可以工作于任何环境中,它的使用权限是所有用户建议针对目录
打包,压缩 -cf
语法:tar 选项 压缩包名称 原文件
[root@localhost ~]# ls -l /etc/ |wc -l 查看统计文件数量
278
[root@localhost ~]# tar -cf etc.tar /etc 打包
tar: 从成员名中删除开头的“/”
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
etc.tar 公共 视频 文档 音乐
[root@localhost ~]# tar -czf etc-gzip.tar.gz /etc 打包并压缩,z是gzip
[root@localhost ~]# ls -lh |grep etc
-rw-r--r--. 1 root root 12M 9月 7 11:00 etc-gzip.tar.gz
-rw-r--r--. 1 root root 38M 9月 7 10:58 etc.t
[root@localhost ~]# tar -cf etc.tar /etc
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -czf etc.tar.gz /etc
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cjf etc.tar.bz /etc
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cJf etc.tar.xz /etc
tar: 从成员名中删除开头的“/”
[root@localhost ~]# ls -lh etc.tar*
-rw-r--r--. 1 root root 38M 9月 7 11:18 etc.tar
-rw-r--r--. 1 root root 11M 9月 7 11:19 etc.tar.bz
-rw-r--r--. 1 root root 12M 9月 7 11:19 etc.tar.gz
-rw-r--r--. 1 root root 8.3M 9月 7 11:19 etc.tar.xz
解压,解包: -xf
查看:tar -tf etc.tar t查看f文件名
解压缩
tar xf etc3.tar.xz 简单粗暴
tar -xvf etc2.tar.bz2 -C /tmp -C重定向到/tmp目录
644

被折叠的 条评论
为什么被折叠?



