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 03-22-2007, 01:06 AM   #1
magnim
LQ Newbie
 
Registered: Mar 2006
Location: Edmonton, AB
Distribution: Ubuntu 11.04
Posts: 2

Rep: Reputation: 0
script to insert filenames into files


I have a few directories containing some text files (a couple thousand files total)
Trying to figure out how to go through the contents of a single directory and insert the name of each file into each of the files, along with some additional text before and after the filename.

Any help would be fantastic.
 
Old 03-22-2007, 01:27 AM   #2
johngreenwood
Member
 
Registered: Nov 2006
Location: Lancashire, United Kingdom
Distribution: Slackware 13
Posts: 243

Rep: Reputation: 31
Hi there, and welcome.
Code:
ls > filename
will get your contents of a single directory into a text file, but I don't know about the other part. I think you will have to be more specific so that the other members will know exactly what you are trying to do.

Edit: Hang on, I just read it again, you want to put the filename into the file it belongs to, sorry. The above command won't do that, In fact, just ignore this post.
(I am tired an have a massive headache)

Sorry for any confusion.

Last edited by johngreenwood; 03-22-2007 at 01:30 AM.
 
Old 03-22-2007, 02:12 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Untested...

Caveat: this doesn't handle files with spaces in the names
Run once per directory ... or modify to match your needs
better.

Code:
for i in `ls *txt`; do echo $i "And other text" > tmp; cat tmp $i > $i.tmp; mv $i.tmp $i; done


Cheers,
Tink
 
Old 03-22-2007, 09:19 AM   #4
magnim
LQ Newbie
 
Registered: Mar 2006
Location: Edmonton, AB
Distribution: Ubuntu 11.04
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks Tinkster! Works like a charm...
 
Old 03-22-2007, 01:05 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Good good :}


Cheers,
Tink
 
  


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
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 02:10 AM
shell script - truncating filenames mr_scary Programming 3 09-04-2006 02:50 PM
Shell script: Delete filenames containing a substring? Drack Linux - General 4 02-12-2006 03:11 PM
need help with script to remove all metachars from filenames BrianK Programming 5 08-20-2005 11:10 PM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 03:10 PM

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

All times are GMT -5. The time now is 01:26 AM.

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