Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I some directories that contain a similarly named file eg (*Sample_name*.base.coverage.txt). And I would like to paste all of the *base.coverage.txt files together. I have something written, but its not quite right as I don't think I am actually pasting them together.

cd /g/bo/vir/analysis
find -type d  |
while read dir; do
if [ -f $dir/*.base.coverage.txt ]; then
    paste  $dir/*.base.coverage.txt >> paste_all_l30.p90.base.cov.norm

fi
done

I am also thinking of a different loop method, but I am unsure how to get the list of directories into an array.

for x in $dir; do cat /$x/*.base.coverage.txt;done > paste_all_l30.p90.base.cov.norm

Any help? thanks

share|improve this question
add comment

3 Answers 3

up vote 0 down vote accepted

try this:

find . -type f -name "*.base.coverage.txt" -exec sh -c 'paste {} paste_all_l30.p90.base.cov.norm > tmp ; mv tmp paste_all_l30.p90.base.cov.norm' \;
share|improve this answer
    
Hmm, snap! (almost) –  ams Apr 11 '12 at 12:41
    
Thanks, but this also just yields a file with one column. –  awall Apr 11 '12 at 12:43
    
Try to change cat to paste like I did in my answer. Is it what you're looking for? –  rush Apr 11 '12 at 12:53
    
find . -type f -name "*.fastx.allbest.l30.p90.base.cov.norm" -exec paste {} > paste_all_l30.p90.base.cov.norm\ ; find: missing argument to `-exec' –  awall Apr 11 '12 at 13:01
    
That's not going to work anyway because appending to the file is the wrong thing. –  ams Apr 11 '12 at 13:04
show 2 more comments

Try this:

find . -type f -name '*.base.coverage.txt' \
  | xargs paste > paste_all_l30.p90.base.cov.norm

This will work as long as there are not too many files. If there are a lot of file then this:

touch paste_all_l30.p90.base.cov.norm
for file in `find . -type f -name '*.base.coverage.txt'`; do
  paste paste_all_l30.p90.base.cov.norm $file > tmp
  mv tmp paste_all_l30.p90.base.cov.norm
done
share|improve this answer
    
The OP wants to paste, not cat –  Peter.O Apr 11 '12 at 12:48
    
Ok, I missed that. I've updated the answer –  ams Apr 11 '12 at 13:01
add comment

The easiest thing to recurse through a number of directories would be to write a short python script and use the os.path.walk function to recurse through the nested directories. For appending, you could use shutil.copyfileobj within the loop, checking filenames with a regex.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.