Monday, June 21, 2010

Search files in Linux

There are couples of tools used to find things in Linux.
  • find [where] [search patterns][actions]
    search patterns include:
    • -name fileNamePattern  match is case sensitive
    • -iname fileNamePattern  match is case insensitive
    • -size [+,-]size (b,c,w,k,M,G) + means great than -means less than
    • -user  username  username is the owner of the files
    • -mmin n  searching files which be modified n mins ago
    • -amin n  searching files which be accessed n mins ago
    • -and/-or/!  are the boolean operator which connect two or more patterns
    actions include:
    • -delete
    • exec command
    examples:
    • $ find / -name ‘*.c’ 2>/dev/null
      find any .c files under the root directory and discard the error message. 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears. So 2>/dev/null in this case means that while finding for the files, in case any error messages pop up simply send them to /dev/null i.e. simply discard all error messages.
    • $ find /etc -name ‘*.conf’ -exec ls -l {} \;
      find any .conf files under /etc directory and execute ls -l on the search result. {} indicate the search result \ indicate the end of the command.

No comments: