OSDN Git Service

Regular updates
[twpd/master.git] / find.md
1 ---
2 title: Find
3 category: CLI
4 layout: 2017/sheet
5 updated: 2019-11-17
6 ---
7
8 ### Usage
9 {: .-prime}
10
11 ```bash
12 find <path> <conditions> <actions>
13 ```
14
15 ### Conditions
16
17 ```bash
18 -name "*.c"
19 ```
20
21 ```bash
22 -user jonathan
23 -nouser
24 ```
25
26 ```bash
27 -type f            # File
28 -type d            # Directory
29 -type l            # Symlink
30 ```
31
32 ```bash
33 -depth 2           # At least 3 levels deep
34 -regex PATTERN
35 ```
36
37 ```bash
38 -size 8            # Exactly 8 512-bit blocks 
39 -size -128c        # Smaller than 128 bytes
40 -size 1440k        # Exactly 1440KiB
41 -size +10M         # Larger than 10MiB
42 -size +2G          # Larger than 2GiB
43 ```
44
45 ```bash
46 -newer   file.txt
47 -newerm  file.txt        # modified newer than file.txt
48 -newerX  file.txt        # [c]hange, [m]odified, [B]create
49 -newerXt "1 hour ago"    # [t]imestamp
50 ```
51
52 ### Access time conditions
53
54 ```bash
55 -atime 0           # Last accessed between now and 24 hours ago
56 -atime +0          # Accessed more than 24 hours ago
57 -atime 1           # Accessed between 24 and 48 hours ago
58 -atime +1          # Accessed more than 48 hours ago
59 -atime -1          # Accessed less than 24 hours ago (same a 0)
60 -ctime -6h30m      # File status changed within the last 6 hours and 30 minutes
61 -mtime +1w         # Last modified more than 1 week ago
62 ```
63
64 These conditions only work in MacOS and BSD-like systems (no GNU/Linux support).
65
66 ### Condition flow
67
68 ```bash
69 \! -name "*.c"
70 \( x -or y \)
71 ```
72
73 ### Actions
74
75 ```bash
76 -exec rm {} \;
77 -print
78 -delete
79 ```
80
81 ### Examples
82
83 ```bash
84 find . -name '*.jpg'
85 find . -name '*.jpg' -exec rm {} \;
86 ```
87
88 ```bash
89 find . -newerBt "24 hours ago"
90 ```
91
92 ```bash
93 find . -type f -mtime +29 # find files modified more than 30 days ago
94 ```