POKE ME for any consultancy

Saturday, October 18, 2014

Locating Files by “find” Command

The find command is used to locate files on a Unix or Linux system.  find will search any set of directories you specify for files that match the supplied search criteria.  You can search for files by name, owner, group, type, permissions, date, and other criteria.  The search is recursive in that it will search all subdirectories too. 

 The syntax looks like this:  find where-to-look criteria what-to-do


You can also find files with various permissions set.  “‑perm /permissions” means to find files with any of the specified permissions on, “‑perm -permissions” means to find files with all of the specified permissions on, and “‑perm permissions” means to find files with exactly permissions.  Permissions can be specified either symbolically (preferred) or with an octal number.  
The following will locate files that are writable by “others” (including symlinks, which should be writable by all):          find . -perm -o=w



A common request is a way to find all the hard links to some file.  Using “ls ‑li file” will tell you how many hard links the file has, and the inode number.  You can locate all pathnames to this file with:
  find mount-point -xdev -inum inode-number


When specifying time with find options such as ‑mmin (minutes) or ‑mtime (24 hour periods, starting from now), you can specify a number “n” to mean exactly n, “‑n” to mean less than n, and “+n” to mean more than n.
Fractional 24-hour periods are truncated!  That means that “find ‑mtime +1” says to match files modified two or more days ago.
For example:
find . -mtime 0   # find files modified between now and 1 day ago
                  # (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago
                  # (i.e., within the past 24 hours, as before)
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between
                          # 6 and 9 minutes ago

No comments:

Post a Comment