Hi All,
How do i find out the number of files in a directory using unix command ?
Hi All,
How do i find out the number of files in a directory using unix command ?
ls -l . | egrep -c '^-'
Hi vgersh99,
Thanks it works!
If i want to find the number of *sorts file in a directory, what would be the modification needed to the code?
ls -l * | grep '^-' | grep sorts | wc -l
ls -l *sorts | awk '/^-/{c++}END{ print c }'
Hi Lorcan,
Thanks but if there's no *sorts file in the directory, the output will be reflected below. Is there any way i can remove the "No match" output, leaving only the "0" as the output ?
$ ls -l *sorts | wc -l
No match
0
ls -l *sorts 2>/dev/null | awk '/^-/{c++}END{ print c }'
Hi Matrix,
Some error encountered when using yr code.
$ ls -l *sorts 2>/dev/null | awk '/^-/{c++}END{ print c }'
Ambiguous output redirect
-rw-r--r-- 1 user grp 0 Aug 16 14:34 asorts
-rw-r--r-- 1 user grp 0 Aug 16 14:34 bsorts
drwxr-xr-x 2 user grp 4096 Aug 16 14:35 dsorts/
ls -l *sorts | awk '/^-/{c++}END{ print c}'
2
only count of files [*sorts] is displayed
find /path -maxdepth 1 -type f -name "*sorts" | wc -l
hi ghostdog,
Some error found when using yr code
$ find /path -maxdepth 1 -type f -name "*sorts" | wc -l
find: bad option -maxdepth
find: path-list predicate-list
0
What's your OS version
This is the simple option.
ls -l "directory name" | grep "^-" | wc -l
or
ls -l . | grep "^-" | wc -l
0r
ls -l * | grep "^-" wc -l
or generic could be like this in shell script--
#!/usr/bin/ksh
echo "display the list of files"
ls -ltr
echo "Read the directory in which you want to count the number of files"
read dir
ls -l $dir | grep "^-" | wc -l
first, make sure you have /path as a directory?? if not substitute ..however if you don't have the maxdepth option, you can use prune, so that it don't traverse subdirectories (if that's what you want.)
# find * -type f -name "*sorts" -print -o -type d -prune |wc -l
Hi namishtiwari,
Thanks your code works.
Thanks to all others who have given me inputs as well.
Really appreciate that.!!