2

I want to copy the whole content of the file into another multiple files as group wise. Suppose If I select 10 .txt files then the contents of those files should be copy and paste in a another one new file. These all files are in the same directory.

I want to do this to multiple files (<2000). So can't do it manually.

5
  • 3
    Do you want to combine the contents of multiple files to one file? Do you want to copy the whole content of a file or only part of it? Please explain in more detail what you want to achieve and provide examples.
    – danzel
    Dec 10, 2018 at 10:57
  • What file do you want to copy to? Are all your input files in the same directory? What name should the target files have? Can't you just copy the directory instead? Please edit ad clarify what you need.
    – terdon
    Dec 10, 2018 at 11:23
  • 1
    Copy the file? Then "the content of the txt file" is copied into another file. You really will need to be more specific on what you want to accomplish.
    – vanadium
    Dec 10, 2018 at 11:36
  • I want to copy the whole content of the file into another multiple files. What I need to do is a creating a corpus for a research purpose. These files contains with uni codes characters.
    – Tharuka
    Dec 11, 2018 at 4:57
  • I have edited the question can you please check.
    – Tharuka
    Dec 11, 2018 at 5:02

2 Answers 2

3

Your question needs clarity. There's two possible cases that I can see happening:

  1. Copying many to one:

    for file in directory/*.txt; do cat "$file" >> someotherdir/combined.txt; done
    
  2. Copying many to many:

    for file in directory/*.txt; do cp "$file" someotherdir/"$( basename "$file" )".copy ; done
    

In each example we're looking at directory named directory in your current location ( check that with pwd command ). So if I'm in /home/foo_user we're looking at /home/foo_user/directory. The glob *.txt will match all files with .txt ending. The destination directory someotherdir also will be referenced relative to your current location. Provide full path such as /home/foo_user/someotherdir/anotherdir if you want to copy to specific location. In first example we're using cat and >> to append contents of all files to single file. In the other cp makes a a copy of each file to new filename, which will take out the name of file currently in "$file" variable and append .copy to it.

Note that if naming is not of importance, we can reduce second case to just making new directory and copying it recursively via cp -r:

mkdir copies
cp -r original_dir/ copies/

So can be done with first case like cat dir/*.txt > combined.txt however loops are preferable when you are dealing with a lot of files and may run into Argument list too long error, from which shell loops do not suffer.

Without knowing specific details, these two solutions should suffice for multiple cases. Adapt accordingly to your particular scenario.

1
  • Of the two answers posted, this is the better +1 Dec 11, 2018 at 0:17
1

To copy all the content from file1.txt to file2.txt use the following code:

cp file1.txt file2.txt

If you know all of the names of the input and output files, you could use a loop:

#!/bin/bash
input=(input1.txt input2.txt inputn.txt)
output=(output1.txt output2.txt outputn.txt)
i=0
while (i<="${input#[@]}")
do
    cp "${input[i]}" "${output[i]}"
    i++
done

Most of this, however, would depend on the specific situation.

3
  • 1
    If you want the array count of elements ${input#} should be changed ${#input[@]}. In my tests ${input#} returns the value of the first element, in your case input1.txt which isn't a number at all. Dec 11, 2018 at 0:14
  • 1
    In addition to that ^, you need (( )) for arithmetic, not ( ).
    – muru
    Dec 11, 2018 at 4:21
  • @WinEunuuchs2Unix, thank you for the correction Dec 11, 2018 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .