Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»find Command in Linux (Search Files and Directories)

    find Command in Linux (Search Files and Directories)

    By RahulMarch 21, 20135 Mins ReadUpdated:July 20, 2021

    The “find” command is present in most Unix-like operating systems. It is used to locate and manipulate files and directories based on certain criteria, i.e., find files/directories based on date, size, groups, file types, and other criteria. It will look for files/directories in the current directory unless specified otherwise. The “find” is recursive, which means that it searches for files in the specified directory as well as the subdirectories of the specified directory. It can even be combined with some options to perform specific actions.

    Advertisement

    This post will focus on how to use the “find” command to locate and manipulate files and directories in your system. Moreover, we will also learn how to combine options with the “find” command to perform several actions.

    • Suggested Read: Grep Command in Linux with Examples

    How to locate files by name using the find command

    First, we will use the “find” command to search files by their names; This is the most common use of the “find” command.

    There are two approaches to search a file by its name using the find command:

    • In the current working directory (without providing any path)
    • In a custom directory

    How to search files by name in the current directory

    To find a file by name in the current directory, use the following command:

    find -name test-file.txt
    

    In this example, we are searching for a file named “test-file.txt” located in the current directory:

    Or:

    find -name test-file2.php
    

    How to search files in a custom directory

    As mentioned above, we can also use the find command to find a file by name in a custom directory. You can use the below-given command to do that:

    find /home/rahul/Documents -name test-file.txt
    

    The command given above searches for a file named “test-file.txt” in the “/home/rahul/Documents” directory.

    The “find” command is case-sensitive and will only look for a file with an exact name. You can use the “-iname” option to make it case insensitive:

    find /home/rahul/Documents -iname TEST-FILE.txt
    

    How to find files by their extension

    The “find” command can also be used to search for files with a specific extension. Just use the “*” along with the name of an extension to find all the files that have that extension:

    find /home/rahul/Documents -name '*.txt'
    

    The command is given above only looks for files with the “.txt” extension in the “/home/rahul/Documents” directory.

    The given-below command looks for all the files that have the “.jpg” extension.

    find /home/rahul/Documents -name '*.jpg'
    

    How to find files by their type

    The “find” command also gives us the option to search for a specific file type. To do that, combine the “find” with the “-type” option along with a flag to specify the file type.

    List of commonly used flags:

    • f: a regular file
    • c: character devices
    • d: directory
    • b: block devices
    • s: socket
    • l: symbolic link

    The following command is searching for directories in the /home/rahul/Documents directory using the d flag:

    find /home/rahul/Documents -type d
    

    And to search for files in the /home/rahul/Documents directory, use the f flag:

    find /home/rahul/Documents -type f
    

    In case you forget the name of a file, just execute the given command below:

    find /home/rahul/Documents -name "test*"
    

    The above-mentioned command will output all the files starting with “test”.

    To find the required file, just replace test with the file you want to find.

    How to search a file in multiple directories
    You can specify multiple directories along with the find command to find the file in those directories:

    find /home/rahul/Documents /home/rahul/Downloads -name test-file.txt -type f
    

    How to search a file by size

    You can also search for a file by specifying its size. You can use:

    • c: is for bytes
    • k: is for kilobytes
    • M: is for Megabytes
    • G: is for Gigabytes

    For example, to search files larger than a certain size, specify the size of the file along with the + operator as shown in the command below:

    find -size +50c
    

    Similarly, to search files smaller than a certain size, specify the size of the file along with the – operator:

    find -size -50c
    

    If you remember the exact size of the file, you can also search for it by specifying its size without any operator:

    find -size 71c
    

    Replace “71c” with the actual size of your file.

    How to search empty files

    To find all the empty files in a certain directory, use the following command:

    find /home/rahul/Documents -type f -empty
    

    How to search empty directories

    To find all the empty directories in a certain directory, use the following command:

    find /home/rahul/Documents -type d -empty
    

    How to find and remove files

    To find and remove a single file, use the following command:

    find -type f -name "test-Img2.jpg" -exec rm -f {} \;
    

    Remember to replace the file name.

    Conclusion

    The “find” command is used to locate and modify files and directories depending upon the specified parameters. It makes the files more accessible and helps the user find files and directories just by running a single command from the terminal.

    This command is regularly used by Linux users and is one of the most popular commands. It is very convenient and proves to be very helpful for the user.

    In this write-up, we touched on what the “find” command is and how we can use it to locate files and directories. Moreover, we also learned how we can combine the “find” command with other options to perform specific tasks.

    find find comand examples how to search content in files linux find command search file in linux using find search files in linux search in linux
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Sort Command in Linux with Practical Examples

    15 Practical Examples of Sed Command

    Sed Command in Linux with 15 Practical Examples

    lsusb Command in Linux (Display USB device Details)

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Backing Up Your Linux System with Rsync: A Step-by-Step Guide
    • Sort Command in Linux with Practical Examples
    • How to Install and Use Flask in Fedora 37/36
    • Sed Command in Linux with 15 Practical Examples
    • How to Print a List without Brackets in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.