LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ls -l *.txt does not print the total (https://www.linuxquestions.org/questions/linux-newbie-8/ls-l-%2A-txt-does-not-print-the-total-941062/)

stf92 04-21-2012 08:45 PM

ls -l *.txt does not print the total
 
Hi:

Code:

$ ls -l
total 37672
-r--r--r-- 1 semoi users 13313654 2011-12-18 23:32 01\ Symphony\ No.25\ in\ G\ Minor\ K.183\ -\ I\ -\ Allegro\ con\ brio.flac
-r--r--r-- 1 root  root  25207604 2011-12-18 23:32 01\ Symphony\ No.25\ in\ G\ Minor\ K.183\ -\ I\ -\ Allegro\ con\ brio.wav

Code:

$ ls -l *flac
-r--r--r-- 1 semoi users 13313654 2011-12-18 23:32 01\ Symphony\ No.25\ in\ G\ Minor\ K.183\ -\ I\ -\ Allegro\ con\ brio.flac
$

As you can see, in the first example, ls prints the total. However, in the second one, it does not. How can I have the total in the second case (assume there is a large quantity of .flac files in the directory)?

EDIT: du *wav does not solve the problem. I was looking in Google and saw this:
Code:

ls -l files | awk '{ x += $5 }
END { print "total K-bytes: " (x + 1023)/1024 }'

It seems ls is not very self-sufficient (IMHO).

allend 04-21-2012 11:17 PM

Try 'du -c *flac' or 'du -cBK *flac' if you want to specify kilobyte as the unit.

grail 04-22-2012 03:29 AM

You are aware that the total is a measure of the number of blocks and not the actual size of items. As far as I can see on a quick search, the total line is only displayed
when used on a directory, ie. the total blocks used in that directory.

allend's solution does seem to provide the same details though :)

David the H. 04-22-2012 03:29 AM

Globbing patterns like "*.flac" are expanded by the shell into a list of filenames before they are passed to the final command.

Code:

#when running...
ls -l *.flac

#The actual command executed is...

ls -l file1.flac file2.flac file3.flac file4.flac etc...

Since ls is now operating on a list of files rather than a directory, you don't get a directory summary in the output. You have to figure out some other way to get the data you want; a loop that adds up a running total, for example.

stf92 04-22-2012 07:10 AM

All I say is that ls could be smart enough to do it by itself. One of the things I dislike in this command is that, when issuing an 'ls foo' it lists not only file foo but the contents of directory foo if it exists. So, if you do 'ls foo*' you do not if you are seeing the foo* files or the contents of dir foo*. Because, on top of that, listings are not headed by the name of the directory as it would be logical. Thanks for the posts.

grail 04-22-2012 08:07 AM

hmmm your ls must behave different to mine as far as:
Quote:

Because, on top of that, listings are not headed by the name of the directory as it would be logical.
Here is an example from my home directory:
Code:

$ ls -l tmp*
-rw-rw-r-- 1 grail grail    0 2012-04-22 21:05 tmp1

tmp:
total 24
-rwxr-xr-x 1 grail grail  256 2012-04-18 02:50 d2.sh
drwxrwxr-x 7 grail grail 4096 2012-04-18 02:15 dir-five
drwxrwxr-x 7 grail grail 4096 2012-04-18 02:15 dir-four
drwxrwxr-x 7 grail grail 4096 2012-04-18 02:13 dir-one
drwxrwxr-x 7 grail grail 4096 2012-04-18 02:14 dir-three
drwxrwxr-x 7 grail grail 4096 2012-04-18 02:13 dir-two

Mine seems to head up the directory

stf92 04-22-2012 08:30 AM

Mine is
ls (GNU coreutils) 6.9

Curiously enough, in a case like your example, it does not head up the list.

suicidaleggroll 04-22-2012 09:17 AM

Add the -d flag to ls if you don't want it to print directory contents

grail 04-22-2012 11:00 AM

Well maybe it is the older version then as mine is:

ls (GNU coreutils) 8.5

stf92 04-22-2012 11:29 AM

Really, I have to have an alias that does the same thing as MS-DOS dir command: print directory name, total file size and number of files and directories. Some day I will have it.


All times are GMT -5. The time now is 10:56 PM.