for i in `find . *.txt`; do perl -pi -e 's/\n/,/' $i; done; #### #!/usr/bin/perl use strict; use warnings; my $dir = "."; lista_dirs($dir, $dir); sub lista_dirs { my ($dir, $dirname) = @_; my (@dirs, @files); opendir DIR, "$dir"; my @dircontent = grep { /[^\.]/ } readdir(DIR); closedir DIR; @dirs = (); @files = (); foreach(@dircontent) { if(-d "$dir/".$_) { push @dirs, $_; } else { push @files, $_ if($_ =~ /\.txt$/); } } foreach my $d(@dirs) { lista_dirs("$dir/".$d, $d); } # IMPORTANT NOTE: if your files aren't too big, you can read everything at once as shown in the code below. # if note, you can open the input file for reading and a temp file for wirting. Read the file line by line, # replace \n with the , and write to the temp file. Close both files when you're done, and then 'rename' # the temp file to the input filename foreach my $file (@files) { $/ = ''; open FILE, "$dir/$file"; my $content = ; close FILE; $content =~ tr/\n/,/; open FILE, ">$dir/$file"; print FILE $content; close FILE; } return; }

update 2 After reading the other replies I realized I misread the question, and there was no need for recursion, so the solution came to be more complex than it needed to be. I was really in the need to get some sleep.