In this tutorial, we will use some commands that help us to find files in Linux.
These commands are:

  • echo
  • ls
  • find
  • whereis
  • locate

List files using echo command

Many people are not aware of echo command potential. This will come very handy when listing files in a given directory where ls, find and other commands are not available in rescue mode. This is because echo is a built-in command and available in any basic Linux machine.

The echo command uses Bash shell expansion to list files in a given directory.

To list files in a given directory use below command.

[10:23:38] [VPS1-centos7] root:/bin # echo *test
 gr2fonttest   grub2-fstest   mysqltest   sctp_test

View files using ls command

The simplest way is to use ls command if you are looking inside a directory of files

[10:23:38] [VPS1-centos7] root:/bin # ls *test*
 gr2fonttest   grub2-fstest   gtester   gtester-report   mysqltest   sctp_test   snmptest   test   testgdbm   testparm   varnishtest

You can use several options with ls. The most used options are :

  • -l : list
  • -a : all files even hidden ones
  • -h : human readble
  • -r : reverse order while sorting
  • -t : sorts output based on the date of modification of the file
[10:28:07] [VPS1-centos7] root:/bin # ls -ltr *test*
 -rwxr-xr-x. 1 root root 30488 Jun 9 2014 testgdbm
 -rwxr-xr-x. 1 root root 32520 Jun 10 2014 sctp_test
 -rwxr-xr-x. 1 root root 147576 Mar 16 2015 varnishtest
 -rwxr-xr-x. 1 root root 18665 Nov 20 2015 gtester-report
 -rwxr-xr-x. 1 root root 24200 Nov 20 2015 gtester
 -rwxr-xr-x. 1 root root 1062624 Jan 5 2016 grub2-fstest
 -rwxr-xr-x. 1 root root 37288 Feb 16 2016 test
 -rwxr-xr-x. 1 root root 24208 Apr 5 2016 gr2fonttest
 -rwxr-xr-x. 1 root root 15248 May 12 2016 snmptest
 -rwxr-xr-x 1 root root 27616 Jul 26 12:38 testparm
 -rwxr-xr-x 1 root root 3614048 Sep 29 20:04 mysqltest

Using find command

You can use find if you are not sure where the file is located

Syntax : find PATH ARGS


[10:29:42] [VPS1-centos7] root:~ # find /var/ -name "test"
 /var/lib/mysql /var/lib/mysql/test
 [10:30:06] [VPS1-centos7] root:~ # find /var/ -name "*test*"
 /var/lib/mysql /var/lib/mysql/test

The command find has many options that help to quickly find the wanted file. The most popular options are:

  • -type : the type of file (example: f for file, d for directory)
  • -name : the file name (we can use regexp as above)
  • -iname : ignoring case (example: ‘Test’ will match when using -iname “test”)
[10:37:38] [VPS1-centos7] root:~ # find /var/ -name "*test*" -type f
[10:37:48] [VPS1-centos7] root:~ # find /var/ -name "*test*" -type d
 /var/lib/mysql /var/lib/mysql/test
 [10:37:51] [VPS1-centos7] root:~ # find /var/ -iname "*tEsT*" -type d
 /var/lib/mysql /var/lib/mysql/test

Using whereis command

whereis – locate the binary, source, and manual page files for a command

[10:40:13] [VPS1-centos7] root:~ # whereis test
 test: /usr/bin/test /usr/share/man/man1/test.1.gz

The most used options of whereis are :

  • -b : Search only for binaries.
  • -m : Search only for manuals.
  • -s : Search only for sources.
[10:40:16] [VPS1-centos7] root:~ # whereis -b test
 test: /usr/bin/test
[10:40:20] [VPS1-centos7] root:~ # whereis -s test
[10:40:24] [VPS1-centos7] root:~ # whereis -m test
 test: /usr/share/man/man1/test.1.gz

Using locate command

The locate command searches for the file pattern in the hole system. It uses an internal database to stock patterns.
To use locate install the package:

DEBIAN : # apt-get install locate
CENTOS : # yum install mlocate

Update the DB using :

# updatedb

Search the pattern:

[10:50:47] [VPS1-centos7] root:~ # locate kibana4
[10:51:11] [VPS1-centos7] root:~ # locate sbtest

You can use -c option to count the number of patterns matching your file in your system

[10:51:16] [VPS1-centos7] root:~ # locate -c sbtest

I hope that this blog helped you. Please visit our website for other interesting blogs and feel free to leave your feedbacks and thoughts. Till next time!

The following two tabs change content below.
I am a hands-on, competent Linux system engineer with 5 years’ experience. I have a strong performance background in wide variety of professional Linux system support including monitoring, configuration, troubleshooting and maintenance. I have worked on numerous projects from concept to completion. A specialist in LAMP platforms, I take pride in administrating Linux systems and regularly refresh my skills to ensure I keep up with ongoing developments and new technologies.