Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Directory or File

  • 07-07-2003 9:00pm
    #1
    Registered Users, Registered Users 2 Posts: 1,722 ✭✭✭


    I'm writing a perl script at the moment and I have to loop through a file structure and do certain things to the objects in each directory depending on if they're files or directories.

    At the moment I've hacked a solution where I do an ls on everything in the directory and depending on what's returned decide if its a file or a directory. IE if you do ls on a file it just returns the file name while if you do a ls on a directory it will return the contents of that directory. This isn't bullet proof tho because if a directory contains only one file which is the same name of its parent directory then you'll think a directory is a file. I was thinking of just using the more command which returns *** <directory name>: directory *** if its a directory and then the contents of the file if its not but if you do a more on an image file it can cause errors.

    Are there any Unix commands I can use which will tell me right away whether what my script is looking at is a file or directory? Failing that does anyone have any other suggestions on a different way to go about finding out if something is a file or directory? Any help is appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 2,755 ✭✭✭niallb


    If you're shelling out to run 'ls' anyway,
    you could use the 'file' command which is very versatile.

    It will return name: directory if it's a directory.

    You can also test for it in bash,
    as in [ -d name ] will return true if name is a directory.
    [ -d name ] && echo IS A DIRECTORY
    [ -d name ] !! echo IS NOT A DIRECTORY
    whichever suits best.

    not 100% on the equivalent perl syntax,
    though if you tried to open a file descriptor
    on it and it failed, it MIGHT be a directory :-)

    ls -l would have given you enough info,
    as the attributes would begin with a d.

    If you have to loop through a large file structure,
    you should take a look at the find command.
    It can scan through a directory tree and find for - example - all directories,
    and either return a list of them or perform an action on them.
    Might suit your script down to the ground.

    Best of luck,
    Niall B.


  • Registered Users, Registered Users 2 Posts: 1,186 ✭✭✭davej


    No No No!

    There are built in perl commands to handle this.

    look at the alphabetical list of functions in perldoc perlfunc


    if (-d '/notsure') { # -d tests a file to see if it's a directory
    print "this is a directory";
    }
    else {
    print "this is not a directory";
    }



    To get a list of files (including directories) in a directory do this:

    opendir (DIR, '/path/to/dir') || die "Couldn't open dir: $!";
    @list=grep(!/^\.\.?$/, readdir DIR); # @list now contains filenames


    davej


  • Closed Accounts Posts: 304 ✭✭Zaltais


    If you're recursing subdirectories

    use File::Find;

    find (\&dosomething, '/start/directory');


    sub dosomething {

    #rob davej's example and modify it
    if (-d $File::Find::name) { # -d tests a file to see if it's a directory
    print "$File::Find::name is a directory\n";
    }
    elsif (-f $File::Find::name) {
    print "$File::Find::name is a file\n";
    }

    }


    There are also checks for special files, text files, files with the executeable bit set, readable files etc, etc, etc....

    Read up on File::Find (which is a native perl implementation of the find command suggested by niallb) and as davej said the alpabetical functions.

    Perldoc is dead handy.

    And cos I'm feeling generous this morning....

    Alphabetical functions

    File::Find

    Probably better suited to programming too.


  • Registered Users, Registered Users 2 Posts: 2,755 ✭✭✭niallb


    Originally posted by davej
    No No No!
    There are built in perl commands to handle this.

    What's with the "No No No" ?
    How about 'This is how you'd do it in perl'

    Seeing as the script itself is in perl, it's a cleaner way of doing it,
    but the QUESTION was
    Are there any Unix commands I can use which will tell me right away whether what my script is looking at is a file or directory?


  • Registered Users, Registered Users 2 Posts: 1,722 ✭✭✭Thorbar


    Thanks for all the suggestions lads. Don't know why I didn't think of using the perl built in functions, I've used them in other projects before but on the current project I'm working on I'm just adding new features to an older script and I was sort of doing things the same way the original programmer had. Anyway thanks again for all the links and suggestions.

    Barry


  • Advertisement
Advertisement