Gossamer Forum
Home : General : Perl Programming :

how to delete all files in a directory?

Quote Reply
how to delete all files in a directory?
What is the command to delete all files in a directory?
Quote Reply
Re: how to delete all files in a directory? In reply to
From perldoc -f unlink:

Quote:
unlink LIST
unlink Deletes a list of files. Returns the number of files successfully deleted.

$cnt = unlink 'a', 'b', 'c';
unlink @goners;
unlink <*.bak>;

Note: `unlink()' will not delete directories unless you are superuser and the -U flag is supplied to Perl. Even if these conditions are met, be warned that unlinking a directory can inflict damage on your filesystem. Use `rmdir()' instead.

If LIST is omitted, uses `$_'.

I hope this helps.

[This message has been edited by Bobsie (edited June 06, 1999).]
Quote Reply
Re: how to delete all files in a directory? In reply to
If you want to wipe a directory and all subdirectories, might also want to look at rmtree in the File::Path module.

Cheers,

Alex
Quote Reply
Re: how to delete all files in a directory? In reply to
Some thing to ask....
if I put:

unlink <*.bak>;

in the script, will I delete all the file with .bak extension in the same directory?
so if I put:

unlink <some_directory/*.*>;

will this delete all files in some_directory??