Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-01-2009, 05:15 PM   #1
LQ Newbie
Registered: May 2009
Posts: 11

Rep: Reputation: 0
append end of multiple files

Hi. I'm trying to add one line to the end of all the files of a certain extension in a directory. For some reason, this is creating a new file named the exact same thing only with a ? added to the end.

for files in `ls *\.txt`
echo "new line" >> ${files}

I have blah.txt, and this creates a new file called blah.txt? that only contains "new line" without the qoutes. If I run it again, it adds a second "new line" to blah.txt?

Thanks for any help.
Old 05-01-2009, 05:28 PM   #2
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Originally Posted by daberkow View Post

for files in `ls *\.txt`
echo "new line" >> ${files}


for files in *.txt; do
 echo "new line" >> $files
Works for me and so did your exact example.
Old 05-01-2009, 05:48 PM   #3
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I'm getting the same results as rweaver.

Old 05-01-2009, 06:11 PM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
It is not clear the syntax used to list the txt files:
ls *\.txt
where the backslash just escapes the dot, but it is not necessary. What is the output of this command if you run it alone?

Anyway, here is an alternative, using sed:
find . -name \*.txt -exec sed -i '$a new line' {} \;
find . -name \*.txt -print0 | xargs -0 sed -i '$a new line'
The loop suggested by rweaver works for me, too.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
wanna to add in newlines in begin and end of multiple files tiaratiara Linux - Newbie 8 03-18-2009 10:32 PM
append the text at the end wanghao Linux - Server 1 12-14-2007 12:17 PM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 08:17 PM
bash: append string to end of line khairil Programming 6 02-27-2007 06:09 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 10:28 AM > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration