Linux: Find Files in a Directory and Subdirectories

This post will guide you how to find files in a directory and its subdirectories under command line interface on Linux. How do I find a file in a specific directory in Linux.

Find Files in a Directory


If you want to find a file in your Linux system, you can use the find command to search in a given directory and its subdirectories.

For example, you want to find a file called fio in /root directory, you can type the following command:

# find /root -name fio

Outputs:

root@devops-osetc:~# find /root -name fio -print
/root/fio

If you want to find all files starting with the name “fio” in root directory, type the following command:

# find /root -name "fio.*"

Outputs:

root@devops-osetc:~# find /root -name "fio*"
/root/fio
/root/fio1

If you want to find all files starting with the name “fio” in given two directories, /root and /home/, type the following command:

# find /root /home -name "fio*"

Outputs:

root@devops-osetc:~# find /root /home -name "fio*"
/root/fio
/root/fio1
/home/devops/working/fio3
/home/devops/working/fio.c
/home/devops/working/fio
/home/devops/fio.txt
/home/devops/fio.cc

More Information about Find Command


If you want to get more help about find command, you can type the following command:

# find --help

Outputs:

root@osetc_test rar]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls]
-context CONTEXT

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.

Or

# man find

 

You might also like:

Sidebar



back to top