Linux Command “find” 101

It’s been a month i move back to Ubuntu as my primary OS. And i would like to write one of the most use command in Linux: “find”. This command is like a magic to search anything i want on my hard drive. I can find a text inside a file, or filter it for certain extension. I can search the files with latest modified time and also the file size.

List to use Linux command “find” that mostly used by me:

  1. Find all PHP Files in a directory:

    find . -type f -name "*.php"

  2. Find a file with a name:

    find /etc/ -type f -name "php.ini"

  3. Find file with wrong permission (i use this because suPHP need file permission 755)

    find . -type f -perm 0777 
    #OR
    find . -type f -perm -o=w
    #And to change them to correct permission, i use this command:
    find . -type f -perm 0777 -print -exec chmod 755 {} \;
    find . -type d -perm 777 -print -exec chmod 755 {} \;
    

  4. Find unnecessary files and remove them. (Use this with extreme cautious):

    find . -type f -name "*.cache" -exec rm -f {} \;
    #to remove empty files:
    find /tmp -type f -empty -exec rm -f {} \;
    

  5. Find files by modified date:

    #this will find all PHP files modified yesterday
    find . -type f -name "*.php" -mtime -1
    #this will find all PHP files modified for the last 2 hours
    find . -type f -name "*.php" -mmin -120
    

  6. Find files by the file size

    #Find zip files with more than 2MB
    find . -name "*.zip" -size +2M
    

  7. Find a text in all files

    #this will find "die_dump" in all php files
    find . -name "*.php" -exec grep -in "die_dump" {} \; -print
    

Comments

  1. Felicitas says:

    In every port, you will be amused by the different activities such
    as partying in the different dockside establishments.
    Plug-in hybrid electric cars (PHEV) use a mix of traditional
    fuels and electrical energy. Take cardboard boxes ashore to the dumpster as soon as you’re done.

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.