How to show file and folder size in ls format

2019-08-15 05:43发布

ls -l will show all file and folder in detail information. but folder size is not correct(alwasy show 4.0k).

So how to show file/folder as ls -l format besides folder with correct size. Thanks!

ls -l format:

drwxr-xr-x  9 jerry jerry XXX.0K Mar  3 14:34 Flask-0.10.1  
-rw-rw-r--  1 jerry jerry 532K Mar  3 14:25 Flask-0.10.1.tar.gz  
drwxrwxr-x 10 jerry jerry XXXK Feb  8 15:41 leveldb1.15

标签: linux bash
7条回答
在下西门庆
2楼-- · 2019-08-15 06:11

quoting @izx

The directory is just a link to a list of files. So the size which you are seeing is not the total space occupied by the folder but the space occupied by the link. The minimum size a file or directory entry/link must occupy is one block, which is usually 4096 bytes/4K on most ext3/4 filesystems.

So the minimum size displayed can be only 4.0k

查看更多
该账号已被封号
3楼-- · 2019-08-15 06:12

Try:

du -b -d 1

This prints the disk usage for each folder to a depth of 1, which is the equivalent of ls -al

查看更多
ら.Afraid
4楼-- · 2019-08-15 06:18

Made a script similar to this (replicating some ls options) before, made du change and should work here

#!/bin/bash

[[ ! -d "$1" ]] && echo "Error $1 not a directory" 1>&2 && exit

for f in "$1"/*; do
  f="${f##*/}"
  symlink=
  if [[ "$(stat -c '%h' "$f")" -gt 9 ]]; then
    string="$(stat -c "%A %h %U %G" "$f")"
  else
    string="$(stat -c "%A  %h %U %G" "$f")"
  fi

  t="$(stat -c "%x" "$f")"

  date="$(date -d "$t" "+%-d")"
  if [[ $date -lt 10 ]]; then
    date="$(date -d "$t" "+%b  %-d %H:%M")"
  else
    date="$(date -d "$t" "+%b %d %H:%M")"
  fi

  if [[ -h $f ]]; then
    size="$(ls -sh "$f" | cut -d ' ' -f1)"
    symlink="$f -> $(readlink "$f")"
    f=
  elif [[ -d $f ]]; then
    size="$(du -sh "$f" | cut -f1 2>&1)"
    f="$f/"
  elif [[ -f $f ]]; then
    size="$(ls -sh "$f" | cut -d ' ' -f1)"
  else
    echo "Error $f" 1>&2 && exit
  fi

  printf "%s %-4s %s %s\n" "$string" "$size" "$date" "${f}$symlink"
done

Will obviously be quite a bit slower than regular ls, since it has to calculate size.

Example output

> ./abovescript "./testdir"
drwxrwxr-x  5 owner group 11M  Apr  3 06:53 workspace/
-rwxrwxr-x  1 owner group 8.0K Mar 30 03:53 a.out
lrwxrwxrwx  1 owner group 0    Apr  3 07:19 foo -> test/file
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-08-15 06:22

You can use the -printf option of find to get most of the info - see the man page for more info. The easiest way I know to find the size of a directory including everything inside it is with du -s, so you will have to print/paste these values together. eg:

paste <(find . -maxdepth 1 -printf "%M %u %c %p\n") <(find . -maxdepth 1 -exec du -s {} \; | cut -f1 ) | column -t

Sample output:

drwxrwxr-x  ooh Thu  Apr  3  07:07:45  2014  .           12260
-rw-rw-r--  ooh  Thu  Apr  3  07:07:41  2014  ./test.txt  5080
drwxrwxr-x  ooh  Thu  Apr  3  07:07:54  2014  ./testdir   7140

So: permission/owner/date/name/size in bytes

查看更多
Fickle 薄情
6楼-- · 2019-08-15 06:25

I'm not quite sure if this is what you are looking for - but from man ls:

--block-size=SIZE scale sizes by SIZE before printing them. E.g., '--block-size=M' prints sizes in units of 1,048,576 bytes. See SIZE format below.

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

EDIT: Based on your comment I would set up an alias to get an output which gives you the desired data but breaks the typical format of ls - e.g.:

alias lsd="ls -l && du -h --max-depth 1"

Result:

$ lsd
total 4
dr-xr-xr-x 4 user group 4096 Mar 12  2012 network
12K ./network
16K .

I do not know of any way to achieve your goal with ls only.

查看更多
女痞
7楼-- · 2019-08-15 06:25

For sure there's a better way, but this works for me:

ls -l | while read f
do
 echo -ne $( echo $f | cut -f -4 -d ' ')
 echo -ne " $(du -s $(echo $f | cut -f 9 -d ' ') | cut -f 1) ";
 echo $( echo $f | cut -f 6- -d ' ');
done
查看更多
登录 后发表回答