Unix command to find a file in a directory and subdirectory

I think I lost (or forgot the file location) a file named toms-first-birthday.mp4 on my Unix based system. Is there is a Unix bash shell command to find a file called “toms-first-birthday.mp4” in a directory and subdirectories?

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:

locate command in action


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • B3n00x7 Dec 20, 2017 @ 10:28

    What about if dir is fresh, it miss updatedb ;)

    • 🐧 Vivek Gite Dec 20, 2017 @ 10:58

      Use find command. It will always search for all file unlike locate.

  • Seshadri Kothandaraman Dec 21, 2017 @ 2:51

    A convenient hack is this:
    find | grep fileName
    or
    find | grep somePartOfFileName

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum