9

I have file called -l in my directory

now I tried to do

for i in *; do stat -c "%s %n" "$i"; done

it lists all files with sizes but in the middle of the output there is something like

395 koko.pub
stat: invalid option -- 'l'
Try 'stat --help' for more information.
2995974 list.txt

so it can not process -l as normal filename, how do I get desired behavior from stat?

lllook
  • 410

3 Answers3

22

Use ./ before filename:

for i in *; do stat -c "%s %n" "./$i"; done

Or use -- to indicate the end of options for stat:

for i in *; do stat -c "%s %n" -- "$i"; done

Though that one will still fail for a file called - (will report information for the file open on stdin instead of the - file in the current directory).

heemayl
  • 56,300
7

Add -- to mark the end of the options to stat:

for i in *; do stat -c "%s %n" -- "$i"; done
Stephen Kitt
  • 434,908
-2

Even simpler, rename the file to -_I or simply I without the dash. Simply as a matter of purient curosity WHY did you place a dash in front of the file in the first pplac?