find命令与文件后缀名的区别

6月8日任务
2.23/2.24/2.25 find命令
2.26 文件名后缀

2.23/2.24/2.25

find命令 上

搜索文件命令
用来搜索命令的系统指令
which 命令文件
whereis 命令文件
locate 命令
安装locate:yum install -y mlocate
安装后不能立即使用locate命令,需要使用updatedb生成引索数据文件

快捷命令
ctrl l 清除当前终端显示的内容
ctrl c 终止输入
ctrl u 先前删除
ctrl d 向后删除
ctrl e 光标移动到最后
ctrl a 光标移动到最前

find查找使用格式
find 路径 -type(条件) 文件类型 条件 "名称条件"
-type 查找某一类型的文件,如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
如查找一个软连接文件,如下:

[root@localhost ~]# find /etc/ -type l -name "rc*"      在etc下查找一个名字包含rc的软连接文件
/etc/rc.local
/etc/rc0.d
/etc/rc1.d
/etc/rc2.d
/etc/rc3.d
/etc/rc4.d
/etc/rc5.d
/etc/rc6.d
[root@localhost ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 5月  28 22:19 /etc/rc.local -> rc.d/rc.local

find命令 中

find按照文件访问、修改、更改时间来查找
-amin n 查找系统中最后N分钟访问的文件
-atime n 查找系统中最后n24小时访问的文件
-cmin n 查找系统中最后N分钟被改变文件状态的文件
-ctime n 查找系统中最后n
24小时被改变文件状态的文件
-mmin n 查找系统中最后N分钟被改变文件数据的文件
-mtime n 查找系统中最后n*24小时被改变文件数据的文件

查看一下文件的时间信息

[root@localhost jiaoben]# stat jisuan.py 
  File: 'jisuan.py'
  Size: 278         Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d  Inode: 33589130    Links: 2
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/   xiang)   Gid: ( 1000/   xiang)
Access: 2018-06-06 17:26:41.461758293 +0800               atime 文件访问时间
Modify: 2018-06-06 15:57:44.692772384 +0800               mtime 更改文件内容更新此处时间,更改内容后Change time时间会一起更新
Change: 2018-06-07 14:57:04.294311968 +0800               ctime 更改权限会更新此处时间
 Birth: -

根据文件时间属性来查找出相关文件,如查找出一天内的更改权限的文件:

[root@localhost src]# find jiaoben/ -type f -ctime -2
jiaoben/1.py
jiaoben/2.py
jiaoben/2.pyc

多条件查找 -o ; -o表示或者,用于多个条件

find jiaoben/ -type f -o -ctime -2 -o -name "jiaoben.*"
jiaoben/
jiaoben/1.py
jiaoben/2.py
jiaoben/2.pyc
jiaoben/2.pyo
jiaoben/jisuan.py
jiaoben/123.txt
jiaoben/.123.txt.swp

find命令 下

find查找硬链接文件,首先查看硬链接的源文件的inode节点,用inode节点查找出链接文件和源文件的位置

[root@localhost src]# ls -i jiaoben/jisuan.py 
33589130 jiaoben/jisuan.py
[root@localhost src]# find / -inum 33589130
/usr/local/src/jiaoben/jisuan.py
/usr/local/src/jisuan.py

find 查找某几个小时时间内的更改
-amin n 查找系统中最后N分钟访问的文件
-mmin n 查找系统中最后N分钟被改变文件数据的文件
-cmin n 查找系统中最后N分钟被改变文件权限的文件

现在查找出两个小时内修改过权限的文件

[root@localhost src]# find jiaoben/ -type f -cmin -120
jiaoben/jisuan.py
[root@localhost src]# find jiaoben/ -type f -cmin -120 -exec ls -l {} \;
-rwxr-xr-x 2 xiang xiang 278 Jun 6 15:57 jiaoben/jisuan.py
-exec命令解释    
-exec ls -l  {} \;   命令格式,将find查找出来的文件通过-exec递归给后面命令操作,{}表示查询出来递归的结果,  \;   ;表示结束\给分号脱意
更多示例:
[root@localhost src]# find jiaoben/ -type f -amin -3600 -exec mv {} /tmp/{}.bak \;
mv: cannot move 'jiaoben/jisuan.py' to '/tmp/jiaoben/jisuan.py.bak': No such file or directory
mv: cannot move 'jiaoben/123.txt' to '/tmp/jiaoben/123.txt.bak': No such file or directory
说明:这里我查找的是/usr/local/src/jiaoben下的文件。-exec作为操作会把jiaoben这个目录也作为路径的一部分来执行,但是在我指定的/tmp/目录下不存在jiaoben这个目录,-exec又不可能给你创建出来这个目录,所以会报错“ No such file or directory”如果/tmp/目录下存在jiaoben这个目录则成功移动备份,在/tmp/目录下创建jiaoben目录后测试又如下
[root@localhost src]# mkdir /tmp/jiaoben
[root@localhost src]# find jiaoben/ -type f -amin -3600 -exec mv {} /tmp/{}.bak \;
[root@localhost src]# ls jiaoben/
1.py  2.py  2.pyc  2.pyo
[root@localhost src]# ll /tmp/jiaoben/
total 20
-rw-r--r-- 1 root  root      6 Jun  6 17:10 123.txt.bak
-rw-r--r-- 1 root  root      0 Jun  6 17:10 123.tx~.bak
-rw-r--r-- 1 root  root  12288 Jun  6 17:10 123_txt.swp.bak
-rw-r--r-- 1 root  root      0 Jun  6 17:10 4913.bak
-rwxr-xr-x 2 xiang xiang   278 Jun  6 15:57 jisuan.py.bak

find 通过大小查找文件

以下是通过大小查找一个文件,多用于文件备份删除处理
find 路径 -type(类型) -fdl... -size +10M -exec ls -lh {} \;
示例如下(查找出大于10M的文件并使用ls -lh列出文件详细信息):

[root@localhost jiaoben]# find /usr/ -type f -size +10M -exec ls -lh {} \;
-rw-r--r--. 1 root root 102M May 28 22:17 /usr/lib/locale/locale-archive
-rwxr-xr-x. 1 root root 52M Apr 1 2015 /usr/lib64/xulrunner/libxul.so
摘抄:
1. 基本用法:
      find / -name 文件名      find ver1.d ver2.d -name '*.c' -print    查找ver1.d,ver2.d *.c文件并打印      find . -type d -print 从当前目录查找,仅查找目录,找到后,打印路径名。可用于打印目录结构。
2. 无错误查找:
      find / -name access_log 2 >/dev/null
3. 按尺寸查找:
      find / -size 1500c (查找1,500字节大小的文件,c表示字节)
      find / -size +1500c (查找大于1,500字节大小的文件,+表示大于)    
      find / -size +1500c (查找小于1,500字节大小的文件,-表示小于)    
4. 按时间:
      find / -amin n 最后n分钟 
      find / -atime n 最后n天
      find / -cmin n 最后n分钟改变状态
      find / -ctime n 最后n天改变状态
5. 其它:
      find / -empty 空白文件、空白文件夹、没有子目录的文件夹
      find / -false 查找系统中总是错误的文件
      find / -fstype type 找存在于指定文件系统的文件,如type为ext2
      find / -gid n 组id为n的文件
      find / -group gname 组名为gname的文件
      find / -depth n 在某层指定目录中优先查找文件内容
      find / -maxdepth levels 在某个层次目录中按递减方式查找
6. 逻辑
      -and 条件与 -or 条件或
7. 查找字符串
      find . -name '*.html' -exec grep 'mailto:'{}

2.26 文件名后缀

linux中后缀文件名不代表实际的意义,比如linux中的某.exe文件,它不代表是windows下的程序文件,因为它可能是touch创建出来的一个文本文件
基本上LInux的文件是没有所谓的扩展名,因为一个Linux能否被执行,与它的第一列的10个属性有关,与文件名一点关系都没有。
不过可以被执行和执行成功是不一样的。能不能执行成功要看文件的实际内容。我们仍然希望可以有扩展名告诉我们该文件是什么东西,所以我们通常还是会以适当的扩展名来表示该文件是什么种类
linux系统中文件的后缀名是为了方便区分管理,如.conf、.tar.gz
查询系统现在时间
[root@localhost jiaoben]# date
2018年 06月 08日 星期五 15:11:50 CST
查看系统字符集
[root@localhost jiaoben]# echo $LANG
zh_CN.utf-8 zh_CN.utf-8 是中文字符集 en 是英文字符集

2.27 linux与windows互传文件

安装lrzsz传输工具,只能在xshell和securecrt中使用,putty是不支持这个工具的

[root@localhost jiaoben]# yum install lrzsz
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirror.bit.edu.cn
 * updates: mirrors.huaweicloud.com
正在解决依赖关系
  正在安装 : lrzsz-0.12.20-36.el7.x86_64 1/1 
  验证中 : lrzsz-0.12.20-36.el7.x86_64 1/1 

已安装:
  lrzsz.x86_64 0:0.12.20-36.el7                                                                       

完毕!

lrzsz使用方法
下载linux中的文件
[root@localhost jiaoben]# sz jisuan.py
find命令与文件后缀名的区别
上传windows中的文件
find命令与文件后缀名的区别
传输过程
find命令与文件后缀名的区别
上传完毕
find命令与文件后缀名的区别
这里注意lrzsz工具不支持大文件传输,如4GB及以上大小的文件!

猜你喜欢

转载自blog.51cto.com/8844414/2126432