The gMan nixWiki

Because the mind is made of Teflon...

User Tools

Site Tools


general_find

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
general_find [2020/11/17 02:35] – [Expressions] gmangeneral_find [2024/01/19 00:06] (current) gman
Line 1: Line 1:
 ====== Find Stuff in Linux ====== ====== Find Stuff in Linux ======
  
-There are three basic commands to find stuff in Linux: +There are four basic commands to find stuff in Linux: 
   - whereis   - whereis
   - which   - which
Line 64: Line 64:
  
 <code>find /path/to/search/ -name searchTerm 2>/dev/null</code> <code>find /path/to/search/ -name searchTerm 2>/dev/null</code>
 +
 +https://www.cyberithub.com/find-command-in-linux/
 +
 +The -exec flag to find causes find to execute the given command once per file matched, and it will place the name of the file wherever you put the {} placeholder. The command must end with a semicolon, which has to be escaped from the shell, either as \; or as ";".
 +
 +Syntax to be used for find exec multiple commands:
 +
 +<code>find {PATH} [OPTIONS] -exec {COMMAND} {} ;</code>
 +
 +**The first argument** after the find command is the location you wish to search. Although you may specify a specific directory, you can use a metacharacter to serve as a substitute. The three metacharacters that work with this command include:
 +  * Period (.): Specifies the current and all nested folders.
 +  * Forward Slash (/): Specifies the entire filesystem.
 +  * Tilde (~): Specifies the active user's home directory.
 +
 +Find all files (not directories or links) of 1033 bytes in size that are not executable and which can be read (i.e., you can cat the file and read the contents):
 +
 +<code>find . -type f -size 1033c ! -executable -exec cat {} \;</code>
  
 ---- ----
Line 79: Line 96:
  
 ---- ----
- 
  
 ==== Expressions ==== ==== Expressions ====
Line 102: Line 118:
 Use the ''-type'' expression. The most common is "f" for file but the others are just as easy: Use the ''-type'' expression. The most common is "f" for file but the others are just as easy:
  
-^ Type  ^ Description + Type  ^ Description 
-block (buffered) special + f  regular file 
-| c | character (buffered) special +  | directory | 
-directory +|  l  | symbolic link | 
-| p | named pipe (FIFO) | +|   | character device 
-f | regular file | + b  block device 
-s | socket |+  | named pipe (FIFO) | 
 +  | socket |
  
 **Examples: ** **Examples: **
Line 124: Line 141:
 Use the ''-size'' expression. All units are rounded up (this is tricky!), for example, 1M = 1048576c but: Use the ''-size'' expression. All units are rounded up (this is tricky!), for example, 1M = 1048576c but:
   * ''-size -1M'' will find only empty files (because anything less that 1 is 0, therefore this will match only those files less than 1, which is 0M).   * ''-size -1M'' will find only empty files (because anything less that 1 is 0, therefore this will match only those files less than 1, which is 0M).
-  * ''-size -1048576c'' will find files from 0 to 1,048,575 bytes (i.e. 0 to on byte below 1M).+  * ''-size -1048576c'' will find files from 0 to 1,048,575 bytes (i.e. 0 to one byte below 1M).
  
-^ Suffix  ^ Description + Suffix  ^ Description 
-| b | blocks (512 bytes), default | +  | blocks (512 bytes), default | 
-| c | bytes | +  | bytes | 
-| k | kilobytes | +  | kilobytes | 
-| M | Megabytes | +  | Megabytes | 
-| G | Gigabytes |+  | Gigabytes |
  
-^ Prefix  ^ Description + Prefix  ^ Description 
-| + | greater than | +  | greater than | 
-| - | less than |+  | less than |
  
 **Example:**  **Example:** 
Line 157: Line 174:
  
 Use the ''-perm'' expression. There are three ways to specify perm mode:  Use the ''-perm'' expression. There are three ways to specify perm mode: 
-  - No prefix: find exact permissions +  - **No prefix:** find exact permissions 
-  - Prefix of '-': find "at least" permissions (not exact) +  - **Prefix of '-':** find "at least" permissions (not exact) 
-  - Prefix of '/': find permission in either owner, group, OR other+  - **Prefix of '/':** find permission in either owner, group, OR other
  
 **Examples:** **Examples:**
Line 178: Line 195:
 find . -type f -perm -220 find . -type f -perm -220
 # find files writeable by BOTH owner AND group (but no one else) # find files writeable by BOTH owner AND group (but no one else)
 +
 +find / -perm /4000 -type f -exec ls -ld {} \; 2>/dev/null
 +# This will find files with the SUID bit set.
 +# -l long listing format
 +# -d list directory names, not contents
 </code> </code>
  
Line 188: Line 210:
 Three timestamps for **DAYS** (24-hour periods):  Three timestamps for **DAYS** (24-hour periods): 
  
-^ Type  ^ Description + Type  ^ Description 
-| atime | shows when the file was last accessed (e.g., read) | + atime  | shows when the file was last accessed (e.g., read) | 
-| mtime | shows when the file contents were last modified | + mtime  | shows when the file contents were last modified | 
-| ctime | shows when metadata was last changed (including content modification) |+ ctime  | shows when metadata was last changed (including content modification) |
  
 Three timestamps for **MINUTES** (60-second periods) Three timestamps for **MINUTES** (60-second periods)
  
-^ Type  ^ Description + Type  ^ Description 
-| amin | file was last accessed (e.g., read) | + amin  | file was last accessed (e.g., read) | 
-| mmin | file contents were last modified | + mmin  | file contents were last modified | 
-| cmin | file metadata was last changed (including content modification) |+ cmin  | file metadata was last changed (including content modification) |
  
-^ Prefix  ^ Description + Prefix  ^ Description 
-| + | greater than | +  | greater than | 
-| - | less than |+  | less than |
  
 **Note:** Any fractional part of the time period is ignored. Therefore ''-atime +1'' will find files accessed TWO days ago or longer (because files accessed today, or within the last 23.99 hours, can be found with ''atime 0'').  **Note:** Any fractional part of the time period is ignored. Therefore ''-atime +1'' will find files accessed TWO days ago or longer (because files accessed today, or within the last 23.99 hours, can be found with ''atime 0''). 
Line 221: Line 243:
 </code> </code>
  
 +----
  
general_find.1605580521.txt.gz · Last modified: by gman