UNIX Find A File Command

I am a new Sun Solaris UNIX user in our Lab. I would like to know more about finding files from the shell prompt. How do I find a file under UNIX operating systems using bash or ksh shell?

You need to use find command which is used to search files and directories under Linux and Unix like operating systems. You can specify criteria while search files. If no criteria is set, it will returns all files below the current working directory. The find command also supports regex matching and other advanced options.

Another option is locate command. It find files by name. The locate command reads databases prepared by updatedb and displays file names matching at least one of the PATTERNs to screen.


The syntax is:
find /dir/to/search -name "file-to-search"
find /dir/to/search -name "file-to-search" -print
find /dir/to/search -name "file-to-search" -ls
find /dir/to/search -name "regex" -print

Examples – find command

To find all perl (*.pl) files in current directory:
$ find . -name '*.pl'
The . represent the current directory and the -name option specifies all pl (perl) files. The quotes avoid the shell expansion and it is necessary when you want to use wild card based search (without quotes the shell would replace *.pl with the list of files in the current directory).

To list only files and avoid all directories

The following command will only list files and will exclude directories, special files, pipes, symbolic links etc:
$ find . -type f -name '*.pl'
Sample outputs:

Fig.01: Find command in action

The -type -f option only selects files. To select only dirs pass the -type d:
$ find /home/vivek/ -type d -name 'project*'
This will find directory names such as project, projects, projects-perl and so on.

Search all directories

Search file called httpd.conf in all directories:
$ find / -type f -name httpd.conf
Generally this is a bad idea to look for files. This can take a considerable amount of time. It is recommended that you specify the directory name. For example look httpd.conf in /usr/local directory:
$ find /usr/local -type f -name httpd.conf
You may need to run it as root:
$ sudo find / -type f -name httpd.conf

Execute command on all files

Run ls -l command on all *.c files to get extended information :
$ find . -name "*.c" -type f -exec ls -l {} ;
$ find . -name "*.c" -type f -ls
You can run almost all UNIX command on file. For example, modify all permissions of all files to 0700 only in ~/code directory:
$ find ~/code -exec chmod 0700 {} ;
Search for all files owned by a user called payal:
$ find . -user <userid>
$ find . -user payal

Search file for specific sizes

Search for 650 megabytes or above size file in ~/Downloads/ dir:
$ find ~/Downloads/ -size +650M
## gigabytes ##
$ find ~/Downloads/ -size +1G
## kilobytes ##
$ find ~/Downloads/ -size +1024k

Read find command man page for detailed information:
$ man find

Say hello to locate command

To search for a file named exactly foo (not *foo*), type:
$ locate -b 'foo'
Just search for file name matching yum.conf, type:
$ locate yum.conf
To search for a file named exactly yum.conf (not *yum.conf* anywhere in / path), type:
$ locate -b 'yum.conf'
Sample outputs:

Fig.02: locate command in action on Linux based system.

See our previous articles about finding files:

Posted by: SXI ADMIN

The author is the creator of SXI LLC 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts