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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
lllook
  • 400
  • 1
  • 3
  • 7

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).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
heemayl
  • 54,820
  • 8
  • 124
  • 141
7

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

for i in *; do stat -c "%s %n" -- "$i"; done
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
-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?

Edgar Naser
  • 154
  • 1