Hi Wise Monks!

I'm trying to write a script that will loop through a bunch of files and redistribute them into a "directory tree". As you can see from my code, I match some useful things in the files' names, such as the variables $mol and $dir that I use later to create the directories where I will move my files.

Directory $mol is always the same and will in the end contain lots of different $dir directories. So that I make this a bit clear, all these files belong to the same pattern, let's say $mol = alcohol, but have multiple $dir values, such as primary, secondary, tertiary, and so on.

My code works, although I had to use option -p on mkdir otherwise I got the warning that directories existed. Naturally, when I loop through the files and start creating the directory tree, directory $mol exists after going through the loop for the first time and matching the first file, so the second time my script matches a file, this time by a different $dir value, mkdir complains that directory $mol exists. This was solved by using option -p, for which the man page says: -p, --parents: no error if existing, make parent directories as needed.

Now my concern is, is there a chance that mkdir may overwrite a directory, thus leading to loss of data? I've tried testing it by running the script multiple times and throwing in ~/folder1/$mol various test directories and test files. I've also put some test files into some of the ~/folder1/$mol/$dir directories and then ran the script again, after adding more and new $txt files to loop through - the test files are intact and more $dir directories are created, as intended.

However I'm getting stressed that I haven't tested enough or that there's something I'm missing - can you help? I've also looked at the mkdir man page and generally googled "can mkdir overwrite directories" and it seems that it can't. But another opinion would also be useful. Thank you all and happy holidays! :)

#/bin/perl/ use strict; use warnings; my $txt = "txt"; my @files; @files = `ls *$txt`; my $filename; my $seq; my $nr; my $mol; my $dir; my $step; my $type; foreach (@files) { /(prefix)(_)(\d+)(_)(\d+)(_)(\w+)(_)(\w+)(_)(\w+)(\.)(\w+)/; $filename = "$1$2$3$4$5$6$7$8$9$10$11$12$13"; print "name of file: $filename \n"; print "\n"; $seq= $3; $nr = $5; $mol = $7; $dir = $9; $step = $11; $type = $13; print `mkdir -p ~/folder1/$mol/$dir`; }

In reply to mkdir in loop - overwrite danger? by fasoli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.