[SOLVED] how to make "ls" display the real size for directories
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
There is less than 24 hours left to vote in the 2015 LinuxQuestions.org Members Choice Awards. Click here to go to the polls. Vote now and make sure your voice is heard!
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
how to make "ls" display the real size for directories
I can't find an option to make "ls" display the real size for directories...all directories are displayed to be 4,0K
as in this example (I use ls -Alh --color):
drwxr-xr-x 2 johnjohn defaut 4,0K 2008-04-30 22:26 directory1
I am unsure of your definition of the real size of a directory. What ls shows is the size of the directory entry. If by "real size" you mean the total size of all of the files in a directory then use the du command. For example if you want the total size of /home/user/data and all of its contents use:
The ls command is not telling you the total size of all the files and subdirectories inside the directory. It is telling you the the amount of bytes it needs to define the files/subdirectories that it contains. It just appears that all directories are 4,0K because that is the default size of a new directory. When you come across a directory with a whole bunch of files, you will see that the number does in fact go up. To see the total size of all the files and subdirectories of a directory, you use "du -s directory".
"du -s directory" actually gives the size I need.
But I am looking for a way to get directly by typing a unique command, the sizes, names, and other details of both files and directories total content....
Is it possible to pass the result of "du -s directory" to ls?
You can't pass du -s to ls, it would try to find a file that is named the size that du returned. However, you could write a script that calls ls and then loops through all the listings and if it is a directory (first letter of line is a d) then run du on the name for that field and spit it out as one line. I'm not aware of a command that will do that. Perhaps someone else?
I pasted this function definition in my bash prompt
ans tested it
but it seems there is an error when I type lsd:
-rw------- 1 louisJ None 71K May 2 08:16 .bash_history
-rwxr-xr-x 1 louisJ None 1.2K Jun 11 2007 .bash_profile
du: can't access `\033[01': No such file or directory
sh: $'34mJC\E[0m': command not found
drwx------+ 2 louisJ None May 2 08:34 JC
I am new at this, any idea where the problem is?
The ls command is not telling you the total size of all the files and subdirectories inside the directory. It is telling you the the amount of bytes it needs to define the files/subdirectories that it contains. It just appears that all directories are 4,0K because that is the default size of a new directory.
The ls command shows me what looks to me to be the sizes of files in the directory that I am in when I enter the command. Are these not the real sizes of those files?
Also, I went used the mouse to click on the "home" icon on the desktop of kde 3.5, and I can browse for the directory that I want to size. I can select that directory and right click with the mouse and select "properties" and it will show me the size of that directory.
1) There is something that I do not understand about that "size" utility in that it calculates the size of the directory, but sometimes the size does not seem to be the right size. For example, when I tried to find out how much harddrive space was left on my harddrive, I went to "Storage Media" and did the properties on it. Something seems wrong with the size that it displays for the harddrives. It says that 11GB are used, but it also says beneath that there is 21GB free of 30GB. If I add 11+21=32GB, I get an extra 2GB. Is there something that I do not know about?
The ls command will tell you the size of the individual files inside the directory. It won't tell you the TOTAL size of all the files and subdirectories inside the directory. So, if a directory is able to define all of its contents with only 4K, it will read 4K. If it needs more bytes to define the contents, it will allocate more space for its size description in 4K blocks. Files DO state the amount of space they take up. Directories do not.
As for your follow up question, the only thing I can think of is that it might be rounding things somehow. If I do the same thing on one of my storage media folders, it tells me I'm using about 25% of my drive. However, the df command tells me I'm really only using 21%. I believe df over the other.
This discussion is flawed. The ONLY space a directory takes up is the space required to hold the directory entries. To talk about the "size" of the directory in the context of "the size of all the files/directories under it" makes no sense (remember that files can be hard-linked in different directories).
It's very important to bear in mind that files are not "contained" in directories. This is one of the things that always irritates me about Windows etc using terms like "folders". A folder gives the impression of a *container*, i.e. you can put things in it. In unix, a directory is NOT a container, it has no size other than the directory entries. Think of a directory tree more as an index or database, to tell you where to find your data (just as the Dewey Decimal system indicates what shelf to look on to find a book in the library, a directory entry tells you (or rather the kernel) what inode to look at to find your file).
Originally Posted by forrestt
As for your follow up question, the only thing I can think of is that it might be rounding things somehow.
This is almost certainly the case. newtovanilla, you probably do not have 11GB, it is probably 10 and a bit. Likewise the free space is probably 20 and a bit, you're filesystem probably isn't _exactly_ 30GB, most likely a few MB this way or that way. When you take this into account, it all adds up. Unless/until your filesystem is 99% full, I wouldn't worry about a few percent discrepancy.
I don't have any problem with someone conceptualizing a directory as a container. We are humans, and this is how we think, and to know how much stuff is in a container is a useful piece of knowledge (if it weren't we wouldn't have the du command). Thinking of directories as containers makes things easier to visualize and keep track of things in our minds. It may not be the way the file-system actually works, but it is how most people think, and makes perfect sense whether you have hard links or not. To visualize a directory as a database could only be useful if you are someone who is actually designing or programming a file-system. For anyone else, to do so is making things way more complicated than they need to be. If someone knows what the size entry in ls means for a directory entry, then they're fine. But, if they don't, then it needs to be explained, because the natural way of thinking is to assume it means something that it does not. It isn't the conceptualizing that is wrong. It is the assumption that a particular tool is giving one piece of information, when it is actually giving another piece, that is wrong. That is fixed by teaching others how to use our tools.
I don't have any problem with someone conceptualizing a directory as a container. We are humans, and this is how we think, and to know how much stuff is in a container is a useful piece of knowledge (if it weren't we wouldn't have the du command). Thinking of directories as containers makes things easier to visualize and keep track of things in our minds. It may not be the way the file-system actually works, but it is how most people think, and makes perfect sense whether you have hard links or not.
Way over-thought. Just compare a directory in a Linux
file-system to a phone directory. It points to what you're
after in an organised way. To say it "contains" those entries
is at best a matter of (flawed) semantics - you wouldn't say
a phone directory has people's phones; it points at them.