Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-04-2009, 05:32 PM   #1
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:
for file in $dir/ref*
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, 05:52 PM   #2
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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:
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 05:54 PM.
Old 09-04-2009, 06:00 PM   #3
LQ Newbie
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Works like a charm. Thanks a lot!!
Old 09-04-2009, 06:03 PM   #4
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
shopt -nullglob
for file in $dir/ref*
Old 09-04-2009, 06:04 PM   #5
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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:
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:
nfiles=$(ls $dir/ref* 2> /dev/null | wc -l)


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 11:11 AM
counting files in multiple directories (zsh) Berticus Programming 2 09-08-2008 03:32 AM
create an error table? finding strings, and counting... in bash elinenbe Programming 3 06-26-2008 04:49 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 02:53 PM
Counting SLOC of multiple files uraja Linux - Newbie 1 09-12-2003 08:12 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration