> I've got a directory that has a lot of files, and some of these files
> correspond to ip addresses.
> I want to list only the files in this directory that are not IP
> addresses but have actual names.
> The ls -I option appears to be what I need. It will ignore a specified
> pattern, but I'm not having much luck getting that pattern to match my
> scenario.
> What I've tried is
> ls -1 --ignore="[1-9]{1,3}.[1-9]{1,3}.[1-9]{1,3}"
> but this doesn't work.
That's because it uses shell (glob) patterns, not regular expressions.
Although that wouldn't be correct as regexp either, if you want literal
periods in there.
Quote:>The filenames start with IP addresses. Basically they are reverse DNS
>host files.
>192.168.0.1.hosts, etc.
Your example above only looks for three parts, is that intentional?
Do you want to skip also files like 192.168.0.hosts?
Anyway, you could do that with multiple --ignore options, but it's gets
really unwieldy:
ls --ignore '[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].*' \
--ignore '[0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].*' ...
You'd need 3^3=27 different patterns for 3 parts, 3^4=81 for four. Ouch.
With ksh you could do
ls !([0-9]?([0-9])?([0-9]).[0-9]?([0-9])?([0-9]).[0-9]?([0-9])?([0-9])*)
or if you could relax the test a little and skip also files with
more than three digits in each field,
ls !(+([0-9]).+([0-9]).+([0-9])*)
It's probably better to use grep, however:
ls | grep -Ev '^([0-9]{1,3}\.){3}'
Note single quotes and the leading ^.
That assumes a period after the IP, if you want to catch names
like "192.168.1fish" as well, try
ls | grep -Ev '^([0-9]{1,3}\.){2}[0-9]{1,3}'
By the way, that's still catching patterns that can't be
IP addresses, like 333.444.555. If those are not
possible, it might be worthwhile to think what exactly
are possible, it could be possible to simplify the
pattern more. On the other hand if you want to
see those, that is exclude only numbers 0-255,
it gets more complicated:
ls | grep -Ev '^((1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}'
That should catch only numbers 0-255 in each part
(untested).
--
Tapani Tarvainen