LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script - process directory (https://www.linuxquestions.org/questions/programming-9/shell-script-process-directory-557617/)

keith2045 05-29-2007 08:48 PM

shell script - process directory
 
I'm trying to write a script that will search a directory and do something with all of the files in the directory. Here is what i have so far.

for show in /mydirectory
do
echo $show #it will do more, just when i can get it to work.
done

when i run the script it shows /mydirectory instead of showing all of the files. Probably an easy fix, i'm not a programmer (not yet)

ghostdog74 05-29-2007 09:02 PM

Quote:

Originally Posted by keith2045
I'm trying to write a script that will search a directory and do something with all of the files in the directory. Here is what i have so far.

for show in /mydirectory
do
echo $show #it will do more, just when i can get it to work.
done

when i run the script it shows /mydirectory instead of showing all of the files. Probably an easy fix, i'm not a programmer (not yet)

Code:

for show in /mydir/*
....


mechdave 05-29-2007 09:39 PM

Try this tutorial out. It is a guide to writing bash scripts. Try this tutorial for learning the whole lot. And also of course the tldp.org Bash Guide for Beginners. Also use google a lot for searching... It is your best friend :)
Have fun learning

tread 05-29-2007 11:20 PM

'find' with the '-exec' option is your friend - you don't even have to take special care for spaces in file names :)

chrism01 05-31-2007 07:00 AM

for file in `find . -type f -maxdepth 1 -print 2>/dev/null`
do
echo $file
done

ghostdog74 05-31-2007 07:44 AM

Quote:

Originally Posted by chrism01
for file in `find . -type f -maxdepth 1 -print 2>/dev/null`
do
echo $file
done

I take it that besides echoing the filename, there are other processing to be done to the file, that's why you have used a for loop..? If not the above is simply
Code:

find . -type f -maxdepth 1 -print #or ls
.

keith2045 05-31-2007 05:26 PM

Thanks for your help guys. I got it working.

chrism01 05-31-2007 07:26 PM

ghostdog74: Yep, see the in-line comment in the OP


All times are GMT -5. The time now is 03:13 PM.