简体   繁体   中英

in linux, how to (quickly) get a list of all files in a directory - with their filesize

I need to get a list of all the files, along with their sizes, in Linux.

the filesystem is ext4, running over USB on a machine with very little RAM

the functions I'm using are these - is there a better technique?

a) opendir()
b) readdir()
c) stat()

I believe I'm getting hit pretty hard with the stat() call, I don't have much RAM and the HD is USB connected

is there a way to say

"give me all the files in the directory, along with the file sizes?" - my guess is that I'm getting impacted because stat() needs to go query the inode for the size, leading to lots of seeks?

No, not really. If you don't want to hit the disk, you would need have the inodes cached in memory. That's the tradeoff in this case.

You could try tuning inode_readahead_blks and vfs_cache_pressure though.

Use cd to get to the directory you want to get the size of the files and directories for, then type:

du -sh *

This will give you all the sizes of the files and directories. The s gives a total for each argument, h makes the output human readable. The * means that it will show all the contents of that directory.

Type man du to find out more about the du command ( q or Ctrl-c to exit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM