htmanning has asked for the wisdom of the Perl Monks concerning the following question:
I've also tried this...$deleteregdir = "$rootpath/$username"; rmdir ($deleteregdir);
and I've tried this...$deleteregdir = "$rootpath/$username"; rmdir -r ($deleteregdir);
All three leave the directory there if it is not empty. If it's empty it gets deleted.$deleteregdir = "$rootpath/$username"; system ("rmdir -r $deleteregdir");
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Removing Directory and Contents with Perl
by bobf (Monsignor) on Mar 01, 2007 at 22:16 UTC | |
If you want to delete a directory tree (including the contents), use the rmtree function in File::Path. In addition, you may be interested in File::Spec, which can be used to portably perform operations on path names. Finally, if you want to stick with rmdir, you should also look at unlink. | [reply] [d/l] |
|
Re: Removing Directory and Contents with Perl
by ptum (Priest) on Mar 01, 2007 at 22:31 UTC | |
Perhaps what you want, for at least some unix variants, is something like this:
| [reply] [d/l] |
by shandor (Monk) on Mar 02, 2007 at 02:35 UTC | |
I would put some strict testing around an "rm -rf" command, especially if you're running this script as root/Admin. We recently lost several TB of data because a script didn't set the directory variables correctly and ran "rm -rf /*" (instead of "rm -rf $path_to_home/*") as root. | [reply] |
|
Re: Removing Directory and Contents with Perl
by grinder (Bishop) on Mar 02, 2007 at 08:38 UTC | |
If you want to delete an entire tree of directories in the file system, you want to use rmtree from the File::Path module (it's part of the standard distribution, you already have it on your system). It takes a reference to a list of directory starting points, so it looks like this:
You don't actually have to specify the 'rmtree' function in the use statement, as it gets exported (made available) by default, but I like to do so anyway, so that subsequent maintainers can see quickly where a function came from. This is especially important when you have half a dozen modules bringing in bits and pieces. If you had an array of user directories you wanted to remove, you could fill the list via a map:
The reason it uses this slightly odd approach is because there are two little-known, rarely-used positional control parameters you can tack on the end:
The above call will behave "verbosely", printing out what it's doing as it goes about its job. There's a second parameter that you can also set to a true (non-zero, 1 by convention) value, that will instruct rmtree to skip the attempt at deleting something for which the process lacks the necessary delete or write privileges (on platforms like VMS where that makes sense). I've never really understood the purpose of this, since whether you ask the function to skip the files, or let it attempt to do so anyway, and fail, the result is the same: there will be some files that will not be deleted. But it usually works out as it should. If the rmtree fails, an equivalent rm -rf would as well. The uselessness of the second parameter notwithstanding, it should be pointed out that this sort of interface is positively archaic, and not to be emulated if you're just starting out. The reason being is that if you want to change the default skipping behaviour by setting the second control parameter, you are forced into a decision about what to set the first control parameter: 0 or 1. What does it do again? Which one sets the print behaviour, the first or the second? There are better interfaces available: either using objects with chained methods to change the behaviour, or just simply a series of public routines like rmtree, rmtree_print and so on. • another intruder with the mooring in the heart of the Perl | [reply] [d/l] [select] |
|
Re: Removing Directory and Contents with Perl
by Moron (Curate) on Mar 02, 2007 at 14:54 UTC | |
...would at least have told you in fairly plain English why that use of rmdir didn't work. -M Free your mind | [reply] [d/l] |
by digitalpbk (Initiate) on Nov 26, 2009 at 12:06 UTC | |
This is the code I wrote for my friend to delete directories. Simple no modules, just plain pure PERL. Readmore over here | [reply] [d/l] |
by afoken (Chancellor) on Nov 28, 2009 at 22:39 UTC | |
--, because that code just hurts my eyes, even more than "PERL". ( The name of the language is "Perl", the name of the executable for running scripts is "perl", but there is no "PERL".) Problems with the code, from first to last line: You might notice that the code contains exactly one non-empty line that I did not critisize. It's the final line "}". Problematic recursion: In the worst case (deep tree where readdir returns subdirectories before files), this keeps a list of all files in the entire directory tree in memory. Wrong tool: Using no modules here is not only stupid, but dangerous. Your code is broken in every single line except for the final bracket, uses far too much resources, and does not do what it promises to do. If your friend really runs this code, he should better get someone else to write code for him until you learned to write safe and clean code. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] [select] |
by Anonymous Monk on Nov 14, 2018 at 09:39 UTC | |
| [reply] |