-3

I want to copy a single file from 10 different directories and append them in another directory.

closed as unclear what you're asking by user79743, Wildcard, Jeff Schaller, Thomas Dickey, cuonglm Mar 30 '16 at 1:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Show an example of what you want to do.. – heemayl Mar 29 '16 at 1:54
  • what do you mean by 'paste'? append them all, one after to the other, to a single file? – cas Mar 29 '16 at 1:54
  • how can it be done with single command line. i need to do it for like 100 directories after that. – PRATEEK BHATT Mar 29 '16 at 1:56
3

There are many ways to do that. One of the simplest is:

for d in dir1 dir2 dir3 dir4 dir5 dir6 dir7 dir8 dir9 dir10 ; do
    cat "$d/filename" >> /path/to/other/dir/filename
done

or, slightly better:

#! /bin/bash

outdir='/path/to/otherdir'
filename='filename.txt'

sourcedirs="dir1 dir2 dir3 dir4 dir5 dir6 dir7 dir7 dir9 dir10"

for d in $sourcedirs ; do
    cat "$d/$filename" >> "$outdir/$filename"
done

The advantage with this version is that just by changing the $sourcedirs variable, you can make it work with any number of directories. Also, $sourcedirs doesn't have to be manually enumerated, it can be generated by another command (e.g. find /top/level/directory -type d).

e.g. this version lets you specify the top-level directory as the first (and only) argument on the command-line. It then generates $sourcedirs as a list of all 1st-level subdirectories (-maxdepth 1) of that top-level directory.

#! /bin/bash

topdir="$1"    

outdir='/path/to/otherdir'
filename='filename.txt'

find "$topdir" -maxdepth 1 -exec cat "{}/$filename" \; >> "$outdir/$filename"

If you want to concatenate a list of all files matching a certain filename in a directory tree, no matter how deep in the tree they are, you'd do something more like this:

#! /bin/bash

topdir="$1"

outdir='/path/to/otherdir'
filename='filename.txt'

find "$topdir" -type f -name "$filename" -exec cat {} + >> "$outdir/$filename"

or as a one-liner:

find /top/dir -type f -name 'filename.txt' -exec cat {} + \
    >> /path/to/other/dir/filename.txt
  • i need something so that everytime i dont have to define the directories. i just need to define the directory and all the subdirectories containing the file should be read. – PRATEEK BHATT Mar 29 '16 at 2:01
  • @PRATEEKBHATT The find one-liner mentioned would be the shortest and most versatile approach. – Guido Mar 29 '16 at 4:01
  • The find versions only work if all of the files to be copied are in the same directory tree(s) (and you want to copy ALL found instances of the file). The for loop versions are better if the directories are scattered all over the filesystem or if you don't want to copy every instance found in a directory tree (or directory trees). – cas Mar 29 '16 at 4:28

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