Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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???
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.
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!!!!!
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.
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?
#!/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.
#!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.