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?

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, 2022 at 18:42

2 Answers 2

1

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

du -s *

will list size and name of files and directories

9
  • 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, 2022 at 15:59
  • @thanasisp, no, du -b is not portable. Do not exist in AIX, Solaris... May 24, 2022 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, 2022 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. May 25, 2022 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, 2022 at 12:42
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'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .