LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 11-04-2011, 07:51 AM   #1
Christos Badogias
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Rep: Reputation: Disabled
script to read paths


hallo for a newbie to shell programming!!i have to read all files from a folder,then open a new file which have all the paths from each file which i had read!!i begin like this:touch newfile,then make a loop to read the folder,using find and cp to copy it!then i use cat newfile!!!any help pls???
 
Old 11-04-2011, 08:01 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Code:
ls -l /my/folder > /myfile; cat /myfile
 
Old 11-04-2011, 08:20 AM   #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
Or (more complete and more robust)
Code:
echo /my/folder/.* /my/folder/* > /tmp/myfile; cat /tmp/myfile
EDIT: ignore that -- it doesn't give one per line

Last edited by catkin; 11-04-2011 at 08:21 AM.
 
Old 11-04-2011, 08:23 AM   #4
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 Christos Badogias View Post
hallo for a newbie to shell programming!!i have to read all files from a folder,then open a new file which have all the paths from each file which i had read!!i begin like this:touch newfile,then make a loop to read the folder,using find and cp to copy it!then i use cat newfile!!!any help pls???
What's the problem?

It would help if you posted what you have tried, any output and an explanation of how it differs from what you want.
 
Old 11-04-2011, 09:09 AM   #5
Christos Badogias
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
sorry i can't understand very well!!is my first script and is a mess!i can't read the file(name task) and print the paths which files have in a new file(name paths.txt)....the paths.txt must create it!!!!!
 
Old 11-04-2011, 10:23 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,358

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Are you trying to create a file that contains the names of all of the files in a directory, or do you have a directory full of files, each of which contains a list of files, and you're trying to create a single file that contains all of the filenames listed in each of the files in the directory?

It's hard to understand what you're trying to do. Maybe if you give an example it would be easier.
 
Old 11-04-2011, 11:14 AM   #7
Christos Badogias
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
the second!!thank you!
 
Old 11-04-2011, 12:51 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,358

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
This should do what you want, of course you might want to clean it up a bit, but this should give you the general gist.

Code:
#!/bin/bash
rm -f /tmp/myfile
for i in /myfolder/*; do
   cat "$i" >> /tmp/myfile
done

Last edited by suicidaleggroll; 11-04-2011 at 12:52 PM.
 
Old 11-05-2011, 08:15 AM   #9
Christos Badogias
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
I tried it and it worked but the problem is that it showed me only the files in my folder, but i want it to show the files in the subfolder which are in my folder. Have any idea how to do that?
 
Old 11-05-2011, 08:29 AM   #10
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
Code:
#!/bin/bash
rm -f /tmp/myfile
for i in /myfolder/* /myfolder/*/*; do
   cat "$i" >> /tmp/myfile
done
That will not show files or directories beginning with a ".". For that you would want /myfolder/* /myfolder/.* /myfolder/*/* /myfolder/*/.* /myfolder/.*/* /myfolder/.*/.* which is getting messy and may result in errors when any of the patterns do not match files (depending on how bash is configured to expand unmatched file name patterns) and when the patterns match a directory.

Alternatively:
Code:
> /tmp/myfile  # Empty it
while IFS= read -r -d '' file
do
   cat "$file" >> /tmp/myfile
done < <(find /myfolder -type f -print0)
This will get all the files in all the subdirectories. f you want to restrict it to only the subdirectories of /myfolder, use find's -depth option.
 
Old 11-06-2011, 12:56 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,358

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Quote:
Originally Posted by catkin View Post
Code:
#!/bin/bash
rm -f /tmp/myfile
for i in /myfolder/* /myfolder/*/*; do
   cat "$i" >> /tmp/myfile
done
That will not show files or directories beginning with a ".". For that you would want /myfolder/* /myfolder/.* /myfolder/*/* /myfolder/*/.* /myfolder/.*/* /myfolder/.*/.* which is getting messy and may result in errors when any of the patterns do not match files (depending on how bash is configured to expand unmatched file name patterns) and when the patterns match a directory.
Indeed, I make it a point to never include directories beginning in "." in my scripts unless explicitly required (almost never).

OP - if you would like to include subdirectories then you should use "find" as catkin suggested.
 
Old 11-06-2011, 09:51 AM   #12
Christos Badogias
LQ Newbie
 
Registered: Nov 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
thanks for the help!!!
 
  


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
Shell Script how to remove absolute paths from zip archive hi_irf Linux - Newbie 1 10-03-2009 06:59 AM
shell script read non-interactive comtmr Linux - General 6 11-01-2006 07:54 AM
Creating a bash script which understands paths to files en8scl Linux - Newbie 9 05-21-2006 08:22 PM
Help with BASH script PLEASE READ!!! hroman Programming 7 10-08-2004 08:39 PM
Automatically resolving WINDOWS paths to pre-configured Linux paths gazzy Linux - General 1 09-05-2003 11:15 PM


All times are GMT -5. The time now is 05:30 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