Linux cribsheet

  • grep files like '*.haystack' for 'needle'

    grep -iHn needle *.haystack

    • grep -i = case-insensitive

    • grep -H = print out the name of the file before the matching line

    • grep -n = print out the number of the matching line

  • Find files like '*.haystack' modified in the last five days and grep them for 'needle':

    find . -name '*.haystack' -mtime -5 -exec grep -iHn needle {} \;

    • the dot after find is where to start, i.e. the current working directory

    • the pattern '*.haystack' has to be quote-enclosed to stop the shell expanding the wildcard

    • -mtime -5 means 'modification time less than five days ago. For files more than five days old, use -mtime +5

    • -exec has to be semi-colon terminated to tell find where the command ends, but the semi-colon has to be escaped to stop the shell treating it as the end of find

  • Find files exeeding a certain size:

    find . -type f -size +1M

    • find files of size 1MB or greater