Recursive files in bash

I have files containing filenames pointing to other files. These files contain additional filenames that indicate additional files, etc. I need a bash script that follows each file recursively and writes to the file each affected file during the run.

file1:
   file2
   file3

file2:
   file4

file3:
   file5

      

file4 and file5 are empty. Result:

file1
file2
file4
file3
file5

      

+2


a source to share


1 answer


Define

function scan() {
  echo $1
  local f
  while read f ; do
    scan $f
  done < $1
}

      

Using:



scan file1 > log

      

Update: Accepted Dennis Williamson's comment and replaced cat $1 | while ... done

with better work while ... done < $1

.

+1


a source







All Articles