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

Hello Perlmonks! So I have a directory(let's call it WEIGHT_HEIGHT) with files as follows:

ID_002_weight.txt ID_002_height.txt ID_003_weight.txt ID_003_height.txt ID_004_weight.txt ID_004_height.txt
What I would like is to make a new directory(let's call it WEIGHT) and put all the weight.txt files in it. So I get all weight files in the new folder as follows:
ID_002_weight.txt ID_003_weight.txt ID_004_weight.txt
Is there a way to do this? I am fairly new to perl, so if you could guide me to some topics or tutorials which would help me achieve this will be really great. Thank in advance!

Replies are listed 'Best First'.
Re: new directory with sorted files
by GrandFather (Saint) on Apr 05, 2012 at 21:56 UTC

    If you are asking (as implied by your question) "Can I write these files to a new directory?" the answer is yes and you already have a few pointers on how to do that.

    If you are asking (as implied by your title) "Can I get these files to be shown in some specific order when I use dir/ls" the answer lies outside of Perl. That is a function of the operating system and the tools provided to show directory contents.

    True laziness is hard work
Re: new directory with sorted files
by choroba (Cardinal) on Apr 05, 2012 at 21:16 UTC
    use File::Copy; mkdir 'WEIGHT' or die $!; for my $file (glob 'ID_*_weight.txt') { copy($file, 'WEIGHT') or die $!; }
    (untested)

    See mkdir, glob and File::Copy.

Re: new directory with sorted files
by mikeraz (Friar) on Apr 05, 2012 at 21:17 UTC

    Assuming:

    /home/perllearner007/WEIGHT_HEIGHT /home/perllearner007/WEIGHT Then fill in the blanks: __intro code __; chdir ____; foreach $filename ( glob "*wei*" ) { link _____; __maybe print out or log a status message __; }
    Reading Perl's Documentation of Functions by Category and checking what is available for files and directories will surely give you other ideas.


    Be Appropriate && Follow Your Curiosity
Re: new directory with sorted files
by ikegami (Patriarch) on Apr 05, 2012 at 21:53 UTC

    Perl? Use bash.

    mkdir WEIGHT mv WEIGHT_HEIGHT/*weight* WEIGHT mv WEIGHT_HEIGHT HEIGHT

      Sure you can use the local shell. But where's the Perl learning opportunity in that? As a side note both of the previous examples will work without bash.


      Be Appropriate && Follow Your Curiosity

        But where's the Perl learning opportunity in that?

        Using the correct tool for the job is a lesson that also applies to Perl.

        As a side note both of the previous examples will work without bash.

        I don't get your point. As far as I can tell, you are falsely implying it will work with all interpreters or shells, or you expect me to list all of them in which the code will work even though I don't know that list.

      Same opinion, if you can, use bash for this. You can use perl of course, just remember that all files in your directory will be always sorted, so you don't need really to care about this. If you want to do this with perl take a look to mkdir and rename. If you use copy instead rename you'll need also to use unlink (that can be a little scary for a novice)

Re: new directory with sorted files
by nemesdani (Friar) on Apr 06, 2012 at 06:48 UTC
    Not closely related to the question, but a suggestion nevertheless:
    I find trial-and-error a more efficient method for learning. If you have a problem, try to solve it, if you're stuck, ask for help. People here at PM are very friendly and ready to help - I know i firsthand, I too posted some rookie questions. But as the saying goes, stupidest question is a question not asked.
    Have fun coding!

    I'm too lazy to be proud of being impatient.
Re: new directory with sorted files
by perllearner007 (Acolyte) on Apr 06, 2012 at 16:01 UTC

    Thank you monks for all your inputs! This was such a help. What I gather is glob does the trick. So I read up about it on perldoc.perl.org... I am working on it now..I am sure to make it work with some more directories. Thanks again!