0

I would like to list the content of a directory, 1 line per entry, with only the files names and the files sizes.

ls -l shows too much information.

ls -1 -s doesn't show a file's size but its allocation (--block-size=1 doesn't change that)

I cannot find a command line argument that makes ls do what I want... is there one?

If not, what would be a good, short and robust solution to make that kind of listing?

Nawak
  • 1
  • 1
    Please [edit] your question and i) tell us what operating system you are using so we know what tools are available and ii) specify why you want this: is it just so you can see it on the terminal or do you need to process the output somehow? – terdon May 24 '22 at 18:42

2 Answers2

1

This is a bit odd solution. You can use du command:

du -s *

will list size and name of files and directories

Romeo Ninov
  • 17,484
  • du will display blocks (disk usage) while some stat or the -printf "%s" of the find command will show bytes. There is du -b but I am not sure if it can always matches the size (see man page), or if it is portable. – thanasisp May 24 '22 at 15:59
  • @thanasisp, no, du -b is not portable. Do not exist in AIX, Solaris... – Romeo Ninov May 24 '22 at 16:03
  • Then it can't print the byte size, but blocks, e.g. for a file with 1 byte it would say 4 (4K) – thanasisp May 25 '22 at 12:04
  • @thanasisp, wrong. For linux AFAIK you will get size in kB, for AIX, Solaris, HP-UX you will get it in 512 bytes blocks. – Romeo Ninov May 25 '22 at 12:37
  • (The 4K is an example ("e.g."), whatever the block-size is). For example what du -s file prints for you, for a text file having only 1 character? – thanasisp May 25 '22 at 12:42
  • @thanasisp, it depend of filesystem. For some extend based it may give you the precise size. :) – Romeo Ninov May 25 '22 at 12:44
  • It will not give byte size. Because it is "disk usage". The block is reserved for this file, so it counts as "usage". It is not byte count. See also this. – thanasisp May 25 '22 at 12:50
  • @thanasisp, did you miss my previous comment? some extent based (filesystems).... – Romeo Ninov May 25 '22 at 12:54
  • I try your command du -s file with a text file of 1 byte/character on GNU/Linux (ext4) and I get output: 4 file. with du -sh I see 4.0K file. Could you please report the output for your OS? – thanasisp May 25 '22 at 13:01
1

With zsh:

zmodload zsh/stat
stat -Ln +size -- *

With GNU find and sort:

LC_ALL=C find . -maxdepth 1 ! -name '.*' -printf '%f %s\0' |
  sort -z |
  tr '\0' '\n'