Unix command to find a file in a directory and subdirectory

in Categories , , last updated December 24, 2017

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 on my Linux/Unix system
locate command in action

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 3 comments/add one below):

3 comment

    Have a question? Post it on our forum!