How to Find a File in Linux in All Directories complete Beginner’s

Vijay Kumar
6 min readJul 31, 2020

Find a file in windows is very easy just go in search bar and search file by name. it is only possible, if your computer has graphical interface in Linux.

Searching files in Windows are easy, Just go to the search box and type your query (name of the file), and hit enter, you will get the result of all files with the name. it happens only in the graphical user interface.

The GUI (Graphical User Interface) is not available in the Linux operating system every time, but the command line interface is available always.

To find a file by name, size, type, etc in the graphical interface is quite easy, And it is not really so easy in CLI (Command Line Interface).

In this article, I am going to cover “ How to find a file in Linux by using CLI mode.

When you read the full article you will be expert to find the files in Linux by using the command line interface.

And you will hate GUI for the same work.

The find is one of the most powerful commands in Linux in the system administrator’s point of view.
Find is a command line utility that allows you to search for files and directories in a directory hierarchy based on user-given expression and applies user-specified action on each matched file.

Course Content

Find Command Syntax in Linux

If you want to be an expert in using commands You must know what is the basic syntax of the same command.

Review the basic syntax of find command before go “How to use find command in Linux or how to find a file in Linux by using find command.

The basic usage of find utility expressions take the following form:

Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path…] [expression]

  • The options attribute [-H] [-L] [-P] [-Olevel] [-D debugopts] controls the treatment of the symbolic links, debugging options and optimization method.
  • The [path… ]an attribute defines the path where find will look for the file. If you want to search in complete system use only / or if you want to search file in etc directory path would be /etc/
  • The [expression] attribute is made up of options, search patterns and actions separated by operators.

Let’s take a look at the following example:

$find -L /etc/ -name passwd

  • Options is -L which allow to follow symbolic links.
  • Path: /etc/ which allow to search entire directory as per path.
  • Expression: -name which allow to search with given name

Find a File in Linux by Type

As you know, different files have different types, regular file, directory and more. you can use -type expression to specify file type.

If you want to search file by type then you can use the following expressions:

Specifying path is really important because if you don’t define path it will search only in current directory.

If you want to search from your current directory then syntax will as following

$find . -type d

From specific directory you must give the right path

$find /etc/ -type d -name passwd

Find a File in Linux by Name

You know the file name but forget where is it? you cand find it by using find command.

The most common use of find command is finding files by name. To find a file by its name by using -name expression followed by file name.

To search for a file called vijay.txt in the /home/ vijay directory you would use the following command:

If you are searching a case sensitive file, use -iname instead of -name. Example given below, look and compare the result.

Find a File in Linux by Extension

I want to have a look over all files with .log.gz extension files in my home directory. I can search file with extension.

The syntax would we following

Find a File in Linux by Size

You can use -size option to search file based on size. After -size you must define size criteria.

Following suffix can be used to define file size

To find the file with accurate size, you can give the size after using -size option. For example if you want to search file has 256 bytes exactly. then you can use following syntax

You can also search file with greater of less than a specified size. For example if you want to search file greater than 100 MB then use +100 MB and for less than 100 MB use -100 MB.

Find a File in Linux by Modification Date

The find command also allows searching files based on its last modification, access, and change time.

$find /etc/dovecot/conf.d -name “*.conf” -mtime 5

$find /home -mtime +30 -daystart

-amin n: File was last accessed n minutes ago.

-anewer: file File was last accessed more recently than file was modified.

File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match — atime +1, a file has to have been accessed at least two days ago.

-cmin n : File’s status was last changed n minutes ago.

-cnewer file: File’s status was last changed more recently than file was modified.

-ctime n: File’s status was last changed n*24 hours ago.

Find a File in Linux by Permissions

To filter the files based on the file / directory permissions, use the -perm option followed by permission number.

You can find all files with permissions of exactly 775 inside the /var/www/ html directory. Following syntax will work fine:

$find /var/www/html -perm 775

Two type prefix can be used — or /

When slash / is used as the prefix, then at least one category (user, group or others) must have at least the respective bits set for a file to match.

$find . -perm /444

If minus — is used as the prefix then for the file to match at least the specified bits must be set.

The following command will search for files that have read and write permission for the owner and group and are readable by other users:

find . -perm -664

Find a File in Linux by Owner

The user, who created file in Linux operating system called owner. Linux is a multiple user based operating system.

You are working as a system administrator and want to filter files based on ownership then this method is really very useful.

To search the files in linux system owned by user vijay

Here is a more advanced example, If you want to find all files owned by the user ftp-user and change the ownership of the matched files from ftp-user to www-data:

$find / -user ftp-user -type f -exec chown www-data {} \;

Find and Delete File in Linux

Find and Delete file is considered as two work, one is find the file another is delete the finding files.

If you are SysAdmin and want some delete .temp files then first command (find) will search all .temp files and then -delete option will delete all files.

$find /var/log/ -name ‘*.temp’ -delete

Conclusion

Find command is very interesting Linux, you can use it any linux distro like Ubuntu, RHEL, Centos, Kali Linux, Arch Linux, Free BSSD etc.

Expert command over find utility make you advanced user of linux, and find command will help you to work fast.

If you have any question please comment below.

Have a nice day

Cheers!

Originally published at https://www.cyberpratibha.com on July 31, 2020.

--

--