Searching for files in Linux

Problem

Searching for files or folders in Linux.

TL;DR

find /-name FileName

Solution

Everyone is probably familiar with the much-slower than you would expect Search tool in Windows. Well, Linux has a similar, but much more efficient, command-line tool called find

Its typical usage has the following format:

find /directory -argument1 value -argument2 value2

For example:

find /sysconfig -name ifcfg-eth0

This will return all instances of a file with the name ifcfg-eth0 in the directory sysconfig. In this instance, search results would look similar to the following:

# find / -name ifcfg-eth0
/etc/sysconfig/networking/profiles/default/ifcfg-eth0
/etc/sysconfig/networking/devices/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth0
Common search parameters:

Enter the value after each parameter.

-name filename Searches for files that have the name filename
-name filena* Wildcard search for files with name beginning in filena. Can be used to search for files with an extension, such as:

find / -name "*.php"

-type f Searches for entries of the designated type (in one letter), in this case, folders.
-mmin 10 Searches for files modified in the last 10 minutes; replace 10 minutes with a different value
-mtime 10 Same as above but in days
-cmin 10 Searches for files created in the last 10 minutes; replace 10 minutes with a different value
-ctime 10 Same as above but in days
-maxdepth 2 Search for files in folders at most 2 levels deeper than the current one; replace 2 with the desired folder depth.

File size parameter

Search by file size has the following format:

find /temp -size +10k

+ can be replaced with - or =.
searches for files larger than size
searches for files smaller than size
searches for files equal to size

10 is the size in units; units are b (bytes) or k (kilobytes). Default units are bytes, so if you do not enter a unit type, it will search for files with the size X bytes.


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 1482