Linux文件查找

Linux常用命令中,有些命令可以帮助我们查找二进制文件,帮助手册或源文件的位置,也有的命令可以帮助我们查找磁盘上的任意文件,今天我们就来看看这些命令如何使用。

which

which命令会在PATH变量指定的路径中,搜索某个系统命令的位置。例如:

bandit4@bandit:~/inhere$ which -a which  #查看命令which所在位置,-a参数表示找出所有
/usr/bin/which
/bin/which

PATH变量有哪些内容呢?我们来看一下(不同电脑可能不同):

bandit4@bandit:~/inhere$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

PATH环境变量存放着一些路径信息,例如/usr/bin,当你在shell终端敲入一个命令,但是在PATH中包含的路径下没有时并且也不是内置命令时,就会提示:command not found。

whereis

whereis命令用于搜索程序的二进制文件源代码文件帮助文档。例如:

bandit4@bandit:~/inhere$ whereis ls #如果上述三者有,则三者都会显示。
ls: /bin/ls /usr/share/man/man1/ls.1.gz

bandit4@bandit:~/inhere$ whereis -m ls # -m:只查看ls的帮助手册
ls: /usr/share/man/man1/ls.1.gz

bandit4@bandit:~/inhere$ whereis -b ls # -b:只查找ls的二进制文件
ls: /bin/ls

bandit4@bandit:~/inhere$ whereis stdio.h #查找stdio.h头文件,和帮助手册
stdio: /usr/include/stdio.h /usr/share/man/man3/stdio.3.gz

同样地,它不能查找到内置命令。

type

type用于查看命令类型,一般有以下类型:

名称描述
alias别名
keyword关键字
builtin内置命令
file外部命令

而常见参数如下:

参数描述
-t输出类型名,如file
-p如果是外部命令,则显示其所在路径
-a对于外部命令,它会显示命令路径,命令类型等信息

我们来看几个例子:

bandit4@bandit:~/inhere$ type ls
ls is aliased to `ls --color=auto'

bandit4@bandit:~/inhere$ type cd    #cd是一个内置命令
cd is a shell builtin

bandit4@bandit:~/inhere$ type find
find is /usr/bin/find

bandit4@bandit:~/inhere$ type  function  #function是一个shell关键字
function is a shell keyword

bandit4@bandit:~/inhere$ type -a which  #显示所有路径
which is /usr/bin/which
which is /bin/which

locate

前面所说的命令都限于查找命令,帮助手册或源文件,而locate用于快速查找任何文件。它从一个系统数据库进行文件查找,而不需要遍历磁盘,因此速度极快。通常该系统数据库每天更新一次(可以查看系统的/etc/cron.daily/mlocate,不同系统可能不一样)。
常见选项如下:

参数描述
-e仅查找存在的文件
-q安静模式,不会显示任何错误讯息
-n至多显示 n个输出
-r使用正规运算式
-i查找忽略大小写
-c打印匹配结果数量

假设当前目录早已存在以下文件:
locate.txt locate.log LOCATE.zip

我们来看一些实例。

## 快速查找文件
bandit4@bandit:~ locate locate.txt  #查找locate.txt
/home/hyb/workspaces/shell/locate/locate.txt

## 查找存在的文件
bandit4@bandit:~ locate locate.txt  #查找之前删除locate.txt
#虽然文件不存在,但是仍然被查找出来
/home/hyb/workspaces/shell/locate/locate.txt 

bandit4@bandit:~ locate -e locate.txt  #-e参数可以查找只存在的文件
#(由于该文件不存在,因此也不会被查找出来)

## 查找计算文件的数量
bandit4@bandit:~ locate -c locate.log #只计算查找到的数量
 1
 
## 忽略大小写查找
bandit4@bandit:~ locate -i locate.zip
/home/hyb/workspaces/shell/locate/LOCATE.zip

## 使用正则表达式精确查找
# 普通的查找是模糊匹配的,因此只要目标名称中包含要搜索的名称,都会被搜索出来
bandit4@bandit:~ locate -r /locate.log$  #查找以/locate.log结尾的文件 

locate查找存在的一个问题是,如果最近有文件被删除,它仍然能找出来,最近有文件增加,它却找不到。也就是说,它的查找并不具备实时性。当然我们可以手动执行updatedb命令来更新数据库(可能需要root权限)。

find

find命令是linux下一个强大的查找命令。与locate命令相比,它需要遍历磁盘文件,因此查找速度较慢,但正因如此,它的实时性比locate好得多。另外一方面,find命令的查找条件比locate丰富得多。

以名称为条件

最常用的恐怕就是以文件名为条件了,涉及参数-name-iname,例如:
当前目录下查找以sort开头的文件:

bandit4@bandit:~/inhere$ find ./ -name "*file*"
./-file09
./-file06
./-file01
./-file02

bandit4@bandit:~/inhere$ find ./ -iname "*FILE*" # 忽略大小写
./-file09
./-file06
./-file01
./-file02
以权限为条件

有时候需要查找特定权限的文件,可以使用-perm参数,例如查找当前目录下权限为777的文件:

find ./ -perm 777
./test
./sort.txt
以文件类型为条件

涉及参数-type,例如要查找当前目录下的符号链接文件:

bandit4@bandit:~/inhere$ find -type f
./-file09
./-file06
./-file01

主要类型有:

类型描述
f普通文件
d目录
b块设备文件
c字符设备文件
l符号链接
s套接字
p管道文件
以文件大小为条件

涉及参数-size,例如:

bandit4@bandit:~/inhere$ find ./ -size 1k #查找当前目录下小于1k的文件
./-file09
./-file06
./-file01
./-file02
./-file05

bandit4@bandit:~/inhere$ find -size +1w#查找当前目录下大于1w的文件
.
./-file09
./-file06
./-file01
./-file02
./-file05

常用单位有:

单位描述
k千字节
M兆字节
G吉字节
c字节
b块,一般为512字节
w字大小,两个字节

以归属为条件

涉及参数-user,-nouser,-group,-nogroup等,例如:

find ./ -user root  #查找当前目录下root用户的文件
find ./ -nouser   	#查找当前目录下root用户的被删除的文件
					# -group,-nogroup类似的用法,只不过条件是用户组。
以时间为条件

涉及参数-mtime,-atime,-ctime,-newer,-anewer,-cnewer,-amin,-cmin等,例如:

find ./ -mtime 3 		#查找3天前更改过的文件
find ./ -mtime -3 		#查找3天内更改过的文件
find ./ -mtime 0 		#查找今天更改过的文件
find ./ -newer sort.txt #查找比sort.txt修改时间更新的文件
find ./ -anewer sort.txt #查找比sort.txt访问时间更新的文件
find ./ -amin  5 		#查找5分钟之前访问过的文件

注:

指令描述
-atime最后访问时间
-mtime最后修改时间
-ctime最后修改时间,这里包括属性和权限

find命令的查找条件比较多,而其用法也非常丰富,本文仅简单介绍,后面的文章将会介绍find的一些高级用法。

strings

strings命令在对象文件或二进制文件中查找可打印的字符串。字符串是4个或更多可打印字符的任意序列,以换行符或空字符结束。 strings命令对识别随机对象文件很有用。

-a --all:扫描整个文件而不是只扫描目标文件初始化和装载段
-f –print-file-name:在显示字符串前先显示文件名
-n –bytes=[number]:找到并且输出所有NUL终止符序列
- :设置显示的最少的字符数,默认是4个字符
-t --radix={o,d,x} :输出字符的位置,基于八进制,十进制或者十六进制
-o :类似–radix=o
-T --target= :指定二进制文件格式
-e --encoding={s,S,b,l,B,L} :选择字符大小和排列顺序:s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit

总结

命令描述
which命令可用于查找命令位置。不能查找内置命令位置。
whereis可查找命令的位置,手册,源文件等。不能查找内置命令位置。
locate查找数据库,查找速度块;缺点:模糊匹配,匹配路径,实时性差。
find遍历磁盘,准确查找,功能丰富;缺点:速度较慢。
strings在对象文件或二进制文件中查找可打印的字符串

find高级用法

查找空目录
bandit4@bandit:~/inhere$ find -type d -empty查 # 找空目录只需要利用-empty参数即可
查找时排除文件或目录
查找时排除文件 ! -name

例如,我们按照名字查找某一类文件,但是又不想找到其中的.log文件时,可以使用!逻辑运算符查找,例如:

bandit4@bandit:~/inhere$ find -name "*file*" ! -name "*file02" # 排除*file02文件
./-file01
./-file05
./-file03
./-file04
./-file00
查找时排除目录-path

我们在查找某些文件时,可能已经知道某个目录有,但又不想浪费时间再次查找,可以使用-prune参数和-path参数,例如:

find .  -path ./test -prune -o -name "*.txt"

这里查找txt文件,但是排除test目录下的txt文件。但是注意./test不能写成./test/,否则结果将不正确。测试的时候,发现./test仍然会出现在查找结果中。另外 -prune不要和-depth(-delete)一起使用,否则-prune将不起作用

如果需要排除多个目录呢?

bandit4@bandit:~/inhere$ find .  -path  /home/bandit4 -prune -o -name "*file*"
./-file09
./-file06
./-file01

注意,这里(\)前后都有空格。它是告诉shell后面的字符不解释,让find命令自己解释其作用。

当然了,排除目录还可以使用逻辑符号。例如:

find ./ -name "*.txt" ! -path "./test"

对查找到的文件执行命令操作
在查找到文件之后,我们可能需要对它进行操作,例如删除,移动等。我们可以利用xargs。例如:

find -name "*.log" |xargs rm -f 		#找到.log文件后,删除
find -name "*test" |xargs chmod 755 	#将找到文件的权限修改为755
find -name "*test" |xargs grep "hello" 	#查找包含hello字符串的test文件

xargs后面跟的是要执行的命令,这里只是简单举例。
我们还可以利用-exec参数。例如:

find ./ -name "*txt"  -exec rm -f {} \;		#找到后删除
find ./ -name "*txt"  -exec cp {} ./test \;	#找到后复制至test目录下

这里的{}指代了查找到的内容。;作为命令参数结束的标志,因此是必要的。rm 带上-i参数,可进行交互式删除,即需要询问。

我们也可以利用-ok参数,它与-exec的差别在于,它会询问用户,很适合用于删除:

find ./ -name “*.log” -ok ls -al {} ;
< ls … ./locate/locate.log > ?

实战

Q1. 查找文件夹下类型为text的文件
https://overthewire.org/wargames/bandit/bandit5.html

bandit4@bandit:~/inhere$ ls
-file00  -file01  -file02  -file03  -file04  -file05  -file06  -file07  -file08  -file09

bandit4@bandit:~/inhere$ file --mime-type ./* | grep text
./-file07: text/plain

Q2. 查找符合以下条件的文件

requirements:

  • human-readable
  • 1033 bytes in size
  • not executable
bandit5@bandit:~/inhere$ find . -type f -size 1033c ! -executable -exec file {} + | grep -w text
./maybehere07/.file2: ASCII text, with very long lines

Q3. The password for the next level is stored somewhere on the server and has all of the following properties:

  • owned by user bandit7
  • owned by group bandit6
  • 33 bytes in size
bandit6@bandit:~$ find /  -size 33c -group bandit6 -user bandit7 | grep bandit7
/var/lib/dpkg/info/bandit7.password

Q4. The password for the next level is stored in the file data.txt next to the word millionth

bandit7@bandit:~$ grep millionth data.txt
millionth       cvX2JJa4CFALtqS87jk27qwqGhBM9plV

Q5. The password for the next level is stored in the file data.txt and is the only line of text that occurs only once

bandit8@bandit:~$ sort data.txt | uniq -u
UsvVyFSfZZWbi6wgC7dAFyFuR6jQQUhR

The password for the next level is stored in the file data.txt in one of the few human-readable strings, beginning with several ‘=’ characters.

bandit9@bandit:~$ strings data.txt | grep "="
2========== the
========== password
>t=     yP
rV~dHm=
========== isa
=FQ?P\U
=       F[
pb=x
J;m=
=)$=
========== truKLdjsbJ5g7yyJ2X2R0o3a5HQJFuLk
iv8!=

Q6. The password for the next level is stored in the file data.txt, which contains base64 encoded data

bandit10@bandit:~$ base64  -d data.txt
The password is IFukwKGsFW8MOq3IRFqrxE1hxTNEbUPR

Reference

  1. https://zhuanlan.zhihu.com/p/52616109
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值