Wednesday, November 21, 2007

Concatenate Multiple Files in Linux

File concatenation involves merging of one or multiple file contents into one single file. We all know that 'cat' linux command displays the contents of a file. Cat linux command can also be used to concatenate file contents into a single file. The concatenation command argument follows the following pattern as follows:

# cat inputfile1 inputfile2 ... inputfileN > outputfile

Here are more sample on how to merge and concatenate multiple file(s) of same file types into a single output file using cat.

# cat file1 file2 >> newfile
Legend
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
file1 and file2 - input files
">" - output operator for new output file
">>" - ouput operator for existing output file
newfile - output files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** Note that if newfile is already existing, it will be overwritten by the merged contents of all input files


More samples of merging contents of multiple files from terminal

The line below creates a new output file called newfile and all contents of new files are merged into the newfile using the redirection ">" operator.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cat file1 file2 file3 file4 file5 file6 > newfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The line below merges all contents of all input files (file1,file2,file3,file4...file6) into an already existing 'existingfile' file using the redirection ">>" operator .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cat file1 file2 file3 file4 file5 file6 >> existingfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Take note that if the output file is already existing, the file access attributes and file ownerships of the output file is preserved. However, the date and time stamps would be changed and updated to current date/time values. This preservation state applies both to ">>" and ">" redirection operators.

Concatenation of files are usually being applied to readable file types, mostly to text date files in linux. Folder path can also be specified with input and output files when concatenating multiple files.

HTH

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy