I need to combine all files in a folder to a single file.
Files are named as t1, t2, t3, ..., t1500
and output file is "all
".
I've used the following command in bash
: cat t* >> all
but it combines files in the sequence t1, t10, t11, ...
instead of t1, t2, t3, ...
Any help please.
To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.
You need to use the cp command. It is used to copy files and directories. The copies become independent of the originals. Any subsequent change in one will not affect the other.
If you want to combine multiple large files, then instead of specifying each file's name to be concatenated, use the wildcards to identify these files, followed by an output redirection symbol. Hence, it is possible to concatenate all the files in the current directory using an asterisk (*) symbol wildcard as: cat *.
You can try
cat t{1..1500} > all
If you get any error involving a too-long command line, you can try
for i in {1..1500}; do
echo "t$i"
done | xargs -n 1500 cat
A longer, but arguably more readable, method that manually splits the list up between multiple calls to cat
:
{ cat t{1..100}
cat t{101..500}
cat t{601..1000}
cat t{1001..1500}
} > all
Mine:
find -maxdepth 1 -regextype posix-egrep -regex '.*t[[:digit:]]+$' | sort -V | xargs cat > all
Or
shopt -s extglob
printf "%s\n" t+([[:digit:]]) | sort -V | xargs cat > all
Or perhaps simply (if applicable):
printf "%s\n" t* | sort -V | xargs cat > all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With