LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-24-2006, 03:15 PM   #1
thetawaverider
Member
 
Registered: Feb 2006
Distribution: CentOS
Posts: 47

Rep: Reputation: 15
Filtering "for in" loop output


This somewhat refers to my previous thread, but it's kindof it's own question now, so here goes...

I'm looping though all the folders in /var/www/vhosts/ with the following script:

for i in `ls /var/www/vhosts/`;
do
echo $i/conf/
done

I need to only echo folders that begin with a certain word. Seemingly, I should use an if conditional to test whether or not the word exists in the folder name. Would I need to use a regular expression here to test, or is there a simpler way? Any examples would be helpful.

Thanks,
TWR

Last edited by thetawaverider; 07-26-2007 at 04:20 PM.
 
Old 03-24-2006, 05:07 PM   #2
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Let's assume the certain word is "something".

for i in `ls /var/www/vhosts/something*`;
do
echo $i/conf/
done

Does it need to be a directory?

for i in `ls /var/www/vhosts/something*`;
do
[ -d "$i" ] && echo $i/conf/
done
 
Old 03-24-2006, 06:15 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The simple answer would seem to be:

Code:
for i in `ls /var/www/vhosts/filespecRegexp`;
do
echo $i/conf/
done
--- rod.
 
Old 03-24-2006, 08:17 PM   #4
thetawaverider
Member
 
Registered: Feb 2006
Distribution: CentOS
Posts: 47

Original Poster
Rep: Reputation: 15
That's what I tried originally, but using:
Code:
for i in `ls /var/www/vhosts/something*`;
returns the contents of the something* folders themselves, not the actual folder names. The output I got when running
Code:
ls /var/www/vhosts/something*
is:
Code:
ls /var/www/vhosts/something1.com:
anon_ftp  conf        httpdocs   pd          subdomains   var
bin       error_docs  httpsdocs  private     tmp          web_users
cgi-bin   etc         lib        statistics  usr

Last edited by thetawaverider; 03-24-2006 at 08:19 PM.
 
Old 03-24-2006, 10:13 PM   #5
thetawaverider
Member
 
Registered: Feb 2006
Distribution: CentOS
Posts: 47

Original Poster
Rep: Reputation: 15
As a quick summary of the above,
Code:
ls /var/www/vhosts/something*
does not return the names of the folders starting with "something", but returns the contents of each folder - that is, every folder inside something1.com, something2.com, etc. I need to return only the folder names that start with "something" that are a direct child of /var/www/vhosts/.

Anyone else know how to do this?

Thanks
 
Old 03-25-2006, 10:55 AM   #6
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
You can get rid of the "ls" part....

for i in /var/www/vhosts/something*;
do
[ -d "$i" ] && echo $i/conf/
done
 
Old 03-25-2006, 02:37 PM   #7
thetawaverider
Member
 
Registered: Feb 2006
Distribution: CentOS
Posts: 47

Original Poster
Rep: Reputation: 15
Perfect. This is exactly what I need. Thanks a million, mikshaw.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Install stops at "Calibrating delay loop" eam Linux - General 0 07-23-2004 08:39 PM
"Real time" Apache log filtering with Perl skelly Programming 1 07-01-2004 03:24 PM
"Real-time" Apache Log Filtering skelly Linux - Software 1 06-30-2004 03:16 PM
PHP "for.. loop" to insert into mysql Boby Programming 10 05-29-2004 11:07 AM
Getting AND filtering the output of "wvdial" rkrijger Linux - Software 0 11-17-2003 09:02 AM


All times are GMT -5. The time now is 06:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration