count of files

How to count the number of files in a directory.

Thanks
Mahalakshmi.A

I think this had been answered many times

only the count of files and not directories

ls -l | grep -c ^-

not sure if this is efficient tho

ls -l | grep -v drw | awk '{ print $9 }' | wc -l

you are assuming r and w perm for users if its a directory,
there need not be such a perm for user.

it could be even dr-x

in that case, your condition would fail to filter

Thanks for the replies which I had got.
Can you please explain me about awk '{print $9}' in ls -l | grep -v drw | awk '{ print $9 }' | wc -l

Thanks
Maha

hi,

grep -v use filter off anything that starts with drw like directories generally start with drw

wc is word count, -l used to display number of lines

awk '{ print $9 }' prints out the 9th column of the output

when you do a ls -l it prints out some file with

col1 col2 col3 col4 col5 col6 col7 col8 col9
-rw-r--r-- 1 root other 151 Nov 15 14:28 xx
drw-r--r-- 1 oracle dba 72 Nov 15 14:36 apple

enjoy

ls -l | grep -v drw | awk '{ print $9 }'

above command shows the 9th column of the grep o/p.

ex:

suppose o/p of the above grep command

-rwxr-xr-x    1 sri    sri         317 Sep 20 16:18 get1

then o/p of awk command would be

get1

Thanks for the replies now I have started learning UNIX ... Day by day I shd improve.

I have another doubt

find /home/psv/datasources/ -type f -print > file_name

this file_name which I am specifying is stored as a new file. Can I store the output of the find command in a variable.

/usr/bin/python -c "import os; print len( [f for f in os.listdir(os.getcwd()) if os.path.isfile(f) ])"
var=`find . -name '*.*' -print`
echo $var

Hi All,

When i do ls -l on my current directory, there is an entry as follows.
-rw-r--r-- 1 sabraham bcc 0 Nov 17 07:58 0

Is this a system generated entry?

When i executed ls -l|grep -c ^- the resultant no of files is 1 less than the result of the command ls -l|grep -v d-w|awk '{ print $9}' |wc -l.
I assume that the result specified in the beginning is a directory and this attributes to the difference.

Any help on this would be appreciated.

using grep -v d-w

will include the
1) total tag and
2) directory for which owners-read permission is set ( drw is not equal to d-w )

hence the difference

What does this tag mean? When i execute the same with drw, d-w, dr- always i'm getting the same result. Can u please explain in detail.

Thanks in advance
Sumesh

you need to go with grep -v ^d ( to remove dirs from the list )