Remove Old Files From Directory

Query:

I'm looking for a perl script or bash to delete ALL files from a folder and all subfolders, but it should NOT delete any folders & subfolders.

Solution:

Well it could look something like this if it was run from in the directory that you want to remove the contents from.

Code:

 rm */*/*/*
 rm */*/*
 rm */*
 rm *

This goes down three levels plus the origional directory.

But be carful with the 'rm' command once used there is no getting it back.
 

or 
 

If you can find a way of skipping directories with: 

Code:

rm -rf directory/*
 

or
 

Easy, no perl needed, just one line bash. Make sure you run it in the directory you want to have your files (only) removed

Code:

find . -type f -exec rm -rf {} \;
 

or

This code was originally written to delete all folders and subfolders with in a directory. Commenting out rmdir can suit your need.

#!/usr/bin/perl
deldir("test"); # or deldir($ARGV[0]) to make it commandline

sub deldir {
my $dirtodel = pop;
my $sep = '\\'; #change this line to "/" on linux.
opendir(DIR, $dirtodel);
my @files = readdir(DIR);
closedir(DIR);

@files = grep { !/^\.{1,2}/ } @files;
@files = map { $_ = "$dirtodel$sep$_"} @files;
@files = map { (-d $_)?deldir($_):unlink($_) } @files;

#rmdir($dirtodel);
}

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.