Skip to content Skip to main navigation Skip to footer

Linux/Unix:如何查找linux或unix服务器上的文件

linux find 命令于linux新手来说,需要查找linux或unix服务器上的某个或某些特定模式的文件,那么如何来定位或查找这些文件呢?
你需要使用find 命令或者locate命令在linux服务器或者类unix服务器上查找文件。
linux find 命令用法
linux find 命令主要用来查找文件,该命令可以查找到你指定的特定目录下的符合特定模式的普通文件或目录文件,指定的查找条件可以是用户名,用户组,文件类型,文件权限,文件创建日期,时间等。而且更重要的一点是find命令可以递归查找整个指定目录下的文件。
使用方法如下:
find {目录} -name {文件名} 动作
该命令默认的对查找到的文件执行的操作是在标准输出上打印查找到的文件,也就是下面的命令:
find / -name “file pattern” -print
Find 命令的示例
1.查找整个根目录下的名为“a.txt”的文件

#find / -name a.txt
 

2.查找/etc/目录下的所有”.conf”普通文件

#find /etc  -type f -name "*.conf"
 

3.查找/home/目录下的除了”*.css”的所有文件
为了查找到除了特定模式下的所有其它文件,可以使用-not 选项 或者“!”

#find /home -not -name "*.css" -print
 

或者

#find /home \! -name "*.css" -print
 

按文件类型查找文件
使用方法如下:

find /dir -type {f|d|l} -name "file pattern" -print
 

其中:
类型选项:

  • f:只查找普通文件
  • d:只查找目录文件
  • l:只查找链接文件

示例:
1.查找/home目录下的所有.c的普通文件

#find /home -type f -name "*.c" -print
 

2.查找/home目录下的所有包含var的目录文件

#find /home -type d -name "*var" -print
 

对查找到的文件执行操作
我们可以对通过find 命令查找到的文件执行某些相应的命令,用法如下:

find /dir -name "file pattern" -exec command {} \;
find /dir -iname "file pattern" -exec command {} \;
 

示例:
1.查找/home目录下的所有“*.sh”文件,并将这些文件的权限设置为777

#find /home -type f -iname "*.sh" -exec chmod 777 {} \;
 

更多参考:
1.其它关于Linux find 命令的用法
2. linux find命令按用户和用户组来查找文件
3. find man帮助 

0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.