LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-22-2010, 05:57 AM   #1
saranraj
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Rep: Reputation: 0
directory list count


Hi i need to know how to find number of files in a directory? is there any system calls in fedora 12.And i need to know how to perform a operation if the that count increases by one?
Please help me..
Thanks in advance...
 
Old 02-22-2010, 06:19 AM   #2
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
how to find number of files in a directory?
Try the following command:
Code:
ls | wc -l
Have a look here:
http://dsl.org/cookbook/cookbook_10.html

Quote:
And i need to know how to perform a operation if the that count increases by one?
You may need to write some script for doing that !

Last edited by TheIndependentAquarius; 02-22-2010 at 06:23 AM.
 
Old 02-22-2010, 06:22 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
You can use simple bash commands as follows:
Code:
count=`ls | wc -l`
echo $count
Now, count contains the total no. of files under a directory.
Now, I think you can write your script to perform some operations based on this count variable.
 
Old 02-22-2010, 06:39 AM   #4
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 34
Code:
find . -maxdepth 1 -type f | wc -l
This will count files only in a current directory. Directories, symlinks, devices, etc. are filtered out.
 
1 members found this post helpful.
Old 02-23-2010, 12:37 AM   #5
saranraj
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Original Poster
Rep: Reputation: 0
hi friends, all answers relating to count the no of files, is ok. but if i write a script to do a operation every time i need to run that script to check file count. But i need a solution that it automatically detect and run that script program when count increases by one.
Thanks for help...
 
Old 02-23-2010, 01:08 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Upto what extent you have tried for this?
It's better if you present us the code what you have tried till now
 
Old 02-23-2010, 01:16 AM   #7
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by saranraj View Post
hi friends, all answers relating to count the no of files, is ok. but if i write a script to do a operation every time i need to run that script to check file count. But i need a solution that it automatically detect and run that script program when count increases by one.
Thanks for help...
I did not reply you yesterday assuming you need some system call to find the directories count. Now I understood you problem.

Still one thing is not clear.

Are you focusing on a single directory where files are created and count needs to be incremented by 1 when a new file is created ? OR

Are you keeping vigil on a directory tree hierarchy ?

Last edited by vinaytp; 02-23-2010 at 01:17 AM.
 
Old 02-23-2010, 01:23 AM   #8
vlrk
Member
 
Registered: Dec 2008
Posts: 36

Rep: Reputation: 1
i think Cron job would also work for you
 
Old 02-23-2010, 01:23 AM   #9
catkin
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
Have a look at inotify
 
1 members found this post helpful.
Old 02-23-2010, 01:40 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Or dnotify http://linux.die.net/man/1/dnotify
 
1 members found this post helpful.
Old 02-23-2010, 01:57 AM   #11
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by chrism01 View Post
Dear Chrism01,

I am using fedora 11 and RHEL 5.3. I couldn't find dnotify command in any of these distors.

Does this utility exists in Debian family of distros ?

Could you please tell us which package provides us dnotify capability in Redhat based distros?

Last edited by vinaytp; 02-23-2010 at 01:59 AM.
 
Old 02-23-2010, 02:16 AM   #12
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by vinaytp
Could you please tell us which package provides us dnotify capability in Redhat based distros?
inotify, is a dnotify replacement.

do man inotify

See here also:
http://www.ibm.com/developerworks/li...l-inotify.html

Last edited by TheIndependentAquarius; 02-23-2010 at 02:19 AM.
 
Old 02-23-2010, 02:44 AM   #13
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by anishakaul View Post
inotify, is a dnotify replacement.

do man inotify

See here also:
http://www.ibm.com/developerworks/li...l-inotify.html
wow..inotify is a system call, I thought inotify is a command.

Last edited by vinaytp; 02-23-2010 at 02:52 AM.
 
Old 02-23-2010, 03:10 AM   #14
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by Vookimedlo
Code:

find . -maxdepth 1 -type f | wc -l

This will count files only in a current directory. Directories, symlinks, devices, etc. are filtered out.
Well, thanks for that useful and interesting information
 
Old 02-23-2010, 03:57 AM   #15
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
If you want live monitoring and not a periodic check based one then inotify is the only way to go.

Whether to use a system call or inotify-tools from command line will depend on how you are designing your solution (are you using C or something like that or scripting?). In any case you will need a kernel that's recent enough and has inotify support. Anything that's not from paleolithic will fulfill that requisite.

With the inotify command line tools you could do something like:

Code:
inotifywait --monitor /tmp/ | while read foo; do echo "$foo"; done
You can monitor for specific events and then do whatever you want with "$foo" inside the loop. Check the inotifywait man page for further details, ask back when/if you have a clear idea of what exactly do you want if you need more help.

You will need to install inotify-tools, the name of the package can vary from distro to distro.
 
  


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
Rsync display count of files during list building sadarax General 2 10-05-2009 11:32 PM
How to get list or count of unleased ips from dhcpd3? matiasquestions Linux - Networking 2 07-24-2008 03:57 AM
Need to count contents of a directory shipon_97 Linux - Newbie 9 08-17-2007 01:31 AM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 07:22 AM
how to get count of files in a directory? hongxing Linux - Software 2 11-10-2005 10:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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