3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 # Many updates/tweaks --SY.
10 # The following list should contain the mount points of all
11 # filesystems that are to be scanned as a space-separated list within
12 # parentheses. / will usually be in this list and if you have /usr on
13 # a separate partition, it will also be in this list. Mount points
14 # whose filesystems are special, such as procfs or sysfs must not be
15 # in this list. While a simple find on those special filesystems
16 # should be harmless, operations such as "-exec grep something" are
17 # NOT SAFE and may have HARMFUL SIDE-EFFECTS, especially when
21 # fs_to_scan=(/ /opt /usr /var)
25 ## NOTE: if any of the directories listed in fs_to_scan contain
26 ## non-UNIX filesystems (MS-DOS, CD-ROM etc) you need to set $NOLEAF
27 ## here to `-noleaf'. But only do so if you really need to as it
28 ## comes with a significant slow down on the find.
32 # Files with a path prefix found in the following list are ignored. As
33 # the main function of this script is to help you find files that
34 # contain hardwired paths to /tools or other unwanted references to
35 # your build system, you will usually prune any directories that don't
36 # contain files of interest, such as /tools (whose files naturally
37 # refer to /tools) and your package users' home directories (which may
38 # also test positive if you have unpacked and configured sources lying
41 # NOTE: If a directory you want to prune is on a separate filesystem
42 # (separate from those listed in fs_to_scan) you don't need to list it
43 # here because of the -xdev option used in the find command.
46 ## My /usr/src is NOT included here because it is on a separate
49 # /{,*/{,*/}}lost+found \
50 # /etc/apache/ssl.key \
51 # /etc/audisp/plugins.d \
54 # /etc/mail/{auth,spamassassin} \
56 # /etc/polkit-1/rules.d \
61 # /opt/sql-ledger/{spool,templates,users,css} \
63 # /usr/lib/deprecated \
65 # /usr/share/polkit-1/rules.d \
66 # /var/{cache,chroot,db,run,snmp,spool} \
67 # /var/lib/{bluetooth,xdm} \
68 # /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
69 # /var/lib/{sshd,nfs,spamassassin,pulse,machines} \
70 # /var/lib/colord/.cache \
72 # /var/tmp) #NO TRAILING SLASHES!!!!
79 /tools) #NO TRAILING SLASHES!!!
81 if [ $# -lt 1 -o "$1" = "--help" ]; then
83 echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
85 echo 1>&2 ' grep -l <grep-commands> -- <file>'
86 echo 1>&2 ' will be executed for each *regular file* <file>'
87 echo 1>&2 ' ATTENTION! If you override the -l switch with a switch that makes grep'
88 echo 1>&2 ' output all individual matches rather than just the matching files,'
89 echo 1>&2 ' then DO NOT redirect output to a file that is in a directory that will be'
90 echo 1>&2 ' scanned, or you risk creating an endless loop that will cause your'
91 echo 1>&2 ' output file to grow till your disk is full.'
96 # suppress ugly debug output from shell
99 # construct find commands that match the prune_prefixes. Each prefix will be
100 # matched as -path <prefix> -or -path <prefix>/*
101 # so that the directory itself and all subdirectories are matched.
103 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
107 y[${#y[@]}]="${prune_prefixes[$i]}"
110 y[${#y[@]}]="${prune_prefixes[$i]}/*"
114 cmd_pre=(-type f -exec grep -l)
117 ## Let root override the prune_prefixes
118 if [ $(id -u) -eq 0 ]; then
119 prune_prefixes=(/home /root /tools)
122 # In the following find command, the part
123 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
124 # is responsible for preventing the files that match prune_prefixes from
125 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
126 # -prune has no effect and is always false when -depth is used (which someone
127 # might do in the future).
128 # The -true before "$@" ensures that -depth can be passed as 1st parameter
129 # of $cmd_pre (should someone change it in the future).
130 find "${fs_to_scan[@]}" -xdev $NOLEAF \
131 -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
132 -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)