Delete subdirectories and files in a given directory

I want to delete all subdirectories and files from the directory, but not the directory itself. For example, if I pass the Sample directory into a variable and the Sample directory contains three subdirectories and 3 files, I want to delete all 3 of these directories and 3 files. In practice, the Sample directory can contain many subdirectories and files.

+4
source share
1 answer

ETA: This is valid in perlfaq5: How to delete a directory tree?

Use File::Path , the main module.

 perl -MFile::Path=remove_tree -we 'remove_tree("Sample",{keep_root=>1}) or die $!' 

The keep_root parameter keep_root remove_tree to save the top directory:

keep_root => $ bool

If set to true, all files and subdirectories will be deleted, except for the originally specified directories. This happens conveniently when cleaning the directory from scratch of the application.

+9
source

Source: https://habr.com/ru/post/1401990/


All Articles