Your question needs clarity. There's two possible cases that I can see happening:
Copying many to one:
for file in directory/*.txt; do cat "$file" >> someotherdir/combined.txt; done
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.