| tero.co.uk | |
|
|
Shell ScriptsBelow are a couple of UNIX shell scripts that can be useful.
Listing Big Files
#!/bin/sh
#This script will find files in the given directory with more than the given kilobytes.
#It defaults to the current directory and 1000 kb.
#Default vaules
mydir="."
mysize=1000
#Override with the arguments
if test -n "$1"; then mydir=$1; fi;
if test -n "$2"; then mysize=$2; fi;
#Say what we will do
echo Finding files in $mydir greater than $mysize kb
#Look for the files
files=`find ${mydir} -size +${mysize}k | sed -e "s/ /?/g"`
#List the files or say that no files were found
if test -n "$files"; then ls -l $files;
else echo "No files found"; fi;
Replacing Text
#!/bin/sh
#Paul 29/4/2004
#This script replaces occurrences of one string with another in a bunch of files.
#Use like this: ./replace.sh NewCategory Category `find . -name \*php`
#the temporary file to use
TEMPFILE=/tmp/.replaceZZ.tmp
#print a usage message
if test $# -lt 3; then
echo Usage $0 replace_this with_this files
exit 1
fi
#get the replacement and text to replace
replacethis=$1 #the first argument
withthis=$2 #the second argument
shift 2
#loop through the files
for file; do
nummatches=`grep "$replacethis" $file | wc -l | sed -e "s/ //g"`
if test $nummatches -eq 0; then
echo $file: 0 occurrences
else
#We move first then sed, because doing it the other way around made the files give
#permission denied when viewed as web pages (???)
mv $file $TEMPFILE
sed -e "s~$replacethis~$withthis~g" $TEMPFILE > $file
if test -s $file; then
echo $file: replacing $nummatches occurrences
rm $TEMPFILE
else
echo $file: not changed as the expression made the file blank!
mv $TEMPFILE $file
fi
fi
done
|