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.
47 # /{,*/{,*/}}lost+found \
50 # /opt/sql-ledger/{spool,templates,users,css} \
51 # /etc/apache/ssl.key \
52 # /etc/audisp/plugins.d \
55 # /etc/mail/spamassassin \
56 # /etc/polkit-1/rules.d \
60 # /var/lib/colord/.cache \
61 # /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
64 # /usr/share/polkit-1/rules.d \
66 # /var/{cache,chroot,db,run,snmp,spool} \
67 # /var/lib/{sshd,nfs,spamassassin,pulse,machines}) #NO TRAILING SLASHES!!!!
74 /tools) #NO TRAILING SLASHES!!!
76 if [ $# -lt 1 -o "$1" = "--help" ]; then
78 echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
80 echo 1>&2 ' grep -l <grep-commands> -- <file>'
81 echo 1>&2 ' will be executed for each *regular file* <file>'
82 echo 1>&2 ' ATTENTION! If you override the -l switch with a switch that makes grep'
83 echo 1>&2 ' output all individual matches rather than just the matching files,'
84 echo 1>&2 ' then DO NOT redirect output to a file that is in a directory that will be'
85 echo 1>&2 ' scanned, or you risk creating an endless loop that will cause your'
86 echo 1>&2 ' output file to grow till your disk is full.'
91 # suppress ugly debug output from shell
94 # construct find commands that match the prune_prefixes. Each prefix will be
95 # matched as -path <prefix> -or -path <prefix>/*
96 # so that the directory itself and all subdirectories are matched.
98 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
102 y[${#y[@]}]="${prune_prefixes[$i]}"
105 y[${#y[@]}]="${prune_prefixes[$i]}/*"
109 cmd_pre=(-type f -exec grep -l)
112 # In the following find command, the part
113 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
114 # is responsible for preventing the files that match prune_prefixes from
115 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
116 # -prune has no effect and is always false when -depth is used (which someone
117 # might do in the future).
118 # The -true before "$@" ensures that -depth can be passed as 1st parameter
119 # of $cmd_pre (should someone change it in the future).
120 find "${fs_to_scan[@]}" -xdev $NOLEAF \
121 -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
122 -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)