LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 09-13-2005, 06:56 PM   #1
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Rep: Reputation: 15
how many files are in a directory?


i would like to know ot find out how many files are in a directory?
is there away to do this without using

>ls | wc -w

the reason why is because i don't want to count folders that may be in the directory.

Another one i woul like to find out about is how list a directory and only return the file that has been modified?
 
Old 09-13-2005, 07:13 PM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 376Reputation: 376Reputation: 376Reputation: 376
Re: how many files are in a directory?

Quote:
Originally posted by bhar0761
i would like to know ot find out how many files are in a directory?
is there away to do this without using

>ls | wc -w

the reason why is because i don't want to count folders that may be in the directory.
i'm sure there's a simpler way to do it, but here's what i could come-up with:
Code:
ls -l | grep -v ^d | grep -v ^total | wc -l
just my ...


 
Old 09-13-2005, 07:31 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
There is usually several ways to do the same thing.

echo $(( `ls | wc -l` - `ls -d */`| wc -l ))

This subtracts the number of directories from the total.
 
Old 09-13-2005, 07:33 PM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 376Reputation: 376Reputation: 376Reputation: 376
Re: how many files are in a directory?

Quote:
Originally posted by bhar0761
Another one i woul like to find out about is how list a directory and only return the file that has been modified?
the find command does all of this kinda stuff, but AFAIK you need something to compare to - something like a date, a time, or a file... check out the man page... you can tell it to find any files that have been modified during the last 3 hours, for example... or you can find any files modified after the modification time of file X (find will look at file X itself)...

you could also use a list of md5sums and make a script to run md5sum on all the files and only list those with different md5sums... it's just a thought...
 
Old 09-13-2005, 07:45 PM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 376Reputation: 376Reputation: 376Reputation: 376
Quote:
Originally posted by jschiwal
There is usually several ways to do the same thing.

echo $(( `ls | wc -l` - `ls -d */`| wc -l ))

This subtracts the number of directories from the total.
awesome, i didn't even know ls had a "-d" option, jeje...

BTW, there's a small typo in your commands, the little thinggy that makes the shell execute the command, i forgot what it's called... anyways, you accidentally placed it after the second ls but it goes after the second wc:
Code:
echo $(( `ls | wc -l` - `ls -d */ | wc -l` ))
 
Old 09-13-2005, 07:49 PM   #6
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 62
I like that command ls -l | grep -v ^d | grep -v ^total | wc -l

To make this easier for use later edit your .bash_profile in your home directory.
Add a line like this to the bottom
alias lsnfo="ls -l | grep -v ^d | grep -v ^total | wc -l"

lsfo short for list number files only

Logout then back in. Now in the terminal type lsnfo on the command line it will run the command ' ls -l | grep -v ^d | grep -v ^total | wc -l '

Thanks for the command there win32sux.
Brian1
 
Old 09-13-2005, 07:55 PM   #7
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 376Reputation: 376Reputation: 376Reputation: 376
Quote:
Originally posted by Brian1
Thanks for the command there win32sux.
you're very welcome... thank you for the alias tip...
 
Old 09-13-2005, 08:18 PM   #8
bhar0761
Member
 
Registered: Jul 2005
Location: San Francisco
Distribution: Fedora Core 6
Posts: 64

Original Poster
Rep: Reputation: 15
thank you all for your replies
 
Old 09-23-2005, 09:56 AM   #9
Mrcdm
Member
 
Registered: Apr 2003
Location: Australia
Distribution: Debian 3, 31r0, 4, slackware, DSL, RH8.0/7, MDK9/10, et al. Vista is cute but not Linux - I tried
Posts: 70

Rep: Reputation: 15
Just in case the original poster was interested, these commands don't include hidden files. If you need to take that into account use -a option to ls.
 
Old 09-24-2005, 12:49 AM   #10
stickman
Senior Member
 
Registered: Sep 2002
Location: Nashville, TN
Posts: 1,552

Rep: Reputation: 53
You could also use the find command if you are interested in counting files in subfolders. You also have the ability to search based on modification or access time.
 
  


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
Autozipping files from 1 directory & dropping them in other directory ??? amitsharma_26 Linux - Networking 5 10-22-2005 07:09 AM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 12:51 PM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 11:55 PM
home directory files gone, hidden files remain Grasshopper Linux - Security 12 04-10-2005 09:23 PM
How to filter files in files and files which are in a sub-directory with "grep"? Piero Linux - Newbie 9 08-29-2003 03:38 AM


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