How can I include hidden directories with find?

Go To StackoverFlow.com

3

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!

2012-04-05 16:22
by Doug Wolfgram
find should normally iterate through all directory entries, including dot-hidden files. is find an alias on your terminal? please include the output of type find in your question - SingleNegationElimination 2012-04-05 16:26
on my machine, the terminal is searching hidden directories by default - hjpotter92 2012-04-05 16:28
Please provide more information. For example, you should change ..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
This Server Fault question looks like it has what you need: http://serverfault.com/questions/139126/how-to-view-hidden-files-using-linux-find-comman - Logical Fallacy 2012-04-05 16:30
On my machine, the terminal is not searching hidden directories by default. However, if I type "find .*" I search hidden directories. This may cause extra problems for you though, as this will also search the ../ directory - theJollySin 2012-04-05 16:30
Sorry guys. My bad. the fault was not in the find but in the stuff between the do and done. I was trying to replace text (trojan) that got inserted. I made the edit above... Still a problem - Doug Wolfgram 2012-04-05 16:39


2

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'
2012-04-05 16:57
by j13r
This is great! Thanks. Can I use index.* in these examples? It doesn't seem to work.. - Doug Wolfgram 2012-04-05 20:28
you can use find -name 'index.*'. See the manpage of find for more options (e.g. -iname) - j13r 2012-04-05 20:47
Please expand what does -rt mean for xargs - kyb 2018-02-16 10:53
https://explainshell.com/explain?cmd=xargs+-r - j13r 2018-02-17 14:20
Ads