How Do I Search My Linux and Unix Server For a File?

I am a new Linux system (CentOS) user. How do I search my Linux box for a file? How can I locate files on a Linux based system using bash command line?

You need to either use find command or locate command to search files on a Linux or Unix-like server.

Find command

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements find
Est. reading time 5m
[/donotprint]The find command is used to find files on a Linux or Unix like system. It will search directories you specify for files that match the supplied search condition. A search condition can be used to search files by name, owner, group, type, permissions, date, time, case, and other criteria. The search is recursive i.e. it will search all subdirectories.ooks like this:

find {dir-name} -name {file-name} action


find where-to-look criteria action

The default action is to print file names:

find /dir/ -name "file-to-search" -print

Find command examples

To find out a file called foo.txt in entire server, type:
# find / -name foo.txt
To find out httpd.log file:
# find / -type f -name httpd.log
To find out httpd.log file in /home/web-server/ directory, type:
# find /home/web-server/ -type f -name httpd.log
To find out httpd.log file in /home/web-server/ directory without case i.e. match httpd.log, HTTPD.LOG, HTTpd.LOG case, type:
# find /home/web-server/ -type f -iname httpd.log
To find out all php file (*.php) in /var/www/ directory, enter:
# find /var/www/ -type f -iname "*.php" -print

Find all files except “*.c” in a directory called ~/projects/?

To find all files except a specific pattern i.e. invert the search with -not or !.
# find /dir/to/search/ -not -name "*.c" -print
# find $HOME -not -iname "*.c" -print

# find /dir/to/search/ \! -name "*.c" print
# find $HOME \! -iname "*.c" print

Finding file by type

The syntax is as follows:

find /dir/to/search/ -type X -name "file_pattern" -print
find $HOME -type X -iname "file_pattern" -print

Where -type X can be any one of the following chracter:

  1. f : Search for normal file only.
  2. d : Search for directory only.
  3. l : Search for symbolic link only

To search for all *.pl (perl) files in /var/www/, enert:

find /var/www/ -type f -name "*.pl" -print
## OR case insensitive search ### 
find /var/www/ -type f -iname "*.pl" -print

How do find files by content on a Linux or UNIX based server?

Use grep command as follows:

grep 'string' *.txt
grep -R 'string' *.txt

Search /etc/ directory for all files and only show files that content an IP address
# find /etc/ -iname "*" | xargs grep ''
See “How To Find Files by Content Under UNIX and Linux” tutorial for more information.

Executing commands on files

You can run any command (such as rm or cp) on files that located using find command. The syntax is:

find /dir/to/search [options] -name "file_pattern" -exec command-name1 {} \;
find /dir/to/search [options] -iname "file_pattern" -exec command-name1 {} \;
find /dir/to/search type f -iname "file_pattern" -exec command-name1 -arg1 {} \;

In this example, find all *.php files as set permission to 0644 using chmod command:
# find /var/www/ -type f -iname "*.php" -exec chmod 700 {} \;

Finding files by owner or group

To find all files owned by the “www” user, type:
# find / -user www -print
To find all files owned by the “lighttpd” group, type:
# find / -group lighttpd -print

See also:

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

🐧 10 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
10 comments… add one
  • Dr.M.Bärmann Apr 15, 2008 @ 22:35

    Make it more easy. i did not understan nothing. can you tell people simply things? I still doo not know who you areß what are you dooing? nothing is that what you want? think abaut. Young People may know this, but what abaut the 50-60 generation.

    • Jack Mar 4, 2011 @ 4:44

      Obvious, but still pretty funny… *and* you fell for it. Didn’t you think it was a bit odd he could spell “generation” but not “do”?? Uh, herr-o?

  • Justin Pinho Mar 30, 2009 @ 18:39

    If you can’t construct a sentence Dr M Barmann, you shouldn’t even bother.

  • Frank Ucker Jul 14, 2009 @ 10:14

    The answer is simple enough to understand although the Doc’s comment isn’t. Thanks for the tip.

  • bob borum Jan 13, 2011 @ 4:54

    I have a new freespire program and like it , but I do have issues , I can not seam to get my lexmark x3550 printer to work the soft ware disk will not run , an other issue is the yahoo messanger I get is yahoo beta I would like the stanard messager 10 , the last issue is I can not find the clock , thank you for your time bob

  • bob borum Jan 16, 2011 @ 3:57

    I still can not find the clock on freespire

  • Justin Nov 22, 2011 @ 9:59

    Thanks for this command. It was quite useful.

  • JN Jan 27, 2012 @ 14:41

    Exactly what i was looking for! thanks!

  • razor 3928 Feb 29, 2012 @ 23:58

    Please step away from the linux server. You will crash the box and hurt yourself. Call your grandkids they will help you.

    Thanks for the chuckle…..

  • vikram Aug 24, 2015 @ 7:44

    I tried so many option to find a sting under all directory/file in unix server….
    but many of them showing permission denied after each root path
    couls anyone tell me how to find it.

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