2

In this directory when I run

ls -l 

it prints the following output:

-rw------- 1 csundl dcsugrad 0 Dec 5 13:51 file3
drwx------ 2 csundl dcsugrad 4096 Dec 5 13:51 Photos
drwx------ 2 csundl dcsugrad 4096 Dec 5 13:51 Pron
drwx------ 2 csunfi dcsugrad 4096 Dec 5 13:51 Spreadsheets
drwx------ 3 csundl dcsugrad 4096 Dec 5 15:12 Stuff
-rwx------ 1 csundl dcsugrad 149 Dec 5 15:08 untitled.sh

The bolded values are the byte sizes (?).

However when I run:

ls -l | wc -c  

The total byte size comes up as 340. Why is this?

Thanks.

3
  • This does not apply to this question, but even if a question is closed, you should not delete it in general, if it has a non-negative score. Commented Dec 10, 2013 at 17:39
  • I really like how you did not filter your ls -l output ;-) Commented Feb 7, 2014 at 14:22
  • @RedX what? "Pron" is just a progressive cron program. What? Commented Feb 7, 2014 at 14:31

1 Answer 1

2

ls command displays size of the file

but this command:

ls -l | wc -c

counts number of characters in the output of ls command.

To count total size of files in a directory use du command:

du -hs mydir
Sign up to request clarification or add additional context in comments.

6 Comments

I just realised, durrrr. Thanks! What would be a good route for calculating the total size, in bytes, of a directories files USING LS
we have to use ls for this exercise.
Parsing ls command for this calculation is not a good idea. I will get lots of hate comments if I post a script to sum up file sizes from ls's output :)
"you will need to parse the output of an ls in order to find the size of each file in order to calculate the total space usage" Not even a hint :P
@Lecaille Hmm, your teacher is incorrectly telling you you will need to parse the output of an ls, but he/she is not actually requiring you to do it, based on that quote. Please read the du manpage, and this treatise on why you should never parse the output of ls. Then, do the assignment both ways for the best education. You'll be smarter than your teacher very shortly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.