Skip to main content
edited tags; edited title
Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

Find Recursively find all files with the namenamed "file.txt" in folders and its sub-folders (and potentially sub-sub-folders) and execute a sed command

Source Link
dwuuuu
  • 71
  • 1
  • 1
  • 3

Find all files with the name "file.txt" in folders and its sub-folders (and potentially sub-sub-folders) and execute a sed command

The challenge of my task is that the file.txt can be in subfolder or in sub-sub folders. The general structure looks like this:

   folder
     |___subfolder
             |_____file.txt
     |
     |___subfolder
             |____subfolder
                      |_______file.txt
     etc

My previous code does not deal with the sub-sub folder and I am not sure how to add in this part (since some sub folder does not have any subsequent folders)

 for dir in ./*/ ; do
        if [ -d "$dir" ]; then 
           cd $d;
           for subdir in ./*/; do
               cd $subdir && $(sed command file.txt) && cd ..;
           done
           cd ..
        fi
done

I have also learned there's a one line method of using find, something like:

find . -name 'file.txt' | sed command file.txt

Apparently this one doesn't work as find only returns the dir to my file.txt