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.
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.
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:
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.