Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include hidden directories with find?

Tags:

find

bash

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!

like image 565
Doug Wolfgram Avatar asked Oct 24 '22 03:10

Doug Wolfgram


1 Answers

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'
like image 152
j13r Avatar answered Oct 31 '22 16:10

j13r