How to use find commands on a Linux OS

  • Filter
  • Time
  • Show
Clear All
new posts

    How to use find commands on a Linux OS

    Finding by Name:

    To find a file by name, type:

    #find -name "query"
    To find a file by name, but ignore the case of the query, type:

    #find -iname "query"
    Finding by Type:

    You can specify the type of files you want to find with the "-type" parameter. It works like this:

    #find -type type_descriptor query
    Some of the most common descriptors that you can use to specify the type of file are here:

    f: regular file
    d: directory
    l: symbolic link
    c: character devices
    b: block devices
    #find / -type c
    We can search for all files that end in ".conf" like this:

    #find / -type f -name "*.conf"
    Filtering by Time and Size:

    You can filter by size with the use of the "-size" parameter.
    We add a suffix on the end of our value that specifies how we are counting. These are some popular options:

    c: bytes
    k: Kilobytes
    M: Megabytes
    G: Gigabytes
    b: 512-byte blocks
    To find all files that are exactly 50 bytes, type:

    #find / -size 50c
    To find all files less than 50 bytes, we can use this form instead:

    #find / -size -50c
    To Find all files more than 700 Megabytes, we can use this command:

    #find / -size +700M

    Linux stores time data about access times, modification times, and change times.

    Access Time: Last time a file was read or written to.
    Modification Time: Last time the contents of the file were modified.
    Change Time: Last time the file's inode meta-data was changed.
    To find files that have a modification time of a day ago, type:

    #find / -mtime 1
    If we want files that were accessed in less than a day ago, we can type:

    #find / -atime -1
    Finding by Owner and Permissions

    You can also search for files by the file owner or group owner.
    You do this by using the "-user" and "-group" parameters respectively. Find a file that is owned by the "syslog" user by entering:
    # find / -user syslog
    Similarly, we can specify files owned by the "shadow" group by typing:

    find / -group shadow
    We can also search for files with specific permissions.
    If we want to match an exact set of permissions, we use this form:

    #find / -perm 644
    Filtering by Depth:

    You can specify the maximum depth of the search under the top-level search directory:

    #find -maxdepth num -name query
    To find "file1" only in the "level1" directories and above, you can specify a max depth of 2 (1 for the top-level directory, and 1 for the level1 directories):

    #find -maxdepth 2 -name file1
    You can also specify a minimum directory if you know that all of the files exist past a certain point under the current directory:

    #find -mindepth num -name query
    We can use this to find only the files at the end of the directory branches:

    #find -mindepth 4 -name file
    You can combine the min and max depth parameters to focus in on a narrow range:

    #find -mindepth 2 -maxdepth 3 -name file
    Executing and Combining Find Commands:

    You can execute an arbitrary helper command on everything that find matches by using the "-exec" parameter. This is called like this:

    #find find_parameters -exec command_and_params {} \;
    The "{}" is used as a placeholder for the files that find matches. The "\;" is used so that find knows where the command ends.
    For instance, we could find the files in the previous section that had "644" permissions and modify them to have "664" permissions:

    #find . -type f -perm 644 -exec chmod 664 {} \;

    Re: How to use find commands on a Linux OS

    Next time it would be easy for me to find something using OS shell. Thanks for the commands.


      Re: How to use find commands on a Linux OS

      Your welcome SalonJud feeling glad, that it is helping you somewhere....


        Re: How to use find commands on a Linux OS

        Thanks for the share. The find command of Linux is one of the much used and one of the most important commands in Linux systems. It helps one to search and locate directories and files based on certain conditions which are specified that match the arguments. Under variety of conditions, find command can be used and files can be searched by users,permissions,file type, groups, size, date etc. and many other criteria.


          How to use find command in Linux guide with example