On Thu, Oct 30, 2025 at 22:36:01 -0700, Michael Paoli wrote:
> $ ls *.pdf
> ls: unrecognized option '--K.pdf'
> Try 'ls --help' for more information.
> $
> Notably I'm unable to find a way to, as OP presumably invoked,
> get ls from coreutils to complain about option '--K',
> but rather only the name of the file (interpreted as option)
> starting with --K and ending with .pdf extension.
That's why I contrived the example where the filename starts with --K
then has an apostrophe, a newline, and the rest of the filename.
It was the closest I could get to the OP's error message.
hobbit:~$ ls -*
ls: unrecognized option '--K'
.pdf'
Try 'ls --help' for more information.
Of course, we know that the OP did not report the *entire* error
message, because it should end with
Try 'ls --help' for more information.
So, we don't actually know what the full error message said. They might
have abridged or altered it in a variety of ways.
David's point about ls vs. echo is also an important one. ls will not
reproduce all filenames verbatim. Compare:
hobbit:~$ ls -- -*
'--K'\'''$'\n''.pdf'
hobbit:~$ printf '%s\n' -*
--K'
.pdf
Now, arguably ls's output may be better for some use cases, and worse
for others. You'll have to take that into consideration for each
problem, one by one.
echo is not the best choice for this particular problem, because the
version of echo in bash *does* process options. If we have a file
whose name is exactly "-e" or "-n", bash's echo will treat that as an
option, instead of printing it. printf, awkward as it may be, is the
only 100% sure way to get exactly the output you want in bash.
hobbit:~$ rm './--K'\''
.pdf'
hobbit:~$ touch -- -n -q
hobbit:~$ printf '%s\n' -*
-n
-q
hobbit:~$ echo -*
-qhobbit:~$
Finally, tab completion is your friend. If you believe there's only
one filename beginning with a hyphen, and you want to rename it,
you can just start typing
mv ./-
and then press Tab, and let the shell write out the filename for you.
Depending on which version of bash it is, the name may end up being
quoted in various ways (single quotes, backslashes, or some combination
of them), but as long as there's no broken bash_completion recipe in
the picture, the result should be usable as an argument to mv.
The rm comand that I typed up above used tab completion.
Should you find yourself facing a broken bash_completion recipe, which
is sadly common, you can run "complete -r" to remove all programmable
completions from the current shell instance.