Operating System - HP-UX
1752571 Members
5252 Online
108788 Solutions
New Discussion юеВ

Re: Need a script for finding bad file names.

 
SOLVED
Go to solution

Need a script for finding bad file names.

I work on an L2000 HP server running HP-UNIX 11.0 with an Informix database.

The problem is that I have users who continue to write bad sqls. These bad sqls created erroronous files with bad file names. These bad file names have been know to cause my ingite backup tape creation to fail. Therefore, I need to make every attempt to ensure that these bad file names are not on the server prior to creating the ignite. To add to the problem, I need to search through all the directories for these files, and the system has thousands of directories. What I am looking for is a script that can search through all the directories for bad file names. A bad file name is any that contains a character other than [a-z],[A-Z],[0-9],".","-", and "_". In otherwords files that contain any of the following "!@#$%^&*()+=[]{}<>?:;~ and `. Plus I have found files that contain a space and others that have special symbols like greek letters. Not sure how that happens, but it does. Below is a perl script that someone gave me a while back and it works for finding the special symbols files, greek letters, but the script doesn't find files that contain the previously mentioned listing, i.e. "!@#$%..." Can any one help? I bet all this will take is to add the previously mentioned list to the filter below, but I am unfamiliar with PERL and don't know how to code it.

Perl Script

$filter = "[a-zA-Z0-9_\.\/\ -\s\{}]";
# regex filter
print "What path to search? (i.e. /var/): ";
$inp_dir = ;
chop $inp_dir;
# remove newline from input
@files = `find $inp_dir`;
# store file list
foreach $line (@files) {
@chars = split(/ */, $line);
# break up each line
foreach $letter (@chars) {
if ($letter !~ /$filter/){
# find bad characters
print $line;
undef @chars;
} #end if
} #end foreach
} #end foreach

Please help.
6 REPLIES 6
Cheryl Griffin
Honored Contributor

Re: Need a script for finding bad file names.

My initial thoughts are that you are checking for obvious bad filenames but Ignite will also fail on files that have characters which you do not see, such as spaces (before or after the name) and control characters.

These can be seen with vis:
# ll | vis

"Downtime is a Crime."

Re: Need a script for finding bad file names.

Yes Cheryl you are correct, also need it to find spaces (before or after the name) and control characters, but I need a script to search through all the directories. There are thousands of directories on my system.
Chris Wilshaw
Honored Contributor

Re: Need a script for finding bad file names.

To find files with control characters, you can use the following (makes use of the vis command mentioned before)

find / -print | vis | grep '\\'

This will find any file with (for example) ^H or ^C in the filename, reporting them in the ascii format

I created a test file called t^C^Hest

The above find command shows it as

t\003\best
Hein van den Heuvel
Honored Contributor
Solution

Re: Need a script for finding bad file names.

Hmmm... I really thought that my little perl script I replied with when you errorneously posted in the image forum solve the problem!
How did it fail for you?

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=499770

Here it is again in two flavors...
RUn stand alone with optional directory argument:

#!/bin/perl
$dir = shift(@ARGV);
$glob = ($dir) ? $dir . "/*" : "*";
print "- $glob -\n";
while (<${glob}>) {
$files++;
$file = $_;
s/\w+//g;
s/[\.\-\/]//g;
print $dirty++ . " $file\n" if ($_ ne "");
}
printf "Found $files files, $dirty with a problem in the name\n";

And the next where you run it at the end of a pipe like:
ls | check_names

or

find /somedir | checknames



#!/bin/perl
while (<>) {
chop;
$files++;
$file = $_;
s/\w+//g;
s/[\.\-\/]//g;
print $dirty++ . " $file\n" if ($_ ne "");
}
printf "Found $files files, $dirty with a problem in the name\n";



Cheers,
Hein.

Re: Need a script for finding bad file names.

Hein,

The first perl script gave me the following results.
- * -
Found 87 files, with a problem in the name

The second perl script game me the following results. It listed the files.

...
filename
filename2
Found 126239 files, 821 with a problem in the name

Which is great, but I was unaware of how often the following characters },!,@,+,= were used in system file names.
Thanks.
Hein van den Heuvel
Honored Contributor

Re: Need a script for finding bad file names.

Thanks for the feedback!

I now see I managed to post a debug version.
In the first script remove the line: "print "- $glob -\n";

Both scripts could use an explicit $dirty=0;

It would be trivial to add say a "+" (from lost+found :-) to the second replace list.

Cheers,
Hein.