Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Finding files in unix irrespective of case

by ashok (Sexton)
on Jan 06, 2001 at 05:56 UTC ( [id://50186]=perlquestion: print w/replies, xml ) Need Help??

ashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Here I am finding one problem in unix. I need to inspect some files in a perticular directory. I receive the code belongs to various platforms. I am having a problem if the files belongs to C on Windows platform. They store the files using capitals in filenames like
MYHEADER.H
But in programs they use
#include <myheader.h>
I receive the files MYHEADER.H and when I inspect the code and analyzing I get an error on unix that file
myheader.h
is missing. In this case I create a soft link on unix like
ln -s MYHEADER.H myheader.h
and continue. Some times I get files like Myheader.h. I wanted to write a perl script for such problems. I store all the missing files in one text file called 'missing-headers'. My logic is like this:
1. Open missing-headers. 2. Read a file and check entire directory if any file is found irrespe +ctive of case(how to?). 3. If found create a soft link in the corresponding directory. 4. If not found report file name in a file 'really-missing.txt'.
Can you pl. help me in doing this in a smart & best way? Is there a facility in perl to create soft link? I appreciate your help. Thanks Ashok

Replies are listed 'Best First'.
Re: Finding files in unix irrespective of case
by repson (Chaplain) on Jan 06, 2001 at 06:33 UTC
    Okay maybe this is what you want:
    open MISS, 'missing.txt' or die "Cant open missing: $!\n"; my %missing = map {(lc($_),$_)} <MISS>; close MISS; opendir DIR, '.' or die "Can't open pwd: $!\n"; while (defined($_=readdir(DIR))) { # convert filename to lowercase and see if it is in list if (exists $missing{lc($_)} ) { symlink($_, $missing{lc($_)}); delete $missing{lc($_)}; } } closedir DIR; print "Still not found:\n\n" . join("\n", keys %missing) . "\n";
    Updated: So the hash keys and input filenames are now compared in lowercase, but the symlinks are done with both original cases.
    If you need to rename the files recursivly then be sure to use File::Find.
      Hi, I appreciate your help. In this code you are converting the file name's all characters to lowercase and searching the directory. If user includes the file like
      #include <Myheader.H>
      then your logic will fail. So it requires to check both lower and upper case for each letter in a file name while searching in the directory. Infact we can put all the filenames user supplied in an array and convert those all file names into lower case and create a hash and compare with the file names in "missing.txt" file. But I think it is laborious and may not be a good idea. Thanks Ashok
Re: Finding files in unix irrespective of case
by Fastolfe (Vicar) on Jan 06, 2001 at 06:00 UTC
    You're probably looking for symlink, but if I were faced with this situation I would probably work on standardizing everything on lower-case. If your files are including other files using a mixed case, change it to lower-case, and then run through all of your files and change them all to lower-case. Your problem should then be solved without having to worry about building symlinks or other such manipulations.

    Short of doing that, I would probably modify the files themselves to point to the correct name. I would view building a bunch of symlinks as a last resort.

      Hi, Since I am not supposed to change the code in the files I received, as a last resort I am creating soft links. Take your suggestion. "MYHEADER.H" becomes "myheader.h". In some of the programs(files) user might be including like
      #include <MYHEADER.H>
      Then I will get the error that MYHEADER.H is missing. So renaming will not work here as you suggested as I should not change code in the files. Many times I found user including like:
      #include <Myheader.h>
      In this case also your suggestion will not work. So I was forced to create soft links. First of all I do not know how to find a file with the same name but in different case in perl. I think I am clear to you. Thanks Ashok
Re: Finding files in unix irrespective of case
by mp3car-2001 (Scribe) on Jan 06, 2001 at 09:20 UTC
    Not sure on your compiler or how brave you are, but you could always hack the preprocessor a bit. It seems like if you are using gcc you could edit the preprocessor to try to #include case insensitively, maybe by embedding a little bit of perl in it or just coding it yourself. I have no clue how you'd start to do that, but if you can't change code or their practices, and you get a lot of these files loaded at a time you might run into inode problems eventually. Actually, it would be pretty cool to be able to #include case insensitive and have a gcc command line option for that, make a patch, maybe they'd take it.
      I am working on Sun Sparc. As you said I am not having any administrative previlege. Thanks Ashok

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://50186]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found