LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2007, 08:48 PM   #1
keith2045
Member
 
Registered: Feb 2005
Location: Missour, USA
Posts: 83

Rep: Reputation: 15
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)
 
Old 05-29-2007, 09:02 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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/*
....
 
Old 05-29-2007, 09:39 PM   #3
mechdave
Member
 
Registered: Apr 2007
Location: Adelaide, Australia
Distribution: Ubuntu 8.10 and 7.10 server
Posts: 95

Rep: Reputation: 15
Post

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
 
Old 05-29-2007, 11:20 PM   #4
tread
LQ Newbie
 
Registered: Feb 2007
Posts: 29

Rep: Reputation: 15
'find' with the '-exec' option is your friend - you don't even have to take special care for spaces in file names
 
Old 05-31-2007, 07:00 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
for file in `find . -type f -maxdepth 1 -print 2>/dev/null`
do
echo $file
done
 
Old 05-31-2007, 07:44 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
.
 
Old 05-31-2007, 05:26 PM   #7
keith2045
Member
 
Registered: Feb 2005
Location: Missour, USA
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks for your help guys. I got it working.
 
Old 05-31-2007, 07:26 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
ghostdog74: Yep, see the in-line comment in the OP
 
  


Reply



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
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 11:29 AM
Shell Script to kill a process automaticaly brazilian_user Programming 11 06-12-2006 09:09 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 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
Open Source Consulting | Domain Registration