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 05-02-2003, 11:31 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
filecount in directory...


Hello,

I'm thinking this should be an easy one, but perhaps not. What I'm looking for is a command that will simply give me the total number of files in a directory in integer form.

I realise I could write a script to do this, but I a wondering if there is a command that will do it for me...
 
Old 05-03-2003, 02:36 AM   #2
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
Re: filecount in directory...

Quote:
Originally posted by bulliver
Hello,

I'm thinking this should be an easy one, but perhaps not. What I'm looking for is a command that will simply give me the total number of files in a directory in integer form.

I realise I could write a script to do this, but I a wondering if there is a command that will do it for me...
ls -l|wc -l
 
Old 05-03-2003, 02:47 AM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Perfect, thanks for your help.

I was thinking I would have to cat the filenames into a temporary file to use wc, just 404ing I guess. Love those pipes.

Thanks again.
 
Old 05-03-2003, 08:11 AM   #4
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
Quote:
Originally posted by bulliver
Perfect, thanks for your help.

I was thinking I would have to cat the filenames into a temporary file to use wc, just 404ing I guess. Love those pipes.

Thanks again.
got to admit that: that may not be what you are exactly seeking
ls -l|wc -l would print total number of files+directories(but not hidden)+1
ls -1|wc -l ditto
ls -la1|wc -l now the hidden files+directories are included but the problem is it outputs the no of files+directories+1(could you find out why this +1?)
ls -1al|grep -e -r -c ;ok now we are going somewhere ; but there are still problems it prints the files +directories number which have
-r in it(all files must have -r-------- at least but directories would have dr????????) the problem with this is that suppose you chmod
707 to a directory it still gets counted;(i know that it is possible with ls and grep to count your files but i will leave it to you; do a little search read man pages and maybe you will get a good one)
now let's try find
find solves all the problems
find . -type f -maxdepth 1|wc -l
it prints the number of all your files in the current directory
now as a side work maybe you could try a perl one liner;
let me give you a hint ; try man 3pm Find::File
and also read
man find2perl
keep posted

Last edited by rch; 05-03-2003 at 08:14 AM.
 
Old 05-03-2003, 08:10 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Your right, ls -l | wc -l gives me one too many.
ls -1 | wc -l works perfectly though...

It's just for a music directory playlist, there aren't any subdirectories so it will suit my purposes fine.

Much obliged...

btw, the +1 is because this:
Code:
total 1121364
is at the top when you ls -l, but isn't when you ls -1

Last edited by bulliver; 05-03-2003 at 08:13 PM.
 
Old 05-03-2003, 10:33 PM   #6
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
Quote:
Originally posted by bulliver
Your right, ls -l | wc -l gives me one too many.
ls -1 | wc -l works perfectly though...

It's just for a music directory playlist, there aren't any subdirectories so it will suit my purposes fine.

Much obliged...

btw, the +1 is because this:
Code:
total 1121364
is at the top when you ls -l, but isn't when you ls -1
nice work!
 
Old 05-03-2003, 11:05 PM   #7
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
lastly I had made another mistake which you didn't point out
i wrote man 3pm Find::File
but it should be File::Find
anyway i love perl
and i think it will be better that you write a simple perl one liner if you use the command frequently
as always perl is a little faster than using bash commands
 
  


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
Autozipping files from 1 directory & dropping them in other directory ??? amitsharma_26 Linux - Networking 5 10-22-2005 06:09 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM
How to rsynce the actual directory path instead of just the directory at the end of Niceman2005 Linux - General 5 04-08-2005 07:58 PM
write permissions for directory - not accidently move/deleted the directory linuxgamer Linux - Newbie 10 12-02-2003 03:04 AM

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

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