Archive for the ‘Shell script’ Category

Recursive touch

Saturday, June 30th, 2007

“touch” is a unix command used to change the time of a file or directory, touch a directory will not touch the files inside.

To touch recursively, it’s simple with the help of “find” and “xargs”:

e.g.

find . | xargs touch

will touch all files and directories recursively under current directory.

Since directory or filename may contain spaces, the shell script need some additional parameteres:

find "$1" -type f -print0 | xargs -0 touch

Popularity: 11% [?]

Shell script to replace strings in all files under directory recursively

Saturday, June 30th, 2007

In one of my project I need to replace some kind of strings in hundreds if file,  the code paste here is for specific usage, but it could be easily changed to do something else.

It will scan all *.xml files in current directory and in sub directories, replace the value of two attributes to a big value.

#!/bin/bash 
# Get List of Files to Be Replaced
file_list=`find . -name ”*.xml” -type f` 
# Define Target String to Be Replaced & Destination String for Substitution
echo ” Replacing *.xml file’s timeout to a big value ” 
# Perform Substitution
for fn in $file_list
do 
if ( test $fn != ./`basename $0` ) 
then
ffnt=”$fn.temp” 
echo ”Processing $fn …… ”  
sed ’s/responseReadTimeout=”[0-9][0-9]*”/responseReadTimeout=”999999″/g’  $fn > $ffnt 
sed ’s/socketReadTimeout=”[0-9][0-9]*”/socketReadTimeout=”999999″/g’ $ffnt > $fn 
rm $ffnt
echo ”  Done ” 
fi
done

Popularity: 15% [?]

Close
E-mail It
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.