Frage

I need to make a shell command that lists all file names and their sizes in a directory. I wrote this:

ls -l | awk ' {print $9, $5} '

the problem is that with $9 it only prints the first word of the name of the file. Any tips to make it print the whole name?

War es hilfreich?

Lösung

Instead of parsing ls, use find:

find . -type f -printf "%s\t%f\n"

The %f directive prints the filename with leading directories removed. %s produces the file size in bytes.

For restricting the listing to the current directory, use -maxdepth:

find . -maxdepth 1 -type f -printf "%s\t%f\n"

You could also use stat:

stat --printf "%s\t%n\n" *
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top