fbpx

How To Find Files In Linux?

Updated on May 14th, 2022
by Editorial Team

Introduction:

Find is a UNIX command line utility that walks a file hierarchy. You can use it to find and operate on files and directories. Files, folders, names, creation dates, modifications dates, owners and permissions can be searched using this command.

You can further combine it with ‘-exec’ option to perform various actions on the results found by the find command.

In this article we will learn how to find files in linux using the Find command.

How Find command works?

Find locates files on your computer. According to the rules of precedence, it evaluates each expression, from left to right, within each directory tree specified by the given paths. In AND operations, or in OR operations, the outcome is “known” when the left side of the expression is TRUE or FALSE. Find then moves on to the next path until all paths have been explored.

The find command is the most fundamental and powerful tool for working with files on a Linux system. Files can be located by itself or in conjunction with other programs.

Syntax:

find [options] [path…] [expression]

or

$ find [where to start searching from]

[expression determines what to find] [-options] [what to find]

  • The symbolic link treatment, debugging options, and optimization strategy are all controlled by the options property.
  • The path… property specifies the directory or directories from which find will look for files.
  • Options, search patterns, and actions are separated by operators in the expression attribute.

Options:

-exec CMD: The file being searched that meets the above criteria and returns 0 for a successful command execution exit status.

-ok CMD: Similar to -exec, but the user is prompted beforehand.

-inum N: Look for files with the number ‘N’ in the inode.

-links N: Find files that have ‘N’ links.

-name demo: Look for files with the name ‘demo’ in the filename.

-newer file: Look for files that have been updated or created since ‘file.’

-perm octal: If the permission is ‘octal,’ look for the file.

-print: Print the path names of the files identified using the remaining criteria.

-empty: Look for files and folders that are empty.

-size +N/-N: Look for files with ‘N’ blocks; ‘N’ followed by ‘c’ can be used to measure character size; ‘+N’ denotes size > ‘N’ blocks, while ‘-N’ means size ‘N’ blocks.

-user name: Look for files with the user name ‘name’ or the ID ‘name’.

(expr ) : If ‘expr’ is true, then true; utilized for grouping criteria with OR or AND.

! expr : True if ‘expr’ is false.

Expressions:

Options (which impact general operation rather than file processing and always return true), tests (which return a true or false value), and actions (which have side effects and return a true or false value) are all separated by operators in the expression. When the operator is absent, -and is inferred.

If the expression contains no additional actions then -prune, -print is applied to all files for which it is true.

For example, let us look a the following command as an example

find -L /var/www -name "*.js"

  • The option -L (options) tells the find command to follow symbolic links.
  • The /var/www (path…) specifies the directory that will be searched.
  • The (expression) -name "*.js tells find to search files ending with .js (JavaScript files).

How to find files in linux by name

Files can be found by name by using the find command. You can search for a file by its name by using the -name option.

find -type f -name functions.php

How To Find Files In Linux

use -iname instead of -name, to run a case-sensitive search

How to find files in linux by extension

You can search for files by extension just like you can search for files by name.

find -type f -name '*.php'

to find files without the given extension, use the following command

find -type f -not -name '*.php'

Find files by type

If you need to find a specific file type, such as a regular file, a directory, or a symlink, you might need to search for specific file types. Everything in Linux is a file.

Specify the type of file you are searching for by using the -type option and one of the following descriptors:

  • f: a regular file
  • d: directory
  • l: symbolic link
  • c: character devices
  • b: block devices
  • p: named pipe (FIFO)
  • s: socket

find public_html/wp-admin/ -type d

Find files by size

The -size parameter should be passed along with the size parameter to find files based on their size. The following suffixes can be used to specify the file size:

  • b: 512 -byte blocks (default)
  • c: bytes
  • w: two-bytes words
  • k: kilobytes
  • M: Megabytes
  • G: Gigabytes

find . -type f -size -1M

If you want to search for files with a greater than 1MB size use +

find . -type f -size +1M

You can even search for files within a size range

find . -type f -size +1M -size 3M

Find files by modification

The find command can also be used to look for files based on when they were last modified, accessed, or changed.

Use the plus and minus characters for “more than” and “less than,” just as you would when searching by size.

find . -mtime +30 -daystart

Find by permission

You can use the -perm option to search for files based on their permissions.

find public_html/wp-admin/css -perm /444

/ is used as the prefix, then at least on category must have at least the respective bit set for a file to match.

-prefix, then at least the specified bits must be set.

Find by owner

Use the -user and -group parameters to find files owned by a certain user or group.

find -user testc7963

Conclusion

We’ve demonstrated how to apply the find command with a variety of criteria and arguments.
This article should provide you with a basic understanding of how to find files on Linux computers.

One comment on “How To Find Files In Linux?”

Leave a Reply

Your email address will not be published.

chevron-down