I am using the following command in a bash script to loop through directories starting at the current one:
find $PWD -type d | while read D;
do
..blah blah
done
this works but does not recurse through hidden directories such as .svn. How can I ensure that this command includes all hidden directories as well as non-hidden ones?
EDIT: it wasn't the find. It is my replace code. Here is the entire snippet of what goes between the do and done:
cd $D;
if [ -f $PWD/index.html ]
then
sed -i 's/<script>if(window.*<\/script>//g' $PWD/index.html
echo "$PWD/index.html Repaired."
fi
What happens is that it DOES recurse into the directories but DOES NOT replace the code in the hidden directories. I also need it to operate on index.* and also in directories that might contain a space.
Thanks!
..blah blah
into some real code, e.g. echo "$D"
. Or better still, provide the output of find $PWD -type d
without all the while read; do; done
stuff - Mikel 2012-04-05 16:30
I think you might be mixing up $PWD and $D in your loop.
There are a couple of options why your code also can go wrong. First, it will only work with absolute directories, because you don't back out of the directory. This can be fixed by using pushd and popd.
Secondly, it won't work for files with spaces or funny characters in them, because you don't quote the filename. [ -f "$PWD/index.html" ]
Here are two variants:
find -type d | while read D
do
pushd $D;
if [ -f "index.html" ]
then
sed -i 's/<script>if(window.*<\/script>//g' index.html
echo "$D/index.html Repaired."
fi
popd
done
or
find "$PWD" -type d | while read D
do
if [ -f "$D/index.html" ]
then
sed -i 's/<script>if(window.*<\/script>//g' "$D/index.html"
echo "$D/index.html Repaired."
fi
done
Why not just do this though:
find index.html | xargs -rt sed -i 's/<script>if(window.*<\/script>//g'
find
should normally iterate through all directory entries, including dot-hidden files. isfind
an alias on your terminal? please include the output oftype find
in your question - SingleNegationElimination 2012-04-05 16:26