8

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?

0
21

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

1
  • 5
    Using for i in ./* should also work – Darkhogg Dec 17 '15 at 12:21
7

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

for i in *; do stat -c "%s %n" -- "$i"; done
-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?

Not the answer you're looking for? Browse other questions tagged or ask your own question.