Operations on irregularly named files in a directory (627 Views)
Reply
Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 1 of 15 (627 Views)
Accepted Solution

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
Honored Contributor
Posts: 1,007
Registered: ‎03-15-2009
Message 2 of 15 (627 Views)

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.
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 15 (627 Views)

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...
Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 4 of 15 (627 Views)

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 !

Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 5 of 15 (627 Views)

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 ?
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 15 (627 Views)

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...
Honored Contributor
Posts: 1,007
Registered: ‎03-15-2009
Message 7 of 15 (627 Views)

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.
Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 8 of 15 (627 Views)

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
Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 9 of 15 (627 Views)

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
Acclaimed Contributor
Posts: 26,107
Registered: ‎03-06-2006
Message 10 of 15 (627 Views)

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]*)
Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 11 of 15 (627 Views)

Re: Operations on irregularly named files in a directory

great - thks
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 12 of 15 (627 Views)

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...
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 15 (627 Views)

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...

Advisor
Posts: 21
Registered: ‎04-07-2011
Message 14 of 15 (627 Views)

Re: Operations on irregularly named files in a directory

-
Regular Advisor
Posts: 85
Registered: ‎04-09-2010
Message 15 of 15 (627 Views)

Re: Operations on irregularly named files in a directory

.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.