1820479 Members
2550 Online
109624 Solutions
New Discussion юеВ

ls -l du

 
SOLVED
Go to solution
Lee_110
Occasional Contributor

ls -l du

Hello,

I have a question regarding ls -l and du -s *
/home/

ls -l /home

drwxr-xr-x 8 users 1024 Apr
3 05:52

du -s /home

3796


If I have to find the exact size of in bytes/kb/mb (size of all files and its subdirectories ...) how should I know? Whats the difference between du -s * and ls -l.

Please help
6 REPLIES 6
Pete Randall
Outstanding Contributor
Solution

Re: ls -l du

The ls command is showing you the space occupied by the directory entry (which is a file itself). The du command is summarizing the space occupied by the contents of the directory. I generally prefer to run "du -sk" to give the output in KB.


Pete

Pete
Geoff Wild
Honored Contributor

Re: ls -l du

The du command gives the number of 512-byte blocks allocated for all
files and (recursively) directories within each directory and file
specified by the name operands.

This is what you want - Kbytes:

du -sk /home/

ls justs list the contents...you could use an awk script to total - but why bother...


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: ls -l du

The two commands serve entirely different purposes. Ls is really geared towards providing information about an individual file (although obviously it will list entire directories). For example your ls -l indicates a size of 1024 bytes for /home. This refers to the size of the directory itself --- not the files underneath it. Du -s displays a summary total of the files including subdirectories and is generally better suited tto your task. Note that du displays its sizes in 512-byte block (or 1k if you add the -k option).

Now, when you say you want the exact size that becomes more tricky.

It's possible to create "sparse" files. For example, suppose you create a file and seek out to offset 1000000 before writing anything.

e.g.
mkdir /var/tmp/mydir
cd /var/tmp/mydir
dd if=/dev/zero bs=1 oseek=999999 count=1 of=mydir

This will create a 1000000 byte file that only uses 1 byte of disk storage (actually 1 block). In this case what is the "true" size. I suggest you run this comand and examine it with both ls -l and du -k. You will get very different results. Which is correct? Both are because they are looking at different things.

If sparse files are heavily used in a filesystem, it is possible to have the total size of the files (as reported by ls -l) much larger than the total size of the filesystem itself.
If it ain't broke, I can fix that.
Dani Seely
Valued Contributor

Re: ls -l du

Keep it simple, just use the -k option to show the disk usage in kilobytes ...
# du -sk
Together We Stand!
Matthew Ghofrani
Regular Advisor

Re: ls -l du

If you have subdirectories in your filesystems such as:
/dev/vg01/lvol1 /home
/dev/vg01/lvol2 /home/matthew
/dev/vg01/lvol3 /home/matthew/emc

and you are sitting in /home and don't want the rest to show up use -x option:

du -x .|sort -nr|more

Matthew from Boston
Life is full of bugs
Cem Tugrul
Esteemed Contributor

Re: ls -l du

Hi Lee,

ls(short list)
du (disk usage)

when you use ls -l need some manipulation
with awk...On the other hand,trying to get
exact size of your dir than you can use
du -ks. -k option means kbytes
i usually use du -ks *|sort -n
and gets what i need...

Good Luck,
Our greatest duty in this life is to help others. And please, if you can't