fasoli has asked for the wisdom of the Perl Monks concerning the following question:
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`; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: mkdir in loop - overwrite danger?
by GotToBTru (Prior) on Dec 22, 2015 at 20:44 UTC | |
by afoken (Chancellor) on Dec 22, 2015 at 22:36 UTC | |
by GotToBTru (Prior) on Dec 23, 2015 at 13:15 UTC | |
|
Re: mkdir in loop - overwrite danger?
by RichardK (Parson) on Dec 22, 2015 at 18:41 UTC | |
|
Re: mkdir in loop - overwrite danger?
by graff (Chancellor) on Dec 22, 2015 at 23:09 UTC | |
|
Re: mkdir in loop - overwrite danger?
by Anonymous Monk on Dec 22, 2015 at 18:37 UTC | |
|
Re: mkdir in loop - overwrite danger?
by Marshall (Canon) on Dec 23, 2015 at 08:59 UTC |