LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-01-2005, 12:13 AM   #1
malaka56
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Rep: Reputation: 15
how to list how many files in a directory?


hi, i know this is a stupid question, but i don't actually know a simple way to do this. especially resursively. thanks for helping out!
 
Old 09-01-2005, 12:34 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
This is really ghetto (counts lines in "ls") but might work:
Code:
ls -A | wc -l
Edit: I am sorry; did you want to know how to list the files in a directory, or count how many there are? This is for the latter.

Last edited by spooon; 09-01-2005 at 12:36 AM.
 
Old 09-01-2005, 12:34 AM   #3
sarajevo
Member
 
Registered: Apr 2005
Distribution: Debian, OpenBSD,Fedora,RedHat
Posts: 228
Blog Entries: 1

Rep: Reputation: 31
Try ls -l|more
read man ls
 
Old 09-01-2005, 02:09 AM   #4
malaka56
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 15
yeah, i just wanted to be able to count how many there are thats all. it seems like this is actually kind of difficult to do. especially if you have a complex directory structure. because it counts the directories also... hmm. there has to be a simple way, this is such a basic need. thanks for the help guys.
 
Old 09-01-2005, 02:51 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could use:
ls -la | grep '^-' | wc -l

The first character on the line will indicate the file type. A normal file will have a dash "-" character. If you want to count links, then use "grep '^[-l]'" which will include either regular files or links. However, symbolic links will start with an "l" character for both files and directories.

ls -la | grep '^[-l] | grep -v '\/$' | wc -l

This will exclude links to directories because they end with the forward slash character.

Or you could use the "--classify" option of ls so all directories have the "/" character appended to the name. This method will not count the "." and ".." entries.

ls -a --classify | grep -v '\/$' | wc -l

The "-v" option of grep will exclude lines that contain the patter. The pattern in this case is any line ending in a forward slash. Earlier, I used the pattern '^[-l]'. The "^" carat character in a pattern indicates the beginning of a line. The "$" character in a search pattern indicates the end of a line.
 
Old 09-01-2005, 03:15 AM   #6
malaka56
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 15
sweet.

i think "ls -laR | grep '^[-l]' | grep -v '\/$' | wc -l" works best for my purposes! thats a big help. im surprised its complex to do get something as simple as a directory file count. thanks!
 
Old 09-01-2005, 03:50 AM   #7
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Yikes...
I'd vote
Code:
find . -type f -print | wc -l
Groetjes,

Kees-Jan
 
Old 09-01-2005, 03:57 AM   #8
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Quote:
Originally posted by kees-jan
Code:
find . -type f -print | wc -l
that would be my weapon of choice aswell
 
Old 09-02-2005, 05:37 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
find . -type f -print | wc -l
A problem with the above script is that it will count files in subdirectories also. You need to use the "maxdepth" option.

Code:
find ./ -maxdepth 1 -type f | wc -l
In a similar vein, you might also want to use the "-d" option for the ls command if you are using wildcards, such as counting files starting with an "s"

However, for complicated searches, the "find" command works best.
You can even count the number of regular files, owned by a particular user, larger than a certain size, that was modified in the last 8 hours. OK, this example is a bit contrived, but searching your system for "suid" files is a very common use.



Last edited by jschiwal; 09-02-2005 at 05:47 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 12:08 PM
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 06:03 AM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
Missing List.* files in URPMI directory Ronw Linux - General 2 10-08-2003 03:57 AM
list only files in current directory xscousr Programming 8 09-22-2003 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:20 PM.

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