Linux中查找文件、命令(find、 which、whereis )以及文件打包

1、按名字、文件大小、修改时间 find 查找

find /tmp/  -name "1.txt" 按照文件名字
find /tmp/  -iname "FILE" 忽略大小写
find /tmp/ -name "*.txt"
find /etc -size +5M  按照文件大小查找
查找在/etc/大于5M的文件
find /etc -size 5M
查找正好是5M的文件
find /etc -size -5M
find /etc -mtime +5	
 按照修改时间查找

#了解
find /opt -mtime +5         #修改时间5天之前
find /opt -atime +1         #访问时间1天之前

2、按文件类型、权限进行查找

按文件类型:
find /dev -type f						   //f普通
find /dev -type d						   //d目录
find /tmp -type f | wc -l
find /dev/ -type b       //查看当前系统下有多少块磁盘

按照文件权限
find . -perm 644  .叫做当前目录
查找当前目录下 权限为644

find /tmp ! -name "1.txt"
! 取反

find /tmp/ -name "1.txt" -o -name "2.txt" 

使用 -a这个参数可以链接2个不同的条件且这2个条件必须要满足!
     -o 在这个参数可以链接2个条件,只要满足1个就会被找出来

find /tmp -empty 查找空文件或者目录


查找出来/tmp下权限是644且名字是.txt结尾的文件
find  /tmp/ -perm 644 -a -name "*.txt" -a -type f

3、查找后做出指令

tar czf tmp.tar.gz   /tmp/    压缩命令
find /etc -name "ifcfg-*" | wc -l  统计名字包含ifcfg的个数
find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;  找到名字包含ifcfg的并且拷贝到tmp目录下

查找  路径  按名   以ifcfg开头
固定参数传递
复制   强制并且显示过程    {}固定占位  路径  \;固定结束 结束符

find / -name "*.txt" -exec rm -rf {} \;
参数传递  删除  占位  \;结尾

find . -name "*.txt" |xargs rm -rf 
find . -name "*.txt" |xargs rm -rvf
find /tmp/\*.txt | xargs rm -rf
exec 每处理一个文件或者目录,它都需要启动一次命令,效率不高


find /tmp/ -name "test*" | xargs -i cp {} /
find /etc/passwd -maxdepth 1 -type f -a -name "passwd" | xargs -i cp {} /tmp
#-i标识允许{}进行占位符占位
find /etc/passwd -maxdepth 1 -type f -a -name "passwd" | xargs cp -rvf -t /tmp
#-maxdepth 1表示只递归当前一层目录
#-t表示调试并且修正命令
find /tmp/ -name "*.txt" -exec tar czf  `date +%F`.tar.gz {}  \;
#错误的只能压出来一个
#压缩尤其是带有重命名的,请使用xargs去压缩操作
find /tmp/ -name "*.txt" | xargs tar czf `date +%F`.tar.gz

4、which/whereis方式查找

which
which命令用于在用户的PATH环境变量指定的目录中查找并显示某个命令的完整路径。它只会在PATH环境变量列出的目录中查找可执行文件。如果找到了指定的命令,which会打印出该命令的完整路径;如果没有找到,则不会显示任何输出(或者可能显示一个错误信息,这取决于具体的shell和which命令的实现)。

用法示例:
bash
which python
这个命令会查找并显示python命令的完整路径,如果python在PATH环境变量指定的目录中。


whereis
whereis命令用于查找二进制程序、源代码和手册页的位置。它会搜索标准目录(如/bin、/usr/bin、/sbin、/usr/sbin等)来查找指定的命令的二进制文件,并可能查找源代码(在/usr/src中)和手册页(在/usr/share/man、/usr/local/man或/usr/share/doc中)。whereis提供的信息比which更广泛,因为它不仅限于可执行文件,还包括了相关的文档和源代码位置(如果可用的话)


5、打包

tar tf 不解压直接查看内容
tar czf  压缩包名字    要压缩的文件或者目录
tar  czf  `date +%F`.mysql.tar.gz /tmp

6、解压缩

tar xf  包名  解压当前路径下
tar xf x.tar.gz   -C  /
.zip结尾的包 必须安装  yum -y install unzip
unzip  xx 解压

你可能感兴趣的:(linux,运维,服务器)