Languages/shell/find

From UIT
Revision as of 09:35, 10 October 2013 by Pim (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

The find command

This command can be used to find files (or directories) recursively.

Finding all files and directories starting from the current directory

find .
Will display all files and directories below the current directory.

Finding all files and directories in a directory

find /etc
Will display all files and directories below /etc.

Finding all files and directories in many directory

find /etc /home
Will display all files and directories below /etc and /home (in that order).

Finding only files in a directory

find /etc -type f
Will display all files (no directories) below /etc.

Finding files with a suffix

find /etc -type f -name "*.conf" Will find all files with name ending by .conf below /etc.

  • Warning: Don't forget the double quotes (") around the file name
  • Warning: find is case sensitive, if you also want files with "*.Conf", use the -iname

Finding files containing a string

find . -type f -exec grep -q "heivs/error.h" {} \; -print
Will find all files containing heivs/error.h

  • The -exec command will execute grep on every file found by file, and evaluates it's return value.
  • The -print command is necessary to display the file that match the -exec command.
  • The {} is the filename given by find to grep

Finding files with any of 2 suffixes

find . -type f -name "*.h" -o -name "*.c"

  • The -o parameter means OR.
Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox