Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy contents of all files into one file in bash different sequence

Tags:

bash

cat


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.

like image 487
Wamiq Avatar asked Jun 10 '14 19:06

Wamiq


People also ask

How do I copy a bunch of files at once in Linux?

To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.

How do I copy the contents of one file to another?

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.

How do I combine all texts into one file in Linux?

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 *.


2 Answers

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
like image 124
chepner Avatar answered Sep 30 '22 10:09

chepner


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
like image 35
konsolebox Avatar answered Sep 30 '22 11:09

konsolebox