-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.txt
68 lines (55 loc) · 3.34 KB
/
find.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Basically find takes 1 \e[1mor more\e[0m directories, tests (name, mtime, type, etc) and actions.
# Most of the below examples are just tests.
# With \e[38;5;208m-not\e[0m or \e[38;5;208m!\e[0m you can negate a test.
# list all files modified within the last day (+1 for before last day, 1 for exactly 24 hours ago)
$ find . -mtime -1
# \e[38;5;208m-mmin\e[0m for minutes ago/before/etc
# if you want long format (think ls -la) add \e[38;5;208m-ls\e[0m:
$ find . -mtime -1 -ls
# then again, -ls has a retarded flag by default stuffing inodes or something in front, you can also use \e[38;5;208mexec\e[0m:
$ find $args \e[32;1m-exec <command> {} \;\e[0m
# \e[38;5;208m{}\e[0m is a placeholder for the filename/path
# \e[38;5;208m\;\e[0m tells find to run \e[38;5;208mcommand\e[0m on \e[1meach found file\e[0m
# So:
$ find . -mtime -1 -exec ls -lAd {} \;
# if there's not too many files, you can use \e[38;5;208m+\e[0m instead of \e[38;5;208m\;\e[0m which runs the command once on \e[1mall files\e[0m (assuming the command
# supports that), which also helps with the columns lining up
# Instead of \e[38;5;208m-exec rm {} +\e[0m you can use \e[38;5;208m-delete\e[0m
$ find ./filesToRemove -type f -delete
# which is more efficient, and (one of?) the fastest way(s) to remove a million files.
# If \e[38;5;208m-exec ls\e[0m has issues with spaces in names (I think that happened to me once?), break out \e[38;5;208mxargs\e[0m telling both \e[1mfind\e[0m and
# \e[1mxargs\e[0m to use a \e[38;5;201mnull delimiter\e[0m for newlines:
$ find . -mtime -1 \e[32;1m-print0\e[0m | xargs \e[32;1m-0\e[0m ls -lAd
# find files owned by specific user or group
$ find . -user joe -group developers
# (yeah, try and remember THAT)
# find files with extension foo, note the wildcard AND the quotes because why make things simple (though the quotes seems to
# not always be required, sometimes they ARE, I dunno, just use 'm)
$ find . -name '*foo'
# find files except not in /that/ directory (note, again, the quotes and slashes and wildcard..)
$ find . -name '*php' -not -path './vendor/*'
# although the second slash is technically optional. possibly. depending, probably.
# find files matching this or that, except not in /that/ directory
$ find . \( -name '*php' -o -name '*json' \) -not -path './vendor/*'
# the -o is an OR, and the escaped () group the name matches to logically separate both from the directory filter
#=- find types -=#
-type f \e[36m# regular files\e[0m
-type d \e[36m# directory\e[0m
-type l \e[36m# links. pipe to xargs realpath to get the sources, or add \e[38;5;208m-ls\e[36m and ignore the extra cruft\e[0m
-xtype l \e[36m# broken links\e[0m
-type p \e[36m# named pipe\e[0m
-type s \e[36m# socket\e[0m
-type c \e[36m# character (unbuffered) special\e[0m
-type b \e[36m# block (buffered) special\e[0m
# simple function searching current and sub-directories for case-insensitive substring match
f ()
{
if [[ "$1" = '' ]]; then
echo ' Finds files in current (sub)directory containing given substring. Usage: $ f <substring>';
echo " Implemented as: $ find . -iname \"*\$1*\" -not -path './.git/*' -not -path './vendor/*' | cut -b 3-;";
echo " It works mostly.";
return;
fi;
find . -iname "*$1*" -not -path './.git/*' -not -path './vendor/*' | cut -b 3-
}
There are modern `find` alternatives, I'll list 'm when I ..find 'm.