You can use ‘find’ to look for files based on their names, sizes, or other properties and execute commands against them.

Basic usage

find <path where you want to search> -name <some word or pattern>
# Add single quotes when using patterns
$ find . -name '*.pdf'
./input/test2.pdf
./input/test3.pdf

Options

  • -iname <some word>: case insensitive search.
  • -size <bytes or number and 'b', 'k', 'M', 'G',...>: search based on file size. You can add + or - for “more than” or “less than”.
    # find files with a size of more than 10MB
    find . -size +10M
    
    • Bear in mind that file size is rounded up to the next unit.
      # this will only find empty files
      find . -size -1M
      
    # show filesize
    find . -size +100M -exec ls -sh {} \;
    
  • -mmin <minutes>: files that were modified in the last specified minutes. You can use -mtime <days> as well.
  • -exec <command>: execute a command with every result. Use {} where you would put the filename and end the command with \;.
    # this will remove all txt files in current directory
    find . -name '*.txt' -exec rm {} \;
    
  • -type d: search only for folders.
  • -type f: search only for files.
  • -maxdepth <number>: limit the depth of your search. For example, -maxdepth 1 does not make a recursive search.
  • See man page for more options.
  • -path <path> -prune -o -print: exclude a directory.
    find . -name '*.txt' -path ./secret -prune -o -print