LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-14-2009, 04:56 PM   #1
ffar
LQ Newbie
 
Registered: Sep 2009
Distribution: Ubuntu 8.04
Posts: 4

Rep: Reputation: 0
Post Appending files question/working with I/O redirection


Hello all linuxquestions members

I am quite new to linux, i hope some one will be able help me with this one.

Scenario

I am trying to send the output of ls command to a file and add text to each line of the output of ls command.

Example

ls

file1.txt
file2.txt

what i want to achive is to place url i.e http://mydomain.com/ in front of each new line/file listed by ls cmd so the output file will look like this :

http://mydomain.com/file1.txt
http://mydomain.com/file2.txt

and so on

it will save me alot of copy/paste manouvers

i have tried basic bash scripting and created variable with (http://mydomain.org/) but i am stuck now

any input will be much appreciated

thanks
 
Old 09-14-2009, 05:09 PM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by ffar View Post
Hello all linuxquestions members

I am quite new to linux, i hope some one will be able help me with this one.

Scenario

I am trying to send the output of ls command to a file and add text to each line of the output of ls command.

Example

ls

file1.txt
file2.txt

what i want to achive is to place url i.e http://mydomain.com/ in front of each new line/file listed by ls cmd so the output file will look like this :

http://mydomain.com/file1.txt
http://mydomain.com/file2.txt

and so on

it will save me alot of copy/paste manouvers

i have tried basic bash scripting and created variable with (http://mydomain.org/) but i am stuck now

any input will be much appreciated

thanks
one way:-

Code:
ls > file.txt;sed -i 's#^#http\:\/\/example.com\/#g' file.txt
 
Old 09-14-2009, 05:12 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The really robust way to do it (in case your file names include characters that ls will display as "?") is
Code:
#!/bin/bash
domain_name='http://mydomain.com/'
find . -type f -print0 | while IFS= read -r -d '' filename
do
    echo $domain_name$filename >> output.txt
done
Details in Greg's BASH FAQ WIKI. I just found that out this evening on LQ (thanks i92guboj )
 
Old 09-14-2009, 05:33 PM   #4
ffar
LQ Newbie
 
Registered: Sep 2009
Distribution: Ubuntu 8.04
Posts: 4

Original Poster
Rep: Reputation: 0
thank you both guys for a prompt response and solution to my problem ( prolly not the last one since i am getting used to linux now)

@centosboy - yours worked perfectly

@catkin - when i run your command i have the output of

http://mydomain.com/./file1.txt
http://mydomain.com/./file2.txt

have i done smth wrong?
 
Old 09-14-2009, 05:46 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by ffar View Post
@catkin - when i run your command i have the output of

http://mydomain.com/./file1.txt
http://mydomain.com/./file2.txt

have i done smth wrong?
Oops! No, nothing wrong. The file names are coming in with pathnames relative to the current working directory, ".". Solution is to strip off the leading "./". This should do it
Code:
#!/bin/bash
domain_name='http://mydomain.com/'
find . -type f -print0 | while IFS= read -r -d '' filename
do
    echo "$domain_name${filename#./}" >> output.txt
done
and the added double quotes are good practice.
 
Old 09-14-2009, 06:05 PM   #6
ffar
LQ Newbie
 
Registered: Sep 2009
Distribution: Ubuntu 8.04
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Oops! No, nothing wrong. The file names are coming in with pathnames relative to the current working directory, ".". Solution is to strip off the leading "./". This should do it
Code:
#!/bin/bash
domain_name='http://mydomain.com/'
find . -type f -print0 | while IFS= read -r -d '' filename
do
    echo "$domain_name${filename#./}" >> output.txt
done
and the added double quotes are good practice.
thanks once again, worked like a charm
 
  


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
Appending the files dina3e Programming 4 10-15-2008 08:03 PM
Appending files whithout reading them cwomeijer Linux - Software 18 01-28-2007 06:48 AM
Appending files in star - strange behaviour billymayday Linux - Software 3 05-05-2006 12:13 PM
core files appending PIDS - they shouldn't be! LavieX Linux - Enterprise 2 04-15-2005 03:26 PM
[c] Appending to files using fopen() in mandrake wedamija Programming 4 11-01-2002 10:39 AM


All times are GMT -5. The time now is 08:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration