Total number of files in the folder should be listed

Hi All,

When i give the ls -lrt to list out all files with total number of files , i get the output as

ls -lrt
total 72
-rw-r--r--    1 hari  staff           796 Jul 11 09:17 va.txt
-rw-r--r--    1 hari  staff           169 Jul 13 00:20 a.log
-rwxr-xr-x    1 hari  staff           659 Aug 10 04:45 final.sh
-rw-r--r--    1 hari  staff            98 Aug 10 04:48 noprob.txt
-rw-r--r--    1 hari  staff          3597 Aug 10 04:48 main.xls
-rw-r--r--    1 hari  staff           299 Aug 10 04:48 mailbody.txt
-rw-r--r--    1 hari  staff          4981 Aug 10 04:48 attachment.txt

Now our expectation is we should get the "total" number of files with only the filenameswhich was at the end like va.txt,a.log,final.sh etc.

I tried with ls -lrt|grep total to get the total line but am unable to get only the filenames by leaving the permission, user name, group, date and all.. Am requesting some help to complete this task as you were all in the part of my previous automations. Kindly do the needful friends.

try sth like this

ls -lrt *.txt *.out
ls *va.txt *a.log *final.sh | wc -l

The above command counts the number of files ending with the patterns listed.

Hi, Thanks

I will be receiving different fileextensions like *.MOB *.TXT *.VBS *.MI so am unable to judge what kind of extension it may have sometimes it wont have an extension to find out. so at that situation i think the above ideas will be collapsed.

i gave the code like ls -1 it listed out only the filename. So when i give the command like ls -lrt|ls -1 its displaying only the filename without the total count. So is there any other way to retrieve only the filenames along with the total number of files on that path. :frowning:

Hello,
You can do it like:

wc -l < <(ls -1 | tee /dev/tty && echo -n "Total: " >/dev/tty)

But, i think awk solution will better.

Regards.

Assuming that you want the names of files in the current directory (including those whose first character is a dot ( . )) and that no file in the current directory have names that contain a newline character:

ls -f | wc -l

will give you the number of files in the current directory.

If you want a sorted list of the files in the current directory followed by the number of files, try:

ls -a | tee /dev/tty | wc -l

This does work.

print "Total: \c" && (ls -lrt | grep "\." | wc -l)

Friends,

Thanks for your valuable inputs. Will try it out and let you know asap.. But i have a doubt on the command

tee /dev/tty

tee means creation of file. Could you please explain me that statement alone. So that it would be useful for me to understand the concept...

The tee utility does not "mean creation of file". The tee utility copies data read from its standard input to its standard output and also makes a copy of everything read from its standard input in all of the files named as operands. The special file /dev/tty is the process' controlling terminal. If you want to redirect the output of your this pipeline to a file, the command line would be:

ls -a |tee file | wc -l >> file

If you want long listing output (instead of just the file names), the command line would be:

ls -l | grep -v '^total' | tee /dev/tty | wc -l
        or
ls -l | grep -v '^total' | tee file | wc -l >> file

By default, the tee utility empties the files named by its operands before copying data to them; this doesn't matter for /dev/tty. If you want to save the output to a regular file and you want to append the data to the file instead of replacing data in the file, use:
ls -l | grep -v '^total' | tee -a file | wc -l >> file

Note that the total line in ls -l output is not the number of files in the directory; it the the number of disk blocks consumed by files in the directory. So, if you want long listing output, the grep -v gets rid of that total line, the tee command copies the remaining ls -l output to your destination file and also copies it to its standard output so wc -l can count the number of lines the ls command printed and adds that output to the end of the output written by the tee command.

Another way:

set -- *; [ -e "$1" ] && printf "%s\n" "$@" "Number of files: $#"

or to include hidden files/directories etcetera including "." and "..""

set -- .* *; [ -e "$1" ] && printf "%s\n" "$@" "Number of files: $#"