LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-06-2005, 12:18 PM   #1
bullium
Member
 
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146

Rep: Reputation: 17
Question Number of files in directory


Here is what I have so fare. It works but the only problem is that the count is not correct, it is also counting the directories in the total. I'm trying to get a total count of my MP3 files from My Music and all its subdirectories. Here is the directory structure.

My Music
| |_> Artist
| |_> Album
| |_> songname1.mp3
| |_> songname2.mp3
|__> Artist 2
|_> Album2
|_> songname1.mp3

Code:
ls -1R | wc -l
(note the "-1" option to 'ls' is hyphen-one, NOT lower-case L, which is
what the option to 'wc' is).

All of my music is in MP3 format so a *.mp3 would work I just don't know how to ad it into the command.
 
Old 01-06-2005, 12:27 PM   #2
Valindar
Member
 
Registered: Jun 2004
Location: Melbourne, Australia
Distribution: Slackware 9.1
Posts: 33

Rep: Reputation: 15
Use a regular expression with grep:
Code:
ls -1R | grep .*.mp3 | wc -l
list all recursively one per line, piped through grep which will output only: any amount of characters, then a dot character then 'mp3' then piped through wc -l to count it.

Edit: Just adding, grep is a very, very useful tool, you might want to read the man page and learn to use it (this is presuming you don't know how to). Grep, awk, sed etc. are very powerful allies when you're working in the CLI!
Check out http://tldp.org for the beginner's guide to bash - amongst teaching you bash basics it will also teach you regexps, grep, awk and sed.

Last edited by Valindar; 01-06-2005 at 12:32 PM.
 
Old 01-06-2005, 12:37 PM   #3
bullium
Member
 
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146

Original Poster
Rep: Reputation: 17
Thumbs up

Quote:
Originally posted by Valindar
Use a regular expression with grep:
Code:
ls -1R | grep .*.mp3 | wc -l
list all recursively one per line, piped through grep which will output only: any amount of characters, then a dot character then 'mp3' then piped through wc -l to count it.

Edit: Just adding, grep is a very, very useful tool, you might want to read the man page and learn to use it (this is presuming you don't know how to). Grep, awk, sed etc. are very powerful allies when you're working in the CLI!
Check out http://tldp.org for the beginner's guide to bash - amongst teaching you bash basics it will also teach you regexps, grep, awk and sed.
Thanks worked like a charm! I'm still working on learning all of the | in and outs. How are you a Newbie !
 
Old 01-06-2005, 12:43 PM   #4
Valindar
Member
 
Registered: Jun 2004
Location: Melbourne, Australia
Distribution: Slackware 9.1
Posts: 33

Rep: Reputation: 15
I'm not a newbie - I just haven't posted much
 
Old 01-06-2005, 01:26 PM   #5
bullium
Member
 
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146

Original Poster
Rep: Reputation: 17
Quote:
Originally posted by Valindar
I'm not a newbie - I just haven't posted much
For whoever reads this post and it helps them, I noticed that I had a few mp3's that weren't lowercase hints "MP3" "mp3" "Mp3". Any way I added another argument to grep to also grab those. Here it is.


Code:
ls -1R | grep -i .*.mp3 | wc -l

Have fun, and thanks again Valindar

Last edited by bullium; 02-16-2005 at 03:09 PM.
 
Old 09-10-2009, 12:40 PM   #6
gqchynaboy
LQ Newbie
 
Registered: Jul 2003
Posts: 20

Rep: Reputation: 0
Code:
ls -1R | grep -i .*.mp3 | wc -l
Now how would you store the count in a bash script?
 
Old 09-10-2009, 12:56 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by gqchynaboy View Post
Code:
ls -1R | grep -i .*.mp3 | wc -l
Now how would you store the count in a bash script?
It depends on what you mean by "store." Example:

#/bin/sh

total=`ls -1R | grep -i .*.mp3 | wc -l`

echo $total
 
Old 09-10-2009, 01:02 PM   #8
gqchynaboy
LQ Newbie
 
Registered: Jul 2003
Posts: 20

Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
It depends on what you mean by "store." Example:

#/bin/sh

total=`ls -1R | grep -i .*.mp3 | wc -l`

echo $total
I want to use the count in a loop, what you have above seems to store the string comand in $total.
 
Old 09-10-2009, 01:22 PM   #9
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by gqchynaboy View Post
I want to use the count in a loop, what you have above seems to store the string comand in $total.
What kind of loop? A loop controlled by the filenames, or by the count? Here's a loop controlled by a number:

Code:
n=15

while [ $n -gt 0 ]
do
   echo $n
   let n--
done
Perhaps a few more details about what you want to do?
 
Old 09-10-2009, 01:39 PM   #10
gqchynaboy
LQ Newbie
 
Registered: Jul 2003
Posts: 20

Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
What kind of loop? A loop controlled by the filenames, or by the count? Here's a loop controlled by a number:

Code:
n=15

while [ $n -gt 0 ]
do
   echo $n
   let n--
done
Perhaps a few more details about what you want to do?
I want to get the count of *.html files in a directory. And then use that count to iterate through all the .html files to perform a sed operation.

Thanks!
 
Old 09-10-2009, 08:27 PM   #11
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by gqchynaboy View Post
I want to get the count of *.html files in a directory. And then use that count to iterate through all the .html files to perform a sed operation.

Thanks!
Actually, counting the files, and operating on them, should probably be performed separately -- it's simpler and easier to understand.

You can create a list of .html files and move through the list, performing any operations you want. Also, listing the files located in a particular directory, and listing the files located in an entire directory tree, are different operations.

Here's an example that lists the .html files in the current directory and operates on them:

Code:
ls -1 *.html | while read line
do
   echo "do something to \"$line\" here."
done
This method is written to allow spaces in the file names. Note: the "-1" argument to the "ls" call is the number one, not a lowercase "L".

Now here's an example that lists all the .html files in a directory tree, but is otherwise the same as the first example:

Code:
path="/netbackup/data/Network/arachnoid"

find $path | grep "\.html$" | while read line
do
   echo "do something to \"$line\" here."
done
The path in this example happens to be the location of my local Website archive, just change it to suit your own needs. Unlike the first example, this one doesn't need to be run in the target directory, in fact it can be run from any location.

One more thing. Because of how "find" operates, the generated list will not be in any predictable order. If you need the names to be in alphabetical order, add a "sort" command to the stream:

Code:
path="/netbackup/data/Network/arachnoid"

find $path | grep "\.html$" | sort | while read line
do
   echo "do something to \"$line\" here."
done
HTH
 
Old 09-10-2009, 08:30 PM   #12
nathanpc
Member
 
Registered: Aug 2009
Location: Brazil
Distribution: Ubuntu Lucid
Posts: 45

Rep: Reputation: 15
Huh, all this discussion only about a simple thing, ow!
But i know that this is a simple thing, but when you want to get better of a command it gets more and more difficult to use!
 
Old 09-11-2009, 12:17 PM   #13
gqchynaboy
LQ Newbie
 
Registered: Jul 2003
Posts: 20

Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by lutusp View Post
Actually, counting the files, and operating on them, should probably be performed separately -- it's simpler and easier to understand.

You can create a list of .html files and move through the list, performing any operations you want. Also, listing the files located in a particular directory, and listing the files located in an entire directory tree, are different operations.

Here's an example that lists the .html files in the current directory and operates on them:

Code:
ls -1 *.html | while read line
do
   echo "do something to \"$line\" here."
done
This method is written to allow spaces in the file names. Note: the "-1" argument to the "ls" call is the number one, not a lowercase "L".

Now here's an example that lists all the .html files in a directory tree, but is otherwise the same as the first example:

Code:
path="/netbackup/data/Network/arachnoid"

find $path | grep "\.html$" | while read line
do
   echo "do something to \"$line\" here."
done
The path in this example happens to be the location of my local Website archive, just change it to suit your own needs. Unlike the first example, this one doesn't need to be run in the target directory, in fact it can be run from any location.

One more thing. Because of how "find" operates, the generated list will not be in any predictable order. If you need the names to be in alphabetical order, add a "sort" command to the stream:

Code:
path="/netbackup/data/Network/arachnoid"

find $path | grep "\.html$" | sort | while read line
do
   echo "do something to \"$line\" here."
done
HTH

The way you explained was so much easier then what I was trying to do, thank you very much!
 
Old 08-31-2011, 08:50 AM   #14
Phil168
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
minor correction

I just wanted to point out that the solution (#2) does not quite do what the author says, and will sometimes miscount
Quote:

Use a regular expression with grep:
Code:
Code:
ls -1R | grep .*.mp3 | wc -l
list all recursively one per line, piped through grep which will output only: any amount of characters, then a dot character then 'mp3' then piped through wc -l to count it.
Namely the dot is a wildcard in grep regular expressions (and * is the repetition) so this will count anything containing "mp3", rather than ".mp3". Thus if you store your mp3 files in a subdirectory of your music folder labelled mp3 (like I do), then you will miscount. To list only those with ".mp3" you need to use the escape character before the dot. For example
Code:
ls -1Rl | grep '\.mp3'  | wc -l
will count the files with ".mp3" somewhere (the ".*" is unneeded as grep looks at substrings anyway). Even better we could look for ".mp3" at the end of the line (via $) and ignore case (so .MP3 is counted too)
Code:
ls -1Rl | grep -i '\.mp3$'  | wc -l
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Maximum number of files in a directory neranjana General 14 05-13-2010 01:33 PM
finding number of files in a directory pantera Programming 2 08-20-2004 10:17 AM
Maximum Number of Directory Entries & Performance aig Linux - General 1 07-09-2004 07:36 AM
finding the number of files in each directory ridertech Programming 1 06-02-2004 10:26 PM
How do I map device serial number to its mount point directory? vagrawal Linux - Software 1 03-11-2004 12:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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