The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
Another Easy One - How To Count Files In Directory?
Discuss Another Easy One - How To Count Files In Directory? in the UNIX Help forum on Dev Shed. Another Easy One - How To Count Files In Directory? UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 28th, 2007, 08:57 PM
|
|
|
|
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.
|

April 28th, 2007, 09:33 PM
|
|
|
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.
|

April 28th, 2007, 10:26 PM
|
 |
(retired)
|
|
Join Date: Dec 2003
Location: The Laboratory
|
|
Quote: | # ls -l | awk '!/^d/{print }' | wc -l |
Eek! Try this:
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
|

April 28th, 2007, 11:14 PM
|
|
|
|
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?
|

April 28th, 2007, 11:58 PM
|
|
|
Quote: | Originally Posted by SimonGreenhill Eek! Try this:
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 
|

April 29th, 2007, 12:00 AM
|
|
|
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.
|

April 29th, 2007, 10:41 AM
|
|
|
|
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.
|

April 30th, 2007, 03:07 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Location: Prague, Czech Republic
|
|
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]$
|

April 30th, 2007, 06:54 AM
|
|
|
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
|

February 24th, 2011, 03:57 AM
|
|
Registered User
|
|
Join Date: Feb 2011
Posts: 1
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
|

February 24th, 2011, 07:02 AM
|
|
|
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}}'
|

January 15th, 2012, 02:38 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 4
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)
|

January 16th, 2012, 05:22 AM
|
|
|
|
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?
|

January 16th, 2012, 12:40 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 4
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....
|

January 16th, 2012, 08:43 PM
|
|
|
|
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?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|