0

How can I write a shell script to go through all the sub-directories below a given directory and in each of those sub-directories search the file called "copyright" for strings like "license" or "GPL" and print the lines with those sub-strings. After running this I should be able to tell what the results were for each copyright file that was found.

2
  • 1
    find command with grep can do but I didn't fully understand the last point. Commented Jun 25, 2013 at 14:56
  • If what you want are totals or a sort of summary report try awk, wc and friends to pull out the information you need. As I note below this may be a job for perl and its format tools ;-) Commented Jun 25, 2013 at 16:19

3 Answers 3

1

Use find with -exec:

find . -name copyright -exec grep -H -e "license" -e "GPL" '{}' + >> results
Sign up to request clarification or add additional context in comments.

2 Comments

Works great Anubhava. How do I make "license" and "GPL" case insensitive?
Just use -i switch in grep like: find . -name copyright -exec grep -H -i -e "license" -e "GPL" '{}' + >> results
1

Command

Using grep and Bash's ** (globstar, for deep expansion):

shopt -s globstar; # enable ** support
grep -i -E 'licence|GPL' **/copyright

Explanation

  • globstar:

If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.

  • -E, --extended-regexp:

Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

References

3 Comments

Thanks a lot for the responses. I will try these and let you know.
The grep works great Edouard but my system does not have globstar.
globstar is available since Bash v4, but off by default. Hence the shopt -s globstar at the beginning. Otherwise use the solution given by @anubhava
0

Here's a sort of embarrassing script I use for producing a "License Report" for a FreeBSD host. I made it faster by switching out xargs for anubhava's -exec. Thanks!

#!/bin/sh
#
# pkg_license_check
#
# TODO: make this report on unlicensed packages.
#
LICENSES='MIT GPL ART BSD'

for LICENSE in $LICENSES
do

 cd /usr/local/share/licenses
 num=`find . -name LICENSE -exec grep -e "$LICENSE" '{}' + | wc -l`
 echo "Total of $num $LICENSE  Licensed packages as follows:"
 find . -name LICENSE -exec grep -e "$LICENSE" -e '{}' + | awk -F":" '{sub("^\.\/", "", $1); print "\n" $1 "\n" $2 $3}'
 echo -e "\n\n------------------------------------------------------------------------- \n\n"

done

A project for one day RSN: rewrite in perl and make "POSIX cross platform" for different packaging systems using plugins. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.