LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 12-13-2006, 07:53 AM   #1
cnm
Member
 
Registered: Nov 2006
Distribution: RHEL 4
Posts: 34

Rep: Reputation: 15
How to find the total number of files in a folder


How can i find the number of files in a perticular folder?

Aftre copying an etire folder is there a way to conform that all the files are coppied?
 
Old 12-13-2006, 08:10 AM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
This will tell you the number of files in a directory.

ls | wc -l
 
1 members found this post helpful.
Old 12-13-2006, 08:34 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by fordeck
This will tell you the number of files in a directory.

ls | wc -l
Actually, I think that will give you the number of files and directories in a directory
 
1 members found this post helpful.
Old 12-13-2006, 09:04 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
A directory is a file but its not a regular file, so the question is not clear enough
If he does not want directory, then something awfull like this should do:
echo $(($(ls -l | grep -v ^d | wc -l)-1))
 
Old 12-13-2006, 09:04 AM   #5
Broder
Member
 
Registered: Oct 2006
Location: Santa Barbara, C.A.
Distribution: Mood dependent
Posts: 117

Rep: Reputation: 15
That's the only way I would know to do it either, there must be a way of counting just files though. It'd be nice to get an output like:
13 files
4 directories
2 symbolic links


You could write a little script to do that cnm and post it up for us! :P

If they have the same extension you can count using:

ls *.txt | wc

You can use the diffcommand to compare two directories if you just want to ensure that it copied successfully.
 
Old 12-13-2006, 09:06 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Quote:
Originally Posted by Feelda
13 files
4 directories
2 symbolic links

Code:
ls -l | grep ^- | wc -l
ls -l | grep ^d | wc -l
ls -l | grep ^l | wc -l

Last edited by nx5000; 12-13-2006 at 09:07 AM.
 
Old 12-13-2006, 09:46 AM   #7
Broder
Member
 
Registered: Oct 2006
Location: Santa Barbara, C.A.
Distribution: Mood dependent
Posts: 117

Rep: Reputation: 15
*******************

Last edited by Broder; 03-13-2007 at 11:54 AM.
 
Old 12-13-2006, 05:33 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
An alternative is to use the find cmd, using -type option:

-type c
File is of type c:

b block (buffered) special

c character (unbuffered) special

d directory

p named pipe (FIFO)

f regular file

l symbolic link

s socket
 
Old 12-14-2006, 03:40 AM   #9
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Actually that's true, the grep doesn't work for regular files that have certain properties (suid,...) so here's a better one:

Code:
for t in files links directories; do echo `find . -type ${t:0:1} | wc -l` $t; done 2> /dev/null
If you don't want to recurse directories (only count files in the current dir):

Code:
for t in files links directories; do echo `find . -maxdepth 1 -type ${t:0:1} | wc -l` $t; done 2> /dev/null
Result:

655 files
6 links
213 directories

And directory count begins at 1 because . is counted as a directory.
 
Old 12-14-2006, 07:33 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
A small command line perl program to do it:
Code:
ls -l |perl -e 'while(<>){$h{substr($_,0,1)}+=1;} END {foreach(keys %h){print "$_ $h{$_}\n";}}'

Last edited by matthewg42; 12-14-2006 at 07:34 AM.
 
Old 12-14-2007, 06:21 AM   #11
thamba
LQ Newbie
 
Registered: Dec 2007
Posts: 1

Rep: Reputation: 4
Use the 'tree' command. If its not present in the system by default, I recommend installing it.
It outputs a beautiful tree structure showing the files and the directories and finally printing number of files and directories in a folder.
 
1 members found this post helpful.
Old 03-05-2009, 10:32 PM   #12
mrampton
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
I did this by:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

which will return the number of files in the directory + 1 (the directory itself).
 
Old 10-28-2009, 08:45 AM   #13
sumeet inani
Member
 
Registered: Oct 2008
Posts: 881
Blog Entries: 26

Rep: Reputation: 47
how to find number of files,subfolders in a file using command

I have taken clue from previous post & come up with simplest way

Finding number of files in a folder
find . -type f | wc -l
this means output of find comand (which is only files no directories)is sent to wc which counts number of lines i.e number of files

Finding number of subfolders in a folder
(1)find . -type d | wc -l
This will give number of subfolders+1 because current folder also gets included.Note that the folders inside subfolders are also included in count.
(2)If you want to see only number of folders in current folder then
find . -maxdepth 1 -type d | wc -l
Here output is numberof subfolders+1
NOTE:folders inside subfolders not considered.

If this information helps you please thank me by pressing thumbs up button.Happy hacking.

Last edited by sumeet inani; 10-28-2009 at 08:53 AM.
 
2 members found this post helpful.
Old 10-28-2009, 08:51 PM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Quote:
grep doesn't work for regular files that have certain properties (suid,
chmod u+s (set suid) affects the user's 'x' permission char, not the file type char ie first char in ls -l
 
Old 12-16-2010, 03:59 PM   #15
dylan_ec
LQ Newbie
 
Registered: Dec 2010
Distribution: OpenSUSE 11.3
Posts: 2

Rep: Reputation: 0
How to find the total number of files in a folder

You can use the command

find . -type f | wc -l

=)
 
  


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
How to find grand total size of all files in a directory fakhrul Red Hat 4 04-17-2010 12:28 AM
find the total number of pseudo terminals of all logins RajRed Linux - General 5 04-18-2006 04:30 AM
Find 100 largest files in folder tree tyreth Linux - General 2 02-07-2006 08:18 AM
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
Command to list total number of files. WillieB_72 Linux - General 3 01-29-2003 09:25 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration