Add text to beginning of file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Add text to beginning of file

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 08-24-2007
Posts: n/a
Add text to beginning of file

I need to add text to the beginning of a file in the same way that cat will put file contents at the end of a file. I want to do this with many files eg
cat newtext >> /usr/home/*/*.bat

Any ideas?
Sponsored Links
Old 08-24-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
Something like this

$ ls /usr/home/*/*.bat | while read file; do 
>   ( echo "add this"; cat ${file} ) > ${file}.new && mv ${file}.new ${file}
> done

Sponsored Links
Old 08-24-2007
ghostdog74 ghostdog74 is offline
Registered User
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts

sed -i  '1i add here' file*bat

The Following User Says Thank You to ghostdog74 For This Useful Post:
tinufarid (03-13-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count Number Of lines in text files and append values to beginning of file motoxeryz125 UNIX for Dummies Questions & Answers 7 04-28-2011 03:36 AM
bash: prepend same text to beginning of all lines of file SilversleevesX Shell Programming and Scripting 1 09-18-2010 06:55 PM
Appending at the beginning of the file. siba.s.nayak UNIX for Dummies Questions & Answers 1 04-14-2010 08:16 AM
Append (cat) to beginning of file ? CBZ Shell Programming and Scripting 8 02-19-2008 01:26 AM
cat a file from end to beginning jpprial UNIX for Dummies Questions & Answers 5 03-31-2001 08:14 AM

All times are GMT -4. The time now is 11:08 PM.