Pages

Friday 3 August 2012

Using find Command By Examples


The find command is a Linux command that is used to search for files in the Linux file system. In fact, it is a standard shell command that exists in most Linux/Unix variants.

    To search for all files that end with .sh, from the root directory down to all sub directories. Execute the find command as

    find / -name "*.sh" -print

    To search for all files that end with .sh, from both the dir1 and dir2 directories, and change the file permission mode to 755 for those matching files. Execute the find command as

    find dir1 dir2 -name "*.sh" -exec chmod 755 {} \;

    To search for all files that contains case-sensitive search term ABC, from the root directory down to all sub directories. Execute the find command as

    find / -name "*" -exec grep -H "ABC" {} \;

    To narrow down the scope of file searching as well as speed up the searching efficiency, rewrite the find command as

    find / -type f -name "*" -exec grep -H "ABC" {} \;

    to get find command search for regular file only instead of all file type that exists in the file system.

    Using the grep command by understanding grep command option switches

    grep -HIirls ABC *

            to search all files from the current directory that contain the search pattern ABC
            -H to print out filename
            -I to ignore binary file
            -i to search case-insensitive
            -r to search recursively from the directory to all sub directories
            -l to show file name once only for the file that containing the search pattern and exit from further search when the first match of pattern found in the file
            -s to suppress any errors message about non-existent or unreadable file

    Others forms of Linux command that serve the same purpose could be

    find / -type f -print | xargs grep -H "ABC" /dev/null
              or
    egrep -r "ABC" *

    To search for all files that are 1000KB (1MB) in file size, from the root directory down to all sub directories. Execute the find command as

    find / -size 1000k -print

    To search for all files that are greater than 1MB in file size, from the root directory down to all sub directories. Execute the find command as

    find / -size +1000k -print

    To search for all files that are between 1MB and 2MB (inclusive) in file size, from the root directory down to all sub directories. Execute the find command as

    find / -size +1000k -size -2000k -print

    Note! Use the +/- sign to indicate range, which apply to the time related quantity, as showing below.

    To search for all files that are own by user ID 501, from the root directory down to all sub directories. Execute the find command as

    find / -user 501 -print

    To search for all files that are created less than five minutes ago, from the root directory down to all sub directories. Execute the find command as

    find / -cmin -5 -print

    This form of find command is useful to find out the effects of system change after installing or updating Linux package.

        Linux file creation time vs Linux file modification time

        The Linux file creation refers to a file being created in the file system, changes of file permission, or changes of file ownership.

        Linux file modification time refers to a file last editing time.

        Take a look on the diagram below, which is a snapshot of $HOME/bin directory contents listing.

        Different of Linux file creation time and file modification time

        The ls -la command shows that wdf file modification or last edited time on Sep 29 11:25. Then ls -lc command shows that wdf file creation time on Oct 15 12:26 which is the time the file being extracted from a tarball.

    To search for all files that are created less than 2 x 24 hours ago, from the root directory down to all sub directories. Execute the find command as

    find / -ctime -2 -print

    To search for all files that are modified less than five minutes ago, from the root directory down to all sub directories. Execute the find command as

    find / -mmin -5 -print

    To search for all files that are modified less than 2 x 24 hours ago, from the root directory down to all sub directories. Execute the find command as

    find / -mtime -2 -print

    To search for all files that are accessed (such as execute the file, view the file using vi, cat, less, etc) less than five minutes ago, from the root directory down to all sub directories. Execute the find command as

    find / -amin -5 -print

    To search for all files that are accessed less than 2 x 24 hours ago, from the root directory down to all sub directories. Execute the find command as

    find / -atime -2 -print

    To search for all files that are own by group ID 22, from the root directory down to all sub directories, and change the group ID to 112 for those matching files. Execute the find command as

    find / -group 22 -exec chown :112 {} \;

    To search for all directories that are global writable, from the root directory down to all sub directories. Execute the find command as

    find / -perm -0002 -type d -print

    To search for all files that are not own by any user and group, from the root directory down to all sub directories. Execute the find command as

    find / -nouser -o -nogroup -print

    To search for all directories exists in the file system. Execute the find command as

    find / -type d -print

    The alternative form of commands are

    ls -ap | grep /
    find / -type d -printf "%P\n" apply -printf switch to show directories name only and discarding the leading path.

    To find the total disk space take up by all tarballs ended with .gz file extension

    find / -type f -name "*.gz" -exec du -k {} \; | awk '{sum+=$1} END {print sum"KB"}'

    To create CPIO archive backup of dir1 and dir2 directories. Execute the find command as

    find /f1 /f2 -depth -print | cpio -ocvB -O /f2f2.cpio

No comments:

Post a Comment

Twitter Bird Gadget