Created
June 8, 2014 15:52
-
-
Save dekarrin/327fcc37d9da9b778d32 to your computer and use it in GitHub Desktop.
Recursively count lines of files in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################## | |
# NAME # | |
# dwc - 'Directory Word Count' - print and format newline, word, and # | |
# byte counts for each file. # | |
# # | |
# SYNOPSIS # | |
# dwc [OPTION]... [FILE]... # | |
# dwc [OPTION]... --files0-from=F # | |
# # | |
# DESCRIPTION # | |
# Wraps wc and provides additional options for recursive counting and # | |
# formatting of output. The wc command is invoked with all options # | |
# and arguments that apply to it in each directory that is desired, # | |
# and the output is piped through various other programs to produce # | |
# the desired format. All options and arguments that apply to wc also # | |
# apply to dwc. See the manpage for wc(1) for additional information. # | |
# # | |
# AUTHOR # | |
# Written by Rebecca 'TF' Nelson # | |
############################################################################## | |
get_total () | |
{ | |
((t1 = 0)) | |
((t2 = 0)) | |
((t3 = 0)) | |
((t4 = 0)) | |
t1set= | |
t2set= | |
t3set= | |
t4set= | |
for file in * | |
do | |
if [ -d "$file" ] | |
then | |
cd $file | |
sub_tot=$(get_total) | |
cd .. | |
if [ -n "$sub_tot" ] | |
then | |
set $sub_tot | |
if [ -n "$1" ] | |
then | |
t1set=1 | |
((t1 += $1)) | |
fi | |
if [ -n "$2" ] | |
then | |
t2set=1 | |
((t2 += $2)) | |
fi | |
if [ -n "$3" ] | |
then | |
t3set=1 | |
((t3 += $3)) | |
fi | |
if [ -n "$4" ] | |
then | |
t4set=1 | |
((t4 += $4)) | |
fi | |
fi | |
fi | |
done | |
files= | |
move_files= | |
for f in *.$EXT | |
do | |
dest=$(echo $f | sed 's/ /_/g') | |
if [[ "$dest" != "$f" ]] | |
then | |
cp "$f" $dest | |
move_files="$move_files $dest" | |
files="$files $dest" | |
else | |
files="$files $f" | |
fi | |
done | |
files=$(echo $files | sed 's/^ //') | |
if [ "$files" != '*'.$EXT ] | |
then | |
WC_ARGS="$WC_ARGS $files" | |
set $(wc $WC_ARGS | sed 's/\t/ /g' | grep -o '\([0-9]\+ *\)\+' | tail -1) | |
if [ -n "$1" ] | |
then | |
t1set=1 | |
((t1 += $1)) | |
fi | |
if [ -n "$2" ] | |
then | |
t2set=1 | |
((t2 += $2)) | |
fi | |
if [ -n "$3" ] | |
then | |
t3set=1 | |
((t3 += $3)) | |
fi | |
if [ -n "$4" ] | |
then | |
t4set=1 | |
((t4 += $4)) | |
fi | |
fi | |
for f in $move_files | |
do | |
rm $f | |
done | |
output= | |
if [ -n "$t1set" ] | |
then | |
output="$output $t1" | |
fi | |
if [ -n "$t2set" ] | |
then | |
output="$output $t2" | |
fi | |
if [ -n "$t3set" ] | |
then | |
output="$output $t3" | |
fi | |
if [ -n "$t4set" ] | |
then | |
output="$output $t4" | |
fi | |
output=$(echo $output | sed 's/ +/ /g' | sed 's/^ //' | sed 's/ $//') | |
echo $output | |
} | |
DIR_NAME=$1 | |
EXT=$2 | |
shift 2 | |
WC_ARGS= | |
while [ -n "$1" ] | |
do | |
WC_ARGS="$WC_ARGS $1" | |
shift | |
done | |
cd $DIR_NAME | |
get_total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment