Re: The ls command
At Thu, 30 Oct 2025 15:34:01 -0400 The Wanderer <wanderer@fastmail.fm> wrote:
>
> On 2025-10-30 at 14:32, debian-user@howorth.org.uk wrote:
>
> > The Wanderer <wanderer@fastmail.fm> wrote:
> >
> >> On 2025-10-30 at 13:33, Michael wrote:
> >>
> >>> The command
> >>>
> >>> ls -- *.pdf
> >>>
> >>> has been run and a very large number of PDF files has been
> >>> listed.
> >>>
> >>> It is very difficult to spot any strangely named PDF files.
> >>>
> >>> What command should be issued to easily list any strangely named
> >>> PDF file ?
> >>
> >> Try:
> >>
> >> $ ls -- *.pdf | grep ' -'
> >>
> >> This should show you any files whose name include a space followed
> >> by a dash. The space would be treated as a token separator, and the
> >> dash would be treated as the beginning of a new argument to ls.
> >
> > err no, ls lists multiple files on a line and this wouldn't pick up
> > any file at the start of a line.
>
> In addition to the notes Nicolas George made, I'll point out that the
> issue is almost certainly not about the characters at the *start* of a
> filename, but about a filename with a space *in* it and a dash *after
> that space*.
Actually no. The shell is not actually that stupid -- what you suggest would
only happen if the OP did a copy-paste or just typed the file names with
spaces and did not escape or quote then. The OP is using a wildcard (*.pdf).
When the shell expands that it loads the *expansions* into separate elements
of the argv array passed to the forked process. That process than looks at
the elements and processes any with a '-' as the first character as options.
>
> The command I suggested was aimed primarily at catching files with such
> a ' -' sequence in the middle of the name.
>
> I'll admit that it wouldn't catch filenames that start with a '-', or at
> least I'm not confident that it would. I suppose something like
>
> # ls -- *.pdf | grep '(^| )-'
>
> would catch both cases.
>
--
Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
heller@deepsoft.com -- Webhosting Services
Reply to: