find: The Search For Files Program
find | |
---|---|
Category | Command Line Program |
Part Of | GNU Core Utilities |
Classification | Tier 1 |
Official Documentation | GNU Find |
1 Arguments
List
find Command Arguments and Options | ||
---|---|---|
Argument | Argument Options or Value | Description |
-H |
Do not follow symbolic links; except while processing the command line arguments. | |
-L |
Follow symbolic links. | |
-P |
Do not follow symbolic links; this is the default. | |
-D |
Output diagnostic information. | |
exec |
Show debug information related to -exec, execdir, -ok, and -okdir. | |
opt |
Show debug information related to expression tree optimization. | |
-Olevel |
Controls the level of query optimisation. | |
0 |
This has the same effect as level 1. | |
1 |
The default optimization level. | |
2 |
-type or -xtype test are performed after any tests based only on the names of files. | |
3 |
This is fastest option, use this level when you want to run the find command as fast as possible. | |
-files0-from |
This command line argument should be used when you want to give the find command a file that lists the starting points rather than listing the starting points on the command line. | |
[ filepath ] |
The path to the file that contains a list of the starting points you would like to use. This can be either an absolute or relative pathname. | |
-version, --version |
Print version information for the find program and then exit. |
|
-xdev |
Exclude directories which are on other filesystems. |
2 Examples
2.1 How To Locate A File By Filename
The find command is the best command to use when you want to locate a specific file or get a list of files which all match a particular pattern.
This example demonstrates searching the entire linux system for a file
named hosts
. The first argument is /etc
,
this indicates the directory where we want to start our
search. The next argument is -name
which tells the find
command that we are looking for a file based on file name (rather than
looking for a directory). The final argument is hosts
which
is the name of the file we are searching for.
user-1@vm:~$ find /etc -name hosts
/etc/avahi/hosts
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/ssl/private’: Permission denied
find: ‘/etc/libvirt/secrets’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
/etc/hosts
user-1@vm:~$
2.2 How To Elide 'Permission denied' Errors From Output
This example is basically the same the previous example but we filter the search
results through grep
and remove any search results which
include the text Permission denied
. So now our results include only
matches to our query which is hosts
.
user-1@vm:~$ find /etc -name hosts 2>&1 | grep -v 'Permission denied'
/etc/avahi/hosts
/etc/hosts
user-1@vm:~$
2.3 Execute Command Using Results of Find
This example demonstrates how the -exec
option works. The -exec
option will accept as input a command with zero or more curly brace pairs {}
.
The curly brace pairs will be replaced with the output from the find
command.
Another options which this example takes advantage of is the -type
option which
is a test used to exclude non-matching file types from the results. In the example below
the f
option is passed to the -type
option which excludes all
non-regular files from the results (such as directories, links, etc).
user-1@vm:~$ sudo find / -name ssh.service -type f -exec cat {} \;
[Unit]
Description=OpenBSD Secure Shell server
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
Alias=sshd.service
user-1@vm:~$
This document was last updated: