Operating System - HP-UX
1753529 Members
4975 Online
108795 Solutions
New Discussion юеВ

Re: Operations on irregularly named files in a directory

 
SOLVED
Go to solution
eric lipede_1
Regular Advisor

Re: Operations on irregularly named files in a directory

great - thks
James R. Ferguson
Acclaimed Contributor

Re: Operations on irregularly named files in a directory

Hi (again) Eric:

> then an ll shows all the (original) files *, \ etc

Oops, try this:

# ls -1 | perl -nle 'm{^[.a-zA-Z0-9]} or unlink'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Operations on irregularly named files in a directory

HI Eric:

...and the addition of the '-l' switch is a kludge which hides a common (?) oversight.

The behavior is documented in 'perlrun' and in part says:



First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted, sets $\ to the current value of $/ .



For every line read from our pipe, the filename already ends with a newline character. In the absence of the '-l' switch we let the print() honor what's there. That's fine for pretty printing, *but* means that 'unlink()" sees a string with the newline which does *not* match the file we think it should.

Adding the '-l' "fixes" the problem (ever so obtusely) since an automatic chomp() of the newline character occurs, leaving 'unlink()' to be handed a filename that can be found.

All this to say, it would have been much better to have written:

# ls -1|perl -ne 'chomp;m{^[.a-zA-Z0-9]} or print "$_\n"'

...to print, and:

# ls -1|perl -ne 'chomp;m{^[.a-zA-Z0-9]} or unlink'

...to actually remove files.

Or, if you prefer to be a a bit long-handed, say:

' ... or unlink $_'

By the way, unlink() will normally not remove directories.

Regards!

...JRF...

hpuxrocks
Advisor

Re: Operations on irregularly named files in a directory

-
eric lipede_1
Regular Advisor

Re: Operations on irregularly named files in a directory

.