服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux文件查找命令总结(上篇)

Linux文件查找命令总结(上篇)

2023-01-28 14:37小熊coder Linux

这篇文章主要介绍了Linux文件查找命令总结(上篇),本文章内容详细,通过案例可以更好的掌握文件查找的相关命令,本篇为上篇,需要的朋友可以参考下

前文

关于Linux文件查找命令总结我们分别介绍了which命令、whereis命令、locate命令、find命令这四个命令,本篇向大家介绍的是:which命令、whereis命令、locate命令。

which命令语法

我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:

  • which 查看可执行文件的位置。
  • whereis 查看文件的位置。
  • locate 配合数据库查看文件位置。
  • find 实际搜寻硬盘查询文件名称。
  1. which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。
  2. 也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

命令格式

which 可执行文件名称

命令功能

which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。

命令参数

  • -n 指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
  • -p 与-n参数相同,但此处的包括了文件的路径。
  • -w 指定输出时栏位的宽度。
  • -V 显示版本信息

which命令使用示例

查找文件、显示命令路径

命令:

?
1
which lsmod

输出:

?
1
2
3
4
5
[root@localhost ~]# which pwd
/bin/pwd
[root@localhost ~]# which adduser
/usr/sbin/adduser
[root@localhost ~]#

说明:

which 是根据使用者所配置的 PATH 变量内的目录去搜寻可运行档的!

所以,不同的 PATH 配置内容所找到的命令当然不一样的!

用 which 去找出 which

命令:

?
1
which which

输出:

?
1
2
3
4
[root@localhost ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
   /usr/bin/which
[root@localhost ~]#

说明:

竟然会有两个 which ,其中一个是 alias 这就是所谓的『命令别名』,意思是输入 which 会等於后面接的那串命令!

找出 cd 这个命令

命令:

?
1
which cd

输出:

Linux文件查找命令总结(上篇)

说明:

cd 这个常用的命令竟然找不到啊!为什么呢?

这是因为 cd 是bash 内建的命令! 但是 which 默认是找 PATH 内所规范的目录,所以当然一定找不到的!

whereis 命令语法

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。

如果省略参数,则返回所有信息。

和find相比,whereis查找的速度非常快,这是因为linux系统会将 系统内的所有文件都记录在一个数据库文件中。

当使用whereis和下面即将介绍的locate时,会从数据库中查找数据,而不是像find命令那样,通 过遍历硬盘来查找,效率自然会很高。

但是该数据库文件并不是实时更新,默认情况下时一星期更新一次。

因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。

命令格式

whereis [-bmsu] [BMS 目录名 -f ] 文件名

命令功能

whereis命令是定位可执行文件、源代码文件、帮助文件在文件系统中的位置。

这些文件的属性应属于原始代码,二进制文件,或是帮助文件。

whereis 程序还具有搜索源代码、指定备用搜索路径和搜索不寻常项的能力。

命令参数

-b 定位可执行文件。

-m 定位帮助文件。

-s 定位源代码文件。

-u 搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件。

-B 指定搜索可执行文件的路径。

-M 指定搜索帮助文件的路径。

-S 指定搜索源代码文件的路径。

whereis命令使用示例

将和*文件相关的文件都查找出来

命令:

?
1
whereis svn

输出:

?
1
2
3
4
5
6
7
[root@localhost ~]# whereis tomcat
 
tomcat:
 
[root@localhost ~]# whereis svn
 
svn: /usr/bin/svn /usr/local/svn /usr/share/man/man1/svn.1.gz

说明:

tomcat没安装,找不出来,svn安装找出了很多相关文件

只将二进制文件 查找出来

命令:

?
1
whereis -b svn

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# whereis -b svn
 
svn: /usr/bin/svn /usr/local/svn
 
[root@localhost ~]# whereis -m svn
 
svn: /usr/share/man/man1/svn.1.gz
 
[root@localhost ~]# whereis -s svn
 
svn:
 
[root@localhost ~]#

说明:

whereis -m svn 查出说明文档路径,whereis -s svn 找source源文件。

locate 命令语法

locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案。

其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了。

在一般的 distribution 之中,数据库的建立都被放在 crontab 中自动执行。

命令格式

Locate [选择参数] [样式]

命令功能

locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的,locate命令在搜寻数据库时比由整个由硬盘资料来搜寻资料来得快,但较差劲的是locate所找到的档案若是最近才建立或 刚更名的,可能会找不到,在内定值中,updatedb每天会跑一次,可以由修改crontab来更新设定值。(etc/crontab)

locate指定用在搜寻符合条件的档案,它会去储存档案与目录名称的数据库内,寻找合乎范本样式条件的档案或目录录,可以使用特殊字元(如”” 或”?”等)来指定范本样式,如指定范本为kcpaner, locate会找出所有起始字串为kcpa且结尾为ner的档案或目录,如名称为kcpartner若目录录名称为kcpa_ner则会列出该目录下包括 子目录在内的所有档案。

locate指令和find找寻档案的功能类似,但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在 执行loacte时直接找该索引,查询速度会较快,索引数据库一般是由操作系统管理,但也可以直接下达update强迫系统立即修改索引数据库。

命令参数

  • -e 将排除在寻找的范围之外。
  • -1 如果 是 1.则启动安全模式。在安全模式下,使用者不会看到权限无法看到 的档案。这会始速度减慢,因为 locate 必须至实际的档案系统中取得档案的 权限资料。
  • -f 将特定的档案系统排除在外,例如我们没有到理要把 proc 档案系统中的档案 放在资料库中。
  • -q 安静模式,不会显示任何错误讯息。
  • -n 至多显示 n个输出。
  • -r 使用正规运算式 做寻找的条件。
  • -o 指定资料库存的名称。
  • -d 指定资料库的路径
  • -h 显示辅助讯息
  • -V 显示程式的版本讯息

locate命令使用示例

查找和pwd****相关的所有文件

命令:

?
1
locate pwd

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
peida-VirtualBox ~ # locate pwd
 
/bin/pwd
 
/etc/.pwd.lock
 
/sbin/unix_chkpwd
 
/usr/bin/pwdx
 
/usr/include/pwd.h
 
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py
 
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc
 
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py
 
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc
 
/usr/lib/syslinux/pwd.c32
 
/usr/share/help/C/empathy/irc-join-pwd.page
 
/usr/share/help/ca/empathy/irc-join-pwd.page
 
/usr/share/help/cs/empathy/irc-join-pwd.page
 
/usr/share/help/de/empathy/irc-join-pwd.page
 
/usr/share/help/el/empathy/irc-join-pwd.page

 搜索etc目录下所有以sh开头的文件

命令:

?
1
locate /etc/sh

输出:

?
1
2
3
4
5
6
7
8
9
peida-VirtualBox ~ # locate /etc/sh
 
/etc/shadow
 
/etc/shadow-
 
/etc/shells
 
peida-VirtualBox ~ #

搜索etc目录下,所有以m开头的文件

命令:

?
1
locate /etc/m

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
peida-VirtualBox ~ # locate /etc/m
 
/etc/magic
 
/etc/magic.mime
 
/etc/mailcap
 
/etc/mailcap.order
 
/etc/manpath.config
 
/etc/mate-settings-daemon

find 命令语法

Linux下find命令在目录结构中搜索文件,并执行指定的操作。

Linux下find命令提供了相当多的查找条件,功能很强大。

由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。

即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。

在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。

命令格式

find pathname -options [-print -exec -ok …]

命令功能

用于在文件树种查找文件,并作出相应的处理

命令参数

  • pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
  • -print: find命令将匹配的文件输出到标准输出。
  • -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为’command’ { } ;,注意{ }和\;之间的空格。
  • -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

命令选项

-name 按照文件名查找文件。

-perm 按照文件权限来查找文件。

-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user 按照文件属主来查找文件。

-group 按照文件所属的组来查找文件。

-mtime -n +n 按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。

-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。

-type 查找某一类型的文件,诸如:

  • b - 块设备文件。
  • d - 目录。
  • c - 字符设备文件。
  • p - 管道文件。
  • l - 符号链接文件。
  • f - 普通文件。

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。

-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。

-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。

-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

另外,下面三个的区别:

  • -amin n 查找系统中最后N分钟访问的文件
  • -atime n 查找系统中最后n*24小时访问的文件
  • -cmin n 查找系统中最后N分钟被改变文件状态的文件
  • -ctime n 查找系统中最后n*24小时被改变文件状态的文件
  • -mmin n 查找系统中最后N分钟被改变文件数据的文件
  • -mtime n 查找系统中最后n*24小时被改变文件数据的文件

find命令使用示例

查找指定时间内修改过的文件

命令:

?
1
find -atime -2

输出:

?
1
2
3
4
5
6
7
8
9
10
11
[root@peidachang ~]# find -atime -2
 
.
 
./logs/monitor
 
./.bashrc
 
./.bash_profile
 
./.bash_history

说明:

超找48小时内修改过的文件

根据关键字查找

命令:

?
1
find . -name "*.log"

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@localhost test]# find . -name "*.log"
 
./log_link.log
 
./log2014.log
 
./test4/log3-2.log
 
./test4/log3-3.log
 
./test4/log3-1.log
 
./log2013.log
 
./log2012.log
 
./log.log
 
./test5/log5-2.log
 
./test5/log5-3.log
 
./test5/log.log
 
./test5/log5-1.log
 
./test5/test3/log3-2.log
 
./test5/test3/log3-3.log
 
./test5/test3/log3-1.log
 
./test3/log3-2.log
 
./test3/log3-3.log
 
./test3/log3-1.log

说明:

在当前目录查找 以.log结尾的文件。 ". "代表当前目录

按照目录或文件的权限来查找文件

命令:

?
1
find /opt/soft/test/ -perm 777

输出:

?
1
2
3
4
5
6
7
8
9
[root@localhost test]# find /opt/soft/test/ -perm 777
 
/opt/soft/test/log_link.log
 
/opt/soft/test/test4
 
/opt/soft/test/test5/test3
 
/opt/soft/test/test3

说明:

查找/opt/soft/test/目录下 权限为 777的文件

按类型查找

命令:

?
1
find . -type f -name "*.log"

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@localhost test]# find . -type f -name "*.log"
 
./log2014.log
 
./test4/log3-2.log
 
./test4/log3-3.log
 
./test4/log3-1.log
 
./log2013.log
 
./log2012.log
 
./log.log
 
./test5/log5-2.log
 
./test5/log5-3.log
 
./test5/log.log
 
./test5/log5-1.log
 
./test5/test3/log3-2.log
 
./test5/test3/log3-3.log
 
./test5/test3/log3-1.log
 
./test3/log3-2.log
 
./test3/log3-3.log
 
./test3/log3-1.log
 
[root@localhost test]#

说明:

查找当目录,以.log结尾的普通文件

查找当前所有目录并排序

命令:

?
1
find . -type d | sort

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@localhost test]# find . -type f -name "*.log"
 
./log2014.log
 
./test4/log3-2.log
 
./test4/log3-3.log
 
./test4/log3-1.log
 
./log2013.log
 
./log2012.log
 
./log.log
 
./test5/log5-2.log
 
./test5/log5-3.log
 
./test5/log.log
 
./test5/log5-1.log
 
./test5/test3/log3-2.log
 
./test5/test3/log3-3.log
 
./test5/test3/log3-1.log
 
./test3/log3-2.log
 
./test3/log3-3.log
 
./test3/log3-1.log
 
[root@localhost test]#

按大小查找文件

命令:

?
1
find . -size +1000c -print

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@localhost test]# find . -size +1000c -print
 
.
 
./test4
 
./scf
 
./scf/lib
 
./scf/service
 
./scf/service/deploy
 
./scf/service/deploy/product
 
./scf/service/deploy/info
 
./scf/doc
 
./scf/bin
 
./log2012.log
 
./test5
 
./test5/test3
 
./test3
 
[root@localhost test]#

说明:

查找当前目录大于1K的文件

find命令之exec使用示例

find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。

exec解释:

-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

{} 花括号代表前面find查找出来的文件名。

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。

大多数用户使用这一选项是为了查找旧文件并删除它们。

建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。

如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

ls -l命令放在find命令的-exec选项中

命令:

?
1
find . -type f -exec ls -l {} \;

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost test]# find . -type f -exec ls -l {} \;
 
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
 
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
 
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
 
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
 
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
 
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
 
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
 
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
 
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
 
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
 
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
 
[root@localhost test]#

说明:

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。

在目录中查找更改时间在n日以前的文件并删除它们

命令:

?
1
find . -type f -mtime +14 -exec rm {} \;

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@localhost test]# ll
 
总计 328
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
-rw-r--r-- 1 root root   33 10-28 16:54 log2013.log
 
-rw-r--r-- 1 root root  127 10-28 16:51 log2014.log
 
lrwxrwxrwx 1 root root   7 10-28 15:18 log_link.log -> log.log
 
-rw-r--r-- 1 root root   25 10-28 17:02 log.log
 
-rw-r--r-- 1 root root   37 10-28 17:07 log.txt
 
drwxr-xr-x 6 root root  4096 10-27 01:58 scf
 
drwxrwxrwx 2 root root  4096 10-28 14:47 test3
 
drwxrwxrwx 2 root root  4096 10-28 14:47 test4
 
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
 
[root@localhost test]# ll
 
总计 312
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
lrwxrwxrwx 1 root root   7 10-28 15:18 log_link.log -> log.log
 
drwxr-xr-x 6 root root  4096 10-27 01:58 scf
 
drwxrwxrwx 2 root root  4096 11-12 19:32 test3
 
drwxrwxrwx 2 root root  4096 11-12 19:32 test4
 
[root@localhost test]#

说明

在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!

当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

在目录中查找更改时间在n日以前的文件并删除它们**,**在删除之前先给出提示

命令:

?
1
find . -name "*.log" -mtime +5 -ok rm {} \;

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@localhost test]# ll
 
总计 312
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
lrwxrwxrwx 1 root root   7 10-28 15:18 log_link.log -> log.log
 
drwxr-xr-x 6 root root  4096 10-27 01:58 scf
 
drwxrwxrwx 2 root root  4096 11-12 19:32 test3
 
drwxrwxrwx 2 root root  4096 11-12 19:32 test4
 
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;
 
< rm ... ./log_link.log > ? y
 
< rm ... ./log2012.log > ? n
 
[root@localhost test]# ll
 
总计 312
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
drwxr-xr-x 6 root root  4096 10-27 01:58 scf
 
drwxrwxrwx 2 root root  4096 11-12 19:32 test3
 
drwxrwxrwx 2 root root  4096 11-12 19:32 test4
 
[root@localhost test]#

说明

在上面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。 按y键删除文件,按n键不删除。

-exec中使用grep命令

命令:

?
1
find /etc -name "passwd*" -exec grep "root" {} \;

输出:

?
1
2
3
4
5
6
7
[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;
 
root:x:0:0:root:/root:/bin/bash
 
root:x:0:0:root:/root:/bin/bash
 
[root@localhost test]#

说明:

任何形式的命令都可以在-exec选项中使用。 在上面的例子中我们使用grep命令。

find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。

查找文件移动到指定目录

命令:

?
1
find . -name "*.log" -exec mv {} .. \;

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@localhost test]# ll
 
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
 
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
 
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
 
[root@localhost test]# cd test3/
 
[root@localhost test3]# ll
 
总计 304
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
-rw-r--r-- 1 root root   61 11-12 22:44 log2013.log
 
-rw-r--r-- 1 root root   0 11-12 22:25 log2014.log
 
[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
 
[root@localhost test3]# ll
 
总计 0[root@localhost test3]# cd ..
 
[root@localhost test]# ll
 
总计 316
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
-rw-r--r-- 1 root root   61 11-12 22:44 log2013.log
 
-rw-r--r-- 1 root root   0 11-12 22:25 log2014.log
 
drwxr-xr-x 6 root root  4096 10-27 01:58 scf
 
drwxrwxr-x 2 root root  4096 11-12 22:50 test3
 
drwxrwxr-x 2 root root  4096 11-12 19:32 test4
 
[root@localhost test]#

用exec选项执行cp命令

命令:

?
1
find . -name "*.log" -exec cp {} test3 \;

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@localhost test3]# ll
 
总计 0[root@localhost test3]# cd ..
 
[root@localhost test]# ll
 
总计 316
 
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
 
-rw-r--r-- 1 root root   61 11-12 22:44 log2013.log
 
-rw-r--r-- 1 root root   0 11-12 22:25 log2014.log
 
drwxr-xr-x 6 root root  4096 10-27 01:58 scf
 
drwxrwxr-x 2 root root  4096 11-12 22:50 test3
 
drwxrwxr-x 2 root root  4096 11-12 19:32 test4
 
[root@localhost test]# find . -name "*.log" -exec cp {} test3 \;
 
cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件
 
cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件
 
cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件
 
[root@localhost test]# cd test3
 
[root@localhost test3]# ll
 
总计 304
 
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
 
-rw-r--r-- 1 root root   61 11-12 22:54 log2013.log
 
-rw-r--r-- 1 root root   0 11-12 22:54 log2014.log
 
[root@localhost test3]#

到此这篇关于Linux文件查找命令总结(上篇)的文章就介绍到这了,下篇的内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_41854911/article/details/122396781

延伸 · 阅读

精彩推荐
  • Linuxlinux更改目录显示颜色实例详解

    linux更改目录显示颜色实例详解

    这篇文章主要介绍了linux更改目录显示颜色实例详解的相关资料,需要的朋友可以参考下...

    wangyang135410942022-01-11
  • Linux如何检查 Linux 服务器的运行时间

    如何检查 Linux 服务器的运行时间

    Linux 中的 uptime 用于查看系统启动后的运行时间。它是一个比较简单的 Linux 命令,下面就一起来学习学习吧!...

    TIAP5962022-11-08
  • Linux详解Linux系统中查询文档的man和info命令

    详解Linux系统中查询文档的man和info命令

    这篇文章主要介绍了Linux系统中查询文档的man和info命令,使用帮助手册是Linux入门学习中的基础,需要的朋友可以参考下...

    Linux命令手册5902019-06-22
  • Linux.htaccess教程之.httacces文件介绍与创建

    .htaccess教程之.httacces文件介绍与创建

    从本指南中,你将可以学习到有关.htaccess文件及其功能的知识,并用以优化你的网站。尽管.htaccess只是一个文件,但它可以更改服务器的设置,允许你做许...

    Linux教程网3342020-06-05
  • LinuxLinux虚拟机怎么拍摄快照并管理?

    Linux虚拟机怎么拍摄快照并管理?

    Linux虚拟机怎么拍摄快照并管理?安装Linux虚拟机后,想要将操作步骤保存成快照,并管理,该怎么实现呢?下面我们就来看看详细的教程,需要的朋友可以...

    百度经验7172020-05-05
  • LinuxLinux Makefile与Shell的问题

    Linux Makefile与Shell的问题

    大概只要知道Makefile的人,都知道Makefile可以调用Shell脚本。但是在实际使用时,并不那么简单,一些模棱两可的地方可能会让你抓狂。你若不信,可以先看...

    Linux之家5252020-08-25
  • LinuxLinux桌面进化史

    Linux桌面进化史

    本周 Linux 刚刚迎来它的28岁生日。自20世纪90年代初期以来,Linux 桌面 也已从简单的窗口管理器发展为成熟、完整的桌面。那么它究竟是如何一步步发展至...

    开源中国4092019-08-29
  • Linuxlinux VPS主机上备份网站和数据库的方法

    linux VPS主机上备份网站和数据库的方法

    现在看到越来越多的VPSer开始使用VPS,在使用VPS的时候一个很重要的任务就是VPS提供备份,虽然一些IDC也提供VPS的备份服务,但要不就是收费,都不太适合...

    Linux教程网8222021-03-14