文件查找
简介:
which :命令查找
find :文件查找,针对文件名
locate : 文件查找,依赖数据库
一、命令文件查找
查找ls命令的位置:
[root@localhost ~]# which ls
alias ls=‘ls --color=auto’
/usr/bin/ls
—>从PATH环境变量:PATH变量就是用于保存可以搜索的目录路径,如果待运行的程序不在当前目录,操作系统便可以去依次搜索PATH变量变量中记录的目录,如果在这些目录中找到待运行的程序,操作系统便可以运行。
或者 whereis vim
二、任意文件
locate
1、locate查找hosts文件
[root@localhost ~]# locate hosts | head -3
/etc/ghostscript
/etc/hosts
/etc/hosts.allow
2、新建文件并查找
[root@localhost ~]# mkdir /home/abc/efg -p(-p 同时新建abc和efg文件)
[root@localhost ~]# locate efg —>新建文件未在数据库中更新 需重启或者刷新数据库
/usr/share/doc/neon-0.30.0/html/refgetst.html
[root@localhost ~]# updatedb —> 刷新数据库
[root@localhost ~]# locate efg
/home/abc/efg ---->找到对应文件
/usr/share/doc/neon-0.30.0/html/refgetst.html
find
语法:
find [path…] [options] [expression] [action]
命令 路径 选项 表达式 动作
按文件名 :
[root@localhost ~]# find /home -name “efg”
/home/abc/efg —>根据正确文件名查找
[root@localhost ~]# find /home -iname “efG”
/home/abc/efg ----> -i 忽略大小写
[root@localhost ~]# find /home -iname “*Fg”
/home/abc/efg —> Fg 查找 不记得文件名
[root@localhost ~]# find /home -iname "eF"
/home/abc/efg
文件查找成功!
按文件大小:
[root@qianfeng ~]# find /etc -size +5M -->文件>5M
[root@qianfeng ~]# find /etc -size 5M -->文件=5M
[root@qianfeng ~]# find /etc -size -5M -->文件<5M
指定查找的目录深度:
[root@qianfeng ~]# find / -maxdepth 4 -a -name “ifcfg-en*”
查询是成功的 —>目录深度是4 可查找到对应文件
[root@qianfeng ~]# find / -maxdepth 3 -a -name “ifcfg-en*”
查询是失败的 —>目录深度不正确 不可查找到对应文件
按文件属主、属组查找:
[root@qianfeng ~]# find /home -user jack //属主是jack的文件
[root@qianfeng ~]# find /home -group hr //属组是hr组的文件
按文件类型:
[root@qianfeng ~]# find /tmp -type f
[root@qianfeng ~]# find /dev -type b
f 普通文件 b 块设备文件 d 目录 p 管道 l 连接
按文件权限:
[root@qianfeng ~]# find . -perm 644 -ls
-ls 是find的动作之一,精确权限(找到并l列出权限信息)
在Linux中最左边是命令 例: touch ls (创建一个ls的文件)
找到后处理的动作 ACTIONS:
找到后默认是显示文件:
[root@localhost ~]# find . -perm 715 -print -->显示文件名
[root@qianfeng ~]# find . -perm 644 -ls —>显示文件属性
找到后删除:
[root@localhost ~]# find /etc -name “775*” -delete
找到后复制:
[root@localhost ~]# find /etc -name “ifcfg*” -ok cp -rvf {} /tmp ;
{}:中是填充寻找的内容
查找近三天修改的文件:
[root@localhost ~]# find /home -ctime -3 | tail -3
/home/file1-h1
/home/abc
/home/abc/efg
-ctime -n +n #按文件创建时间来查找文件,-n指n天以内,+n指n天以
前
查找修改近三天的/mnt下的文件状态
-ctime n 对文件状态的最近一次修改是在 n*24 小时之前。
文件打包及压缩
简介:tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。建议针对目录。
打包、压缩(此项很少做,基本上就是解压)
压缩的原理:把重复的内容用符号代替;文件体积变小,缺点无法直接使用 耗时 如图 无法直接读,需要解压
语法:tar 选项 压缩包名称 源文件
打包、压缩
-f:filename
-c:create
-cf:创建一个文件名
-v :显示压缩过程(进度)
-j:是bzip
-J: 是xzip
下列压缩按照时间所占内存排序由大到小 时间由短到长
[root@localhost ~]# tar -cf etc.tar /etc -->打包(没有压缩)
[root@localhost ~]# tar -czvf etc.tar.gz /etc
–>z是gzip压缩包(压缩包文件名可自定义,后缀不可改)
[root@localhost ~]# tar -cjf etc.tar.bz /etc
tar: 从成员名中删除开头的“/”
(将源文件的/删掉,将压缩的文件放置当前目录,以免解压时覆盖)
观察三个包的体积:
[root@localhost ~]# ll -h etc.tar*
-rw-r–r--. 1 root root 37M 11月 30 10:15 etc.tar
-rw-r–r--. 1 root root 11M 11月 30 11:08 etc.tar.bz
-rw-r–r--. 1 root root 11M 12月 1 19:36 etc.tar.gz
-rw-r–r--. 1 root root 656K 11月 30 11:10 etc.tar.xz
压缩速度和压缩体积成反比。
解压、解包(用的较多)
查看,并没有解压
[root@localhost ~]# tar -tf etc.tar --> -t查看列出备份文件的内容。 -f文件名
解压缩:
[root@localhost ~]# tar xf etc.tar.xz -->- 可省略
[root@localhost ~]# # tar -xvf etc2.tar.bz2 -C /tmp --> -C重定向到//tmp目录