Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Operations on irregularly named files in a directory

SOLVED
Go to solution
eric lipede_1
Regular Advisor

Operations on irregularly named files in a directory

Scenario

There are 10 files in a directory called:
a
b
1a
\
*
x
-
\*
9
0

what scripting code/ awk / perl would you use to (safely and robustly) delete all files that d0 not start with an alpha or numeric ?

thks in advance
14 REPLIES
Viktor Balogh
Honored Contributor
Solution

Re: Operations on irregularly named files in a directory

# ls -li

this will display the inode number of those files

after this, you can delete it with:

# find /path_to_file -xdev -inum -exec rm -i {} ;

****
Unix operates with beer.
James R. Ferguson
Acclaimed Contributor

Re: Operations on irregularly named files in a directory

Hi Eric:

You could do:

# cd mydirectory
# ls -1 | perl -ne 'm{^[a-zA-Z0-9]} or print'

Note that the 'ls -1' is a numeric ONE.

If you are satisfied with the output of the above, which would be the files to be *removed* then _change_ the 'print' to 'unlink'. Doing so will remove those files.

Regards!

...JRF...
eric lipede_1
Regular Advisor

Re: Operations on irregularly named files in a directory

Hi there

thks ...the inode solution is good but how would you script that ?
The perl is slick ...but how would one script the end bit ...to avoid

rm "*" (lol) and rm "\" ....

as a

for i in `perl stuff here`
loop would cause $i (in the case if *) to list all the files....
so doing
rm $i would effectively be doing rm "list of file" or rm *

thks !

eric lipede_1
Regular Advisor

Re: Operations on irregularly named files in a directory

...forgot to mention that

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

produces the same output / full file list without any deletions ..unless Im getting the syntax wrong ?
James R. Ferguson
Acclaimed Contributor

Re: Operations on irregularly named files in a directory

HI (again) Eric:

OK, it's reasonable to skip the dot files :-) Your original description of your problem didn't suggest that requirement :-)

#cd /path && ls -1 | perl -ne 'm{^[.a-zA-Z0-9]} or print'

Note that I added a dot ('.') to the permitted character list.

Regards!

...JRF...
Viktor Balogh
Honored Contributor

Re: Operations on irregularly named files in a directory

> the inode solution is good but how would you script that ?

I think it's not common to have nonprinting characters in filenames, so there's no need to script it. Anyway, I like to sleep well and wouldn't automatize (e.g. through cron) such a script that checks for such files and deletes those. I like to do this manually, that's why I said "rm -i". Just to be sure nothing important get deleted.
****
Unix operates with beer.
eric lipede_1
Regular Advisor

Re: Operations on irregularly named files in a directory

> the inode solution is good but how would you script that ?

I think it's not common to have nonprinting characters in filenames, so there's no need to script it.

wow Viktor ...thats a very special statement there. I bow to your superior knowledge on whats not common and what scripts are "needed" for everyone in the SA world.
;-)

...Perhaps there should be a new thread called "It happened to me" to get a sense of the odd things that have happened ..

Seriously though, the fact that the question has been asked is indicative of a "need". It only needs to occur 1ce for it to be relevant to the question asker. ...of course saying "I dont know" ...or nothing might also be an option
eric lipede_1
Regular Advisor

Re: Operations on irregularly named files in a directory

Hi James

yes the dot files are excluded from the list/ requirement. I do like the simiplicity of the perl solution ...I m still unclear though how you would delete the found files ....

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

then an ll shows

all the (original) files *, \ etc

thks
Dennis Handly
Acclaimed Contributor

Re: Operations on irregularly named files in a directory

>delete all files that do not start with an alpha or numeric?

rm -i !([A-Za-z0-9]*)
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

.