LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 03-08-2012, 12:07 PM   #1
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
bash looping through a group of files alternatives to ls?


Hello there guys, i am trying to work on reports, nasty things

i am doing this thing

Code:
for group in $(ls -1 grupos_*)
do
echo "Hello $group"
done
But i do know that using ls is not he right way any sugegstions
 
Old 03-08-2012, 12:31 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Is this what you are after:
Code:
for group in grupos_*
do
  echo "Hello $group"
done
There's no need for the ls -1, grupos_* is expanded by the shell when processing the commands.

Hope this helps.
 
Old 03-09-2012, 09:49 AM   #3
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
Rep: Reputation: 15
thanks

yeah i found that yesterday forgot to put it on the forum, thanks a bunch for answering though
 
Old 03-10-2012, 01:06 AM   #4
propofol
Member
 
Registered: Nov 2007
Location: Seattle
Distribution: Debian Squeeze, Wheezy
Posts: 244

Rep: Reputation: 46
One other option which adds the flexibility of 'find' and avoids issues with spaces in file names:

Code:
find . -type f -name 'grupos_*' -print | while read group
do 
   echo "Hello $group"
done
Regards,
Stefan
 
Old 03-10-2012, 07:19 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Check out these links for details on how to handle this issue:

http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.org/DontReadLinesWithFor
http://mywiki.wooledge.org/BashFAQ/001

In a nutshell, you have to be very careful how you handle the output of commands, particularly ones that generate lists of filenames. Word-splitting can cause all sorts of problems if you don't do it right.

propofol's find solution is better, but still not completely safe as written. You should always use the -print0 option, and the corresponding syntax in the reading application. This is also detailed in the above links.


Finally, beware of this common pipe subshell pitfall:

http://mywiki.wooledge.org/BashFAQ/024
 
1 members found this post helpful.
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] (BASH) Looping over an array Dick Dastardly Programming 6 01-22-2012 07:37 AM
[SOLVED] Looping over mysql query results in bash bryanvick Programming 5 09-15-2010 08:19 PM
Looping issue in bash scripting ZAMO Linux - General 4 09-26-2008 11:22 AM
Looping a bash script SoulGrind Linux - Newbie 5 09-22-2006 06:03 PM
Bash case structure (looping) ravvar Programming 4 10-07-2003 07:07 PM


All times are GMT -5. The time now is 08:24 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration