LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to read paths (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-read-paths-911823/)

Christos Badogias 11-04-2011 06:51 AM

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???

kbp 11-04-2011 07:01 AM

Code:

ls -l /my/folder > /myfile; cat /myfile

catkin 11-04-2011 07:20 AM

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

catkin 11-04-2011 07:23 AM

Quote:

Originally Posted by Christos Badogias (Post 4515646)
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.

Christos Badogias 11-04-2011 08:09 AM

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!!!!!

suicidaleggroll 11-04-2011 09:23 AM

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.

Christos Badogias 11-04-2011 10:14 AM

the second!!thank you!

suicidaleggroll 11-04-2011 11:51 AM

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


Christos Badogias 11-05-2011 07:15 AM

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?

catkin 11-05-2011 07:29 AM

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.

suicidaleggroll 11-05-2011 11:56 PM

Quote:

Originally Posted by catkin (Post 4516449)
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.

Christos Badogias 11-06-2011 08:51 AM

thanks for the help!!!


All times are GMT -5. The time now is 02:32 PM.