You need to use the find command on a Linux or Unix-like system to search through directories for files.
Syntax
The syntax is
find /search/directory/ -name "matching file search criteria" -actions
find /dir/to/search -name "pattern" -print
find /dir/to/search -name "file-to-search" -print
find /dir/to/search -name "file-to-search" -print [-action]
The find command will begin looking in the /dir/to/search/ and proceed to search through all accessible subdirectories. The filename is usually specified by the -name option. You can use other matching criteria too:
- -name file-name – Search for given file-name. You can use pattern such as *.c
- -iname file-name – Like -name, but the match is case insensitive. For example, the patterns `fo*’ and `F??’ match the file names `Foo’, `FOO’, `foo’, `fOo’, etc. The pattern `*foo*` will also match a file called ‘.foobar’.
- -user userName – The file’s owner is userName
- -group groupName – The file’s group owner is groupName
- -type N – Search by file type. N can be any one of the following:
- b : block (buffered) special
- c : character (unbuffered) special
- d : directory
- p : named pipe (FIFO)
- f : regular file
- l : symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
- s : socket
- D : door (Solaris Unix)
The default action is to display the file name when criteria is matched. You can specify the following actions when file found:
- -print : Show pathnames of matching files.
- -exec cmd {} + : Run command cmd on a file.
- -ls : List current file in ls -dils format on screen.
- -delete : Delete file.
Unix find command examples
To find and report toms-first-birthday.mp4 file in your HONE directory, enter:
$ find $HOME -name "toms-first-birthday.mp4" -print
To find and report all mp4 files starting at the /home/vivek/ directory, enter:
$ find /home/vivek/ -name "*.mp4" -print
To find and report all mp4 files starting at the /home/vivek/ and /tmp/ directory, enter:
$ find /home/vivek/ /tmp/ -name "*.mp4" -print
For more info, see the Unix find command manual page:
$ man find
A note about locate command on Linux/Unix
To find files by name use locate command:
locate file-name
locate pattern
locate [option] pattern
locate toms-first-birthday.mp4 | more
locate -b '\toms-first-birthday.mp4'
locate *.sh | more
Sample outputs:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 3 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
What about if dir is fresh, it miss updatedb ;)
Use find command. It will always search for all file unlike locate.
A convenient hack is this:
find | grep fileName
or
find | grep somePartOfFileName