LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-12-2008, 09:50 AM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
bash - find and append to each file found


Hello,


I'm trying to write a bash one liner that finds some files, and for each file it appends a string to the end of them. I tried several attempts with no luck.

Code:
find . -name "*.txt" -exec echo "append_this_string" >> `echo {}` \;
OR

Code:
find . -name "*.txt" -exec echo "append_this_string" >> {} \;
Both the these examples do not work. I tried googling and reading man pages, but couldn't find anything. Since my bash skills aren't very good, I was hoping someone could help me out.

I figured I could write a script, but I was hoping I could do it with a simple one-liner. (to impress the ladies )

Thanks,

djgerbavor3
 
Old 11-12-2008, 09:56 AM   #2
Autocross.US
LQ Newbie
 
Registered: Aug 2006
Location: Chesapeake, VA
Distribution: Solaris, HP-UX, RedHat, Fedora
Posts: 15

Rep: Reputation: 0
Here's my one-liner:

find . -name "*.txt" | while read TXT ; do echo "string to append" >> $TXT ; done
 
Old 11-12-2008, 10:00 AM   #3
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
that works for me.

thanks,
 
Old 11-12-2008, 10:05 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using sed:
Code:
find . -name "*.txt" -exec sed -i '$ a\append_this_string' {} \;

Last edited by colucix; 11-12-2008 at 10:07 AM.
 
Old 11-12-2008, 10:05 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using sed:
Code:
find . -name "*.txt" -exec sed -i '$ a\append_this_string' {} \;
Edit: sorry for double post, just a mistake...

Last edited by colucix; 11-12-2008 at 10:09 AM.
 
Old 11-12-2008, 10:14 AM   #6
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
colucix, I like your way..very cool. Thanks
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
BASH append to middle of file baks Programming 4 02-28-2007 03:27 AM
determine whether 'find' cmd found the file or not mufy Linux - General 7 03-16-2005 07:24 AM
Bash File not found, What am I doing wrong? Magus_Rune Programming 2 11-28-2004 03:39 PM
[Bash] Find occurences in a file, then change them fr0st Linux - General 2 04-05-2004 09:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:13 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
Open Source Consulting | Domain Registration