LinuxQuestions.org
Visit Jeremy's Blog.
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 08-03-2003, 02:35 AM   #1
Caidence
LQ Newbie
 
Registered: Jul 2003
Location: NJ, US
Distribution: Debian
Posts: 15

Rep: Reputation: 0
Question Newbie Bash scripting question


Anyone know of anyway out there I can get ls to either

a.) give me all of the subdirectories in a directory

or

b.) give me all of the files of type *.spam in the current directory and all subdirectories in a format suitable to be read into a for loop's sentinel array?

What I'm trying to do:
I have a webpage that I preproccess with gcc, and output the HTML to another production directory.

I want to write a script that will find all of the source files in one directory, compile them, and place them in a parallel directory in the production directory... then do the same for all subdirectories.


ls's -R option gives everything in a sectional listing rather than a literal one, with every file preceeded by the directory it's located in.

Also, I can't find an ls option that only returns directories.

Perhaps I should be ls-ing the whole directory and finding a way in bash script to parse the filenames for their extensions?

I'm really a newbie in this area. Thanks for reading!
 
Old 08-03-2003, 02:50 AM   #2
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
Well, to list all the subdirectories, you can do something like

ls -RF | grep \/

When you have all the subdir's then you can go through these on an ordinary loop, and list the appropriate files.

Code:
#!/bin/bash

for dir in `ls -RF | grep \/ | tr \: \/` ; do
   for file in `ls $dir/*.c` ; do
      gcc $file <whatever options>
   done
done
Or not

The "tr \: \/" pipe only exchanges the trailing ":" to slashes, to avoid errors.

Last edited by Bebo; 08-03-2003 at 02:52 AM.
 
Old 08-03-2003, 04:53 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Find would be much better for this:
find . -type d
Would show all the directories in the tree.
find . -name *.spam -print
Would find all the *.spam files.
 
  


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
Newbie Bash Scripting Question jsaxton86 Programming 2 11-26-2005 07:58 PM
bash scripting question Red Squirrel Linux - General 1 07-11-2005 09:57 PM
bash scripting question mehesque Programming 2 03-07-2004 01:37 PM
Newbie bash scripting question... cmfarley19 Linux - Newbie 2 05-03-2003 08:26 AM
Scripting Question (BASH) merana Linux - General 4 01-12-2003 01:32 PM

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

All times are GMT -5. The time now is 01:45 AM.

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