05. Links and find command

find command
  • find: searches a given part of the system directory tree for any file that matches some given criteria
  • Basic format:    find   start_dir   criteria_list
    • start_dir is the directory from which find will start the search
    • start_dir can be an absolute path or a relative path
    • If it is a relative path, start_dir is relative to the directory where find is run
    • If no start_dir is given, find starts the search from the current directory
    • criteria_list tells search what to look for
    • The criteria_list can be 1 or more criteria
  • find does a recursive search from the start directory, which means it will go down all subdirectories of each directory that it encounters
  • When ls is used to search for a file, the search only occurs at the directory that is given as the argument to ls
    • When find is use to search for a file, the search starts at the directory given and proceeds down all subdirectories, so it is a deeper search
  • find is a powerful command that can do work (take action) on the files it found that match the criteria. 
    • The action on the files can include removing the files, modifying the files, copying the files to another location, etc. 
    • Printing the location of the matched files is the default action of find
    • When printing the location of the matched files, find prints the path of the matched files with respect to the start_dir

Criteria for find
  • Files matching filename
    • -name  filename                               all files matching filename 
    • -name ‘name_with_wildcards’         all filenames matching  name_with_wildcards (single quotes are required)
  • Files of a certain file type
    • -type  d     all files that are directories
    • -type  f       all files that are regular files
    • -type  l       all files that are links (only symbolic link)
  • Files with a certain permission
    • -perm  octal_mode         all files with mode matching octal_mode (see File Permissions section for octal_mode)
  • Files that are empty
    • -empty             applies to regular files and directories

  • Files with a certain number of hard links
    • -links  +num      all files with number of hard links greater than num
    • -links  num        all files with number of hard links equal num
    • -links  –num      all files with number of hard links less than num, num is a number
  • Hard links to a file
    • -inum   inode_num       all files with a certain inode_num, Recall: inode_num is found by using  ls  –i
  • Symbolic links pointing to a certain file
    • -lname   path                             all links that contain a specific path
    • -lname ‘path_with_wildcards’    all links that contain paths that match the path_with_wildcards (single quotes are required)
    • Recall: when a symbolic link points to a file, it contains the path to that file. The –lname option looks at the path in the symbolic links for a match. If there is a match, it means the link points to that file.

  • find accepts one or more criteria
  • To use more than one criteria
    • AND
      • the file has to match all criteria listed
      • List the criteria separated by space
      • Example:   find   ~   -type d   -empty
    • OR:   
      •    the file has to match at least 1 criterion in the list
      •    List all criteria separated by   -o    (for or)
      •    Example:    find   ~   -type d   -o   -type  f

Comments