LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-04-2009, 04:32 PM   #1
paul_l
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 0
[Bash] Counting files


I'm making a script that's supposed to do different things depending of how many files there is in a folder that starts with ref.

I've tried to do a count with this for-loop:
Code:
for file in $dir/ref*
do
		i=$((i+1))
done
It seems to work fine, as long as there is at least one file starting with ref in the folder. If there is none the loop runs once with the variable file as "$dir/ref*". Anybody got some suggestion on how I could avoid that? Alternative solutions are also welcome

Thanks a lot in advance!
 
Old 09-04-2009, 04:52 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
i havent done any linux scripting in quite a long time, but instead of using the hardcoded string "$dir/ref*", can you run a command and save the output? something like:
Code:
for file in `ls $dir/ref*`
i know this is possible in other shells, but the syntax may be different in bash, i dont remember.

edit: note that above ` are "backticks" not single quotes.

Last edited by nadroj; 09-04-2009 at 04:54 PM.
 
Old 09-04-2009, 05:00 PM   #3
paul_l
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Works like a charm. Thanks a lot!!
 
Old 09-04-2009, 05:03 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Try
Code:
shopt -nullglob
for file in $dir/ref*
do
		i=$((i+1))
done
 
Old 09-04-2009, 05:04 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Following the suggestion by nadroj above, you can use the ls command, but you don't really need a loop if you do something like:
Code:
ls $dir/ref* 2> /dev/null | wc -l
the 2> /dev/null part redirects the standard error to nothing, so that you don't get the message "ls: cannot access dirname/ref*: No such file or directory" in the terminal if ls does not find ref* files. To assign the result to a variable, just do command substitution:
Code:
nfiles=$(ls $dir/ref* 2> /dev/null | wc -l)
 
  


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
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 10:11 AM
counting files in multiple directories (zsh) Berticus Programming 2 09-08-2008 02:32 AM
create an error table? finding strings, and counting... in bash elinenbe Programming 3 06-26-2008 03:49 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM
Counting SLOC of multiple files uraja Linux - Newbie 1 09-12-2003 07:12 PM


All times are GMT -5. The time now is 06:36 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