Get the file count in a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the file count in a folder
# 1  
Old 03-25-2011
Get the file count in a folder

What is the best way to get the file count (Including the subdirectories) under a folder?
# 2  
Old 03-25-2011
One way would be..
Code:
find . -type f |wc -l

# 3  
Old 03-25-2011
Hi un1xl0ver_rwx,

Another way could be:
Code:
ls -lR | awk '$7~/:/ && $1!~/^d/{print}' | wc -l


But the outputs are different between:

Code:
find . -type f | wc -l

and

Code:
ls -lR | awk '$7~/:/ && $1!~/^d/{print}' | wc -l

I compare and I found that when I use
Code:
"find . -type f

appear extra lines with
Code:
Some/Path/.exists

in empty folders and this count like another file when really doesn't exist.

Why does
this happen?

Regards
This User Gave Thanks to cgkmal For This Post:
# 4  
Old 03-25-2011
@cgkmal, why would you want to count files using ls -lR? your pattern "$1!~/^d/{print}" will also count other types such as sockets, pipes etc....
# 5  
Old 03-25-2011
Code:
find Commands|wc -l

is what I needed.

Thanks!
# 6  
Old 03-25-2011
Quote:
Originally Posted by kurumi
@cgkmal, why would you want to count files using ls -lR? your pattern "$1!~/^d/{print}" will also count other types such as sockets, pipes etc....
Hi Kurumi,

In order to learn, may you show how it looks a socket, pipe and other different elements in a ls -lR print?

Regarding to your question:
Well, when I see the print of ls -lR, prints all files in a folder and its subfolders showing this structure:

Code:
./Some folder:
total 20
drwxr-xr-x 8 user user  4096 2011-03-17 20:13 Xlib
-rw-r--r-- 1  user user   948 2005-09-30 12:57 Changes

As far as I know, in this print out format, lines beginning with "d" are representing directories and the other would be files.
Being newbie in unix I don't know completely how it work linux systems, what you say is new for me, and I'll take present, thanks for that.

But actually using my code (in my case), the result (205 files) is less than find command's output (220).

I see is because "find . -type f" is adding "some path/.exists" lines like ls -lRa does.

Maybe somebody knows?

Regards.

Last edited by cgkmal; 03-25-2011 at 04:24 AM..
# 7  
Old 03-25-2011
Use this command this is the fastest , even faster than find.

Code:
 ls -lR . | egrep -c '^-'

The reason for difference is output in find and ls is ignoring hidden files.
These 2 Users Gave Thanks to dinjo_jo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Searching for file types by count in specific folder in RHEL 6

So I'm trying to search for the top 10 or 15 items under a directory by file type. I want to run a command on a directory and get something like the following: Example of expected output.. .PDF: 100, .txt: 95, .word: 80.. What would be the best way of going about this? I've searched around... (2 Replies)
Discussion started by: shackle101
2 Replies

2. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

3. Shell Programming and Scripting

want to move set of file from one folder to another folder

Hi all, let me explain my requirments i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead each 5 folders contain 15 files i want to move 10 files to some other folder, remain 5 files should be there in the same folder. give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies

4. UNIX for Dummies Questions & Answers

Separate text files in a folder by word count

Hi, been searching Google for like an hour and I doubt I got the right keywords so might as well ask here. What I need: Before: Article1.txt 564 Article2.txt 799 Article3.txt 349 Article4.txt 452 * Separate files over 400 wordcount * After: Article1.txt 564... (3 Replies)
Discussion started by: Richard2000
3 Replies

5. UNIX for Dummies Questions & Answers

How to obtain a count of files in a folder and it's subfolders

First of all, the extent of my unix knowledge is next to nil. I've been able to telnet to a unix box, and thanks to the Computer Hope website, I've been able to learn a few basic commands to navigate from folder to folder, and view contents. What I really need to do is obtain a count of all... (2 Replies)
Discussion started by: scarfinv
2 Replies

6. Shell Programming and Scripting

Count matching filenames in a folder

Hi all, I have 4 files for example named abc01012009.txt abc02012009.txt abc03012009.txt abc04012009.txt in a folder. I would like to firstly backup the latest file available, in this case, the latest date available, abc04012009.txt to its subfolder named backup, and then rename the... (4 Replies)
Discussion started by: tententen
4 Replies

7. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

8. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

9. UNIX for Dummies Questions & Answers

command to get count in unzipped folder

Hi All, Is there is a way to do this in a single unix command I have unzipped some files into a directory .. I want to check the count of the files in the directory and also i need to check whether the files is having read permision or not.. I know it can be done by a script isthere... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question