LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how many files are in a directory? (https://www.linuxquestions.org/questions/linux-newbie-8/how-many-files-are-in-a-directory-363082/)

bhar0761 09-13-2005 05:56 PM

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?

win32sux 09-13-2005 06:13 PM

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 :twocents:...



jschiwal 09-13-2005 06:31 PM

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.

win32sux 09-13-2005 06:33 PM

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...

win32sux 09-13-2005 06:45 PM

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` ))

Brian1 09-13-2005 06:49 PM

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

win32sux 09-13-2005 06:55 PM

Quote:

Originally posted by Brian1
Thanks for the command there win32sux.
you're very welcome... thank you for the alias tip... ;)

bhar0761 09-13-2005 07:18 PM

thank you all for your replies

Mrcdm 09-23-2005 08:56 AM

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.

stickman 09-23-2005 11:49 PM

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.


All times are GMT -5. The time now is 06:16 PM.