Question

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?

Was it helpful?

Solution

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" *
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top