Operating System - HP-UX
1751692 Members
4739 Online
108781 Solutions
New Discussion юеВ

Re: 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 14
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]*)