ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
du lists disk usage, but by whom?? ofcourse a file.. everything is file in linux. than what is the difference between size listed by du for a file and file size showing using stat command ?
This produces a sparse file. You can read it normally, but the initial zeros you see are not stored on disk, and do not consume disk space.
So, even though ls -l example-file outputs
Code:
-rw-rw-r-- 1 nominal animal 1048575 May 10 14:43 example-file
and find example-file -printf '%s\n' and stat -c '%s' example-file both output
Code:
1048575
the actual disk usage is much less than the file size. My ext4 partition I used for this example utilizes 4k blocks, so du -hs example-file outputs
Code:
4.0K example-file
Do you now see the difference between file sizes and disk usage?
Directories also consume disk space. The amount depends on the number of entries, the length of the file and directory names in it, and the number of POSIX and extended attributes used. If the du command you run includes the directories, the result will reflect the actual disk space used, and will be significantly different to the total size of the relevant files. You cannot even say which one (disk usage or total file size) is greater, unless you check!
Someone should also point out that this line will never work as expected seeing the while loop is in a sub-shell and so any changes to the variables inside the loop
will not be reported outside the loop.
This produces a sparse file. You can read it normally, but the initial zeros you see are not stored on disk, and do not consume disk space.
So, even though ls -l example-file outputs
Code:
-rw-rw-r-- 1 nominal animal 1048575 May 10 14:43 example-file
and find example-file -printf '%s\n' and stat -c '%s' example-file both output
Code:
1048575
the actual disk usage is much less than the file size. My ext4 partition I used for this example utilizes 4k blocks, so du -hs example-file outputs
Code:
4.0K example-file
Do you now see the difference between file sizes and disk usage?
Directories also consume disk space. The amount depends on the number of entries, the length of the file and directory names in it, and the number of POSIX and extended attributes used. If the du command you run includes the directories, the result will reflect the actual disk space used, and will be significantly different to the total size of the relevant files. You cannot even say which one (disk usage or total file size) is greater, unless you check!
That is correct. However, look at the -b and --apparent-size switches for du which will show the correct result.
That is correct. However, look at the -b and --apparent-size switches for du which will show the correct result.
Only if the parameter list does not refer to any directories. When using
Code:
du -b --apparent-size ... DIRECTORY ...
the result includes the size of the directories, not just the files. It is therefore not the total size of the files, as specified in the title of this thread; it yields the total size of the specified files and directories.
Only if the parameter list does not refer to any directories. When using
Code:
du -b --apparent-size ... DIRECTORY ...
the result includes the size of the directories, not just the files. It is therefore not the total size of the files, as specified in the title of this thread; it yields the total size of the specified files and directories.
Isn't a directory also a file (everything's a file in unix)? By the way, the original question was (emphasis added):
Quote:
There are different methods but what is the most robust method to make the total size of the files and/or directories from a list of files (`ls -1 `)?
if you don't want directories, you can use find . -type f or whatever.
There are enough differences that make the distinction meaningful.
Directory size is almost always an integral number of disk allocation blocks, making it useless for detecting e.g. additions or removals. (Note: Symlink size is almost always the length of the symlink target path.)
Directories cannot be hardlinked, only files can. (You can, however, bind-mount directories and files over other directories and files, so only the "top one" is accessible.)
Execute right to a directory allows you to stat() the directory and any file you know the name of in that directory. (Without read rights, you cannot scan the directory, so I guess read and write rights are analogous between files and directories.)
For mount points, /path/to/directory and /path/to/directory/. have different device and inode numbers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.