简体   繁体   English

在Linux中,如何(快速)获取目录中所有文件的列表-及其文件大小

[英]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. 我需要获取Linux中所有文件的列表及其大小。

the filesystem is ext4, running over USB on a machine with very little RAM 文件系统是ext4,可以在RAM很少的机器上通过USB运行

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 我相信stat()调用会给我很大的打击,我没有太多的内存并且HD已连接USB

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? -我的猜测是我受到影响,因为stat()需要查询inode的大小,从而导致大量查找?

No, not really. 不,不是。 If you don't want to hit the disk, you would need have the inodes cached in memory. 如果您不想打磁盘,则需要将inode缓存在内存中。 That's the tradeoff in this case. 在这种情况下,这就是权衡。

You could try tuning inode_readahead_blks and vfs_cache_pressure though. 您可以尝试调整inode_readahead_blksvfs_cache_pressure

Use cd to get to the directory you want to get the size of the files and directories for, then type: 使用cd进入要获取其文件和目录大小的目录,然后键入:

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. s给出每个参数的总数, h使得输出易于阅读。 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) 键入man du以了解有关du命令的更多信息( qCtrl-c退出)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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