Concatenate files into one single file

baralp used Ask the Experts™
I am trying to concatenate the following individual files into one file based on the current date that is attached to the individual file names.
For eg: There could be several files. One of the file name appears like SVCTURN_ON_SO2_29JAN2014_075144.txt and another as PNDGSVCORD_SO1_29JAN2014_075153.txt and so on..
I need to find a way to concatenate these individual files that includes the current date in the name.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2015

Are you familiar with the cat command?


yes I am familiar with cat. I can do something like this cat *.txt >> all.txt. But I don't know how to concatenate those files that have current date attached in the file name. For eg. you can see the current date on the file SVCTURN_ON_SO2_26Feb2014_075144.txt that I need to look to concatenate.
This should do it:

for i in `ls | grep 29JAN`; do cat $i >> newfile.txt; done

It will find all the files in the current working directory that have 29JAN in them (current date) and then one by one put the contents of that file into newfile.txt.
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

SeanSystem Engineer


## date format ##

## Backup path ##

for i in /etc/logs/*
cat $i >> $FILE

This should do close to what you want. May need to change the paths/names etc but just put this into a shell script and run it.


In your command for i in `ls | grep 29JAN`; do cat $i >> newfile.txt; done
Can we replace 29Jan with the current date variable since the file name changes according to the run date?
SeanSystem Engineer
now=$(date +"%m_%d_%Y")

for i in `ls | grep $now`; do cat $i >> newfile.txt; done

This will use the current date.


I think now=$(date +"%m_%d_%Y")  would be now=$(date +"%d_%m_%Y") as the file name appears like SVCTURN_ON_SO2_26Feb2014_075144.txt. Correct?
SeanSystem Engineer

Yes you can change it to match the file.

see reference for the different ways to use the date command.
Yes, but it has to be in the format that is in the filename.

Try this:

today=`date +%d%b`; for i in `ls | grep $today`; do echo $i >> newfile.txt; done

This will format the date to look like this: 26Feb

Which matches the examples you provided.
Top Expert 2007
Assuming the files contain the date format


eg:  26Feb2014

then you can simply do

cat *$(date +%d%b%Y)* >concatenated_file

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial