| tero.co.uk | |||||
|
Quick links:
|
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/bash
#This script replaces occurrences of one string with another in a bunch of files.
#Use like this: ./replace.sh oldthing newthing `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
echo $file: replacing $nummatches occurrences
sed -e "s+$replacethis+$withthis+g" $file > $TEMPFILE
mv $TEMPFILE $file
fi
done
|
||||