UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 28th, 2007, 08:57 PM
Superman859 Superman859 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 313 Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 51 m 59 sec
Reputation Power: 29
Another Easy One - How To Count Files In Directory?

Still learning UNIX...here is another easy question that I haven't been able to find the answer to.

How do you count or display the number of files in a directory? I know with ls -s you can find out the size in kb, but I'm curious to know the file count. I recently transferred a directory from another server and wanted to make sure the file count was the same on the new server.

Everything I can see with the ls command seems to deal with file sizes though, rather than how many there are.

Reply With Quote
  #2  
Old April 28th, 2007, 09:33 PM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 177 ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Days 21 h 34 m 32 sec
Reputation Power: 233
Code:
# ls -l | awk '!/^d/{print }' | wc -l

note this does not count hidden files. to count hidden files, you use the -a switch

Last edited by ghostdog74 : April 28th, 2007 at 09:36 PM.

Reply With Quote
  #3  
Old April 28th, 2007, 10:26 PM
SimonGreenhill's Avatar
SimonGreenhill SimonGreenhill is offline
(retired)
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Dec 2003
Location: The Laboratory
Posts: 10,101 SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 4925 Folding Title: Novice Folder
Time spent in forums: 3 Months 3 Weeks 6 h 8 m 34 sec
Reputation Power: 0
Facebook
Quote:
# ls -l | awk '!/^d/{print }' | wc -l



Eek! Try this:

Code:
ls -1 | wc -l


Which means:
ls: list files in dir
-1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
| : pipe output onto...
wc: "wordcount"
-l: (that's an "L" for Linux), count lines.

--Simon

Reply With Quote
  #4  
Old April 28th, 2007, 11:14 PM
Superman859 Superman859 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 313 Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 51 m 59 sec
Reputation Power: 29
Thanks.

By the way - I've run into these problems a few times when seeing commands and reading them in the book I'm reading (although it usually explains which to pick)...

What is the best way to know if you are supposed to type a 1 or an l, or an O or an 0?

A lot of the UNIX stuff is written in monotype, as that's how it is displayed. The only unfortunate thing is they all seem to look the same in monotype. When reading man pages or trying to use flags, it's very hard to tell and can be frustrating.

Is there a good way to get around this? Any tips or advice? If I type the wrong one by accident should I worry I will wreak havoc somehow?

Reply With Quote
  #5  
Old April 28th, 2007, 11:58 PM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 177 ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Days 21 h 34 m 32 sec
Reputation Power: 233
Quote:
Originally Posted by SimonGreenhill
Eek! Try this:

Code:
ls -1 | wc -l


Which means:
ls: list files in dir
-1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
| : pipe output onto...
wc: "wordcount"
-l: (that's an "L" for Linux), count lines.

--Simon

well, i have thought of that too..but OP did not indicate he wants to count subdirectories. ls will list subdirectories too...unless of course OP wants to include subdirectories

Reply With Quote
  #6  
Old April 29th, 2007, 12:00 AM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 177 ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level)ghostdog74 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Days 21 h 34 m 32 sec
Reputation Power: 233
Quote:
Originally Posted by Superman859
Thanks.

By the way - I've run into these problems a few times when seeing commands and reading them in the book I'm reading (although it usually explains which to pick)...

What is the best way to know if you are supposed to type a 1 or an l, or an O or an 0?

A lot of the UNIX stuff is written in monotype, as that's how it is displayed. The only unfortunate thing is they all seem to look the same in monotype. When reading man pages or trying to use flags, it's very hard to tell and can be frustrating.

Is there a good way to get around this? Any tips or advice? If I type the wrong one by accident should I worry I will wreak havoc somehow?

-l will include the "total" line. so its best to just ls or ls -1.

Reply With Quote
  #7  
Old April 29th, 2007, 10:41 AM
Superman859 Superman859 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 313 Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 51 m 59 sec
Reputation Power: 29
Yes, I've learned ls -l . However, it took me a few moments to figure out which one to use. If I haven't previously learned it am in the processing of learning it, I have trouble knowing which it is.

So it's mainly for all the new stuff I'm learning.

Reply With Quote
  #8  
Old April 30th, 2007, 03:07 AM
zby zby is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Prague, Czech Republic
Posts: 867 zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 23 h 41 m 23 sec
Reputation Power: 69
Using ls is simple and fast, but you may try to count files or directories recursively. The you'd better use find which includes even the hidden files as a default. With ls you have to use ls -la .....
Code:
[zby@DDFF Text-PDF-0.29]$ find . -type d 
.
./scripts
./blib
./blib/man3
./blib/script
./blib/lib
./blib/lib/auto
./blib/lib/auto/Text
./blib/lib/auto/Text/PDF
./blib/lib/Text
./blib/lib/Text/PDF
./blib/arch
./blib/arch/auto
./blib/arch/auto/Text
./blib/arch/auto/Text/PDF
./lib
./lib/Text
./lib/Text/PDF
./examples
[zby@DDFF Text-PDF-0.29]$ find . -type d |wc -l
19
[zby@DDFF Text-PDF-0.29]$ 
[zby@DDFF Text-PDF-0.29]$ 
[zby@DDFF Text-PDF-0.29]$ find . -type f |wc -l
84
[zby@DDFF Text-PDF-0.29]$ 

Reply With Quote
  #9  
Old April 30th, 2007, 06:54 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
And just to prove there are N+1 ways of doing things in Unix, given N programmers ...

To count JUST files in immediate directory
Code:
ls -1p | grep -vc "/$"

The "/$" in the grep is looking for lines ending in /, which the -p flag of ls will put in for directories. The -v flag of the grep will invert the search, so only lines NOT ending in / (i.e., all non directories) will be listed, and the -c of the grep means count.
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #10  
Old February 24th, 2011, 03:57 AM
Denso Denso is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 1 Denso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 51 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
And just to prove there are N+1 ways of doing things in Unix, given N programmers ...

To count JUST files in immediate directory
Code:
ls -1p | grep -vc "/$"

The "/$" in the grep is looking for lines ending in /, which the -p flag of ls will put in for directories. The -v flag of the grep will invert the search, so only lines NOT ending in / (i.e., all non directories) will be listed, and the -c of the grep means count.


Hi Simon i was looking this command for a long time
i m not a unix user but just need to do something with it.

here is my problem ;
i need to display the file names one by one order by date,
i mean i m using BO Data Integrator tool to get files name from unix.And i have a loop in BODI tool,

my loop does this ;
1. count files in directory ( to a variable : $count)
2. get file name one by one ( to a variable : $filename)

suppose you have 3 files in that directory
/home/dummy.csv
/home/dummy.xls
/home/dummy.txt

i need a command like (its just my imagenation );
:> getfilename(1)
dummy.csv
:> getfilename(2)
dummy.xls
:> getfilename(3)
/home/dummy.txt

thats what i need
i hope i could explain detaily
Thx

Reply With Quote
  #11  
Old February 24th, 2011, 07:02 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Well, the command been here for a long time, too!

So, you have a need to list files one by one ... ok, almost got that: we are counting them with the ls command shown (using the "c" option) so we just drop that. By date ... ok, add in the "t" option (depending on which order you want them listed you may also wish to use the "r" option to reverse the sort order".
What we end up with is:
Code:
ls -1pt | grep -v "/$"

We then get to what you say next, the ability to "pluck out" the n-th placed filename. Well, we can do that too ...
Code:
# Name: getfilename.sh
# Desc: Return n-th placed file in current directory
#

# Check we have only one, numeric and positive, parameter
if [ $# -eq 1 ]
then
  numx=$(echo "$1" | tr -d "[0-9]")
  if [ "$numx" != "" ]
  then
    echo "Parameter must be numeric"
    exit 3
  else
    num=$1
    nfiles=$(ls -1p | grep -vc "/$")
    if [ $num -lt 1 -o $num -gt $nfiles ]
    then
      echo "Parameter out of range, must be between 1 and $nfiles"
      exit 4
    fi
  fi
else
  echo "Must specify just one parameter"
  exit 2
fi

# Parm is number and in range if we get here

ls -1pt | grep -v "/$" | awk -vR=$num '{if (NR == R) {print; exit}}'

Reply With Quote
  #12  
Old January 15th, 2012, 02:38 PM
TPUT CUP TPUT CUP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 4 TPUT CUP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 22 sec
Reputation Power: 0
how to create script that counts hidden and unhidden files

Create a script called countfiles that accepts the following parameters and their specific actions:
-h it displays the total number of hidden files and the
absolute path of the directory where the script is executed.

-u it displays the total number of unhidden files on the directory where the
script is created and the absolute path of the directory where the script
is executed.

By default (no parameters), the script should show the total
number of hidden files, the total number of unhidden files in the directory where it is executed and the sum total both.

SAMPLE RUN:

THE DIRECTORY WHERE THE SCRIPT IS RUN:

total number of hidden files: 13
total number of unhidden files in this directory: 124
=====
Sum total: 137

(please help me)

Reply With Quote
  #13  
Old January 16th, 2012, 05:22 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Looks like homework ...
But, we can steer and guide, maybe!
First things first - what is your defintion of a hidden file?
Next - what have you tried, thus far?
Next - have you managed to create a 'skeleton' script that accepts, checks and prcesses the the parameters correctly, but does not actually do any file counting?

Reply With Quote
  #14  
Old January 16th, 2012, 12:40 PM
TPUT CUP TPUT CUP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 4 TPUT CUP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 22 sec
Reputation Power: 0
For me, a hidden file begins with a period and when a file is hidden it can not been seen with the bare ls command or an un-configured file manager. and I've tried the script but
it's not actually that completely working. Also, I have not yet tried to do some 'skeleton' script.

please help....

Reply With Quote
  #15  
Old January 16th, 2012, 08:43 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Before we start, this being the Unix forum and not the Linux forum I am not going to assume you are using any specific shell, so we shall start our questions with that:

1) What shell are you going to be using, on what OS platform?

2) Do you know how to check the parameters passed to a script?

3) Is there a deadline to you doing this?

4) Do you want to know how to do this yourself or do you just have wish to have a script dumped in your lap without having a clue what it does nor how?

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Another Easy One - How To Count Files In Directory?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap