You are here:

Unix/Linux OS/-delete flag with find

Advertisement


Question
Hi!

I am trying to delete all files and folders that find returns using the following command.  It works, but not for directories that have contents.  How do I need to modify the statement?

find . -name "foo*" -delete

Thanks in advance,

Tim

Answer
Hi Tim.

Command "find" is very powerful with "-exec" flag. It can execute anything .

find . -name "foo*" -prune -exec rm -rf {} \;

This will remove any such file/folder found. "-prune" is to stop it from descending into a folder that has been selected, to avoid messages about "file not found" (since they are being deleted).

Nix.

Unix/Linux OS

All Answers


Answers by Expert:


Ask Experts

Volunteer


nix

Expertise

UNIX, programing, C, C++, awk, shell scripts, Web, HTTP, Apache, firewall, AltaVista firewall, E-Mail, sendmail, IMAP4 POP3, DNS, NTP, system administration, TCP/IP, security

Experience

UNIX
Digital UNIX
AltaVista Firewall
TCP/IP

Education/Credentials
BSCS

©2016 About.com. All rights reserved.