LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-01-2009, 04:15 PM   #1
daberkow
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.
Code:
#!/bin/bash

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

done
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, 04:28 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by daberkow View Post
Code:
#!/bin/bash

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

done

Code:
#!/bin/bash
for files in *.txt; do
 echo "new line" >> $files
done
Works for me and so did your exact example.
 
Old 05-01-2009, 04:48 PM   #3
forrestt
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.

Forrest
 
Old 05-01-2009, 05:11 PM   #4
colucix
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:
Code:
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:
Code:
find . -name \*.txt -exec sed -i '$a new line' {} \;
or
Code:
find . -name \*.txt -print0 | xargs -0 sed -i '$a new line'
The loop suggested by rweaver works for me, too.
 
  


Reply


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 09:32 PM
append the text at the end wanghao Linux - Server 1 12-14-2007 11:17 AM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 07:17 PM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM


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

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