Linux / UNIX: Deleting Files In Many Subdirectories

by on November 6, 2010 · 3 comments· LAST UPDATED November 6, 2010

in

How do I delete all files (only files and not subdirs) located in /var/pub/ftp/incoming and its subdirectories under Linux or UNIX operating systems?

You can use any one of the following command:
# find /var/pub/ftp/incoming -type f -delete
Your find command may not support -delete option, so use it as follows:
# find /var/pub/ftp/incoming -type f -print0 | xargs -0 -I file rm -f file
OR
# find /var/pub/ftp/incoming -type f -exec rm -f {} \;
You can also select file types. In this example, delete all *.exe files, enter:
# find /var/pub/ftp/incoming -type f -iname "*.exe" -exec rm -f {} \;

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 satish dubey November 19, 2012 at 8:58 am

find . -name *.* | xargs rm -rf *.*

Reply

2 dude May 14, 2013 at 9:28 am

No! That command does not do what you think it does.

Your version: xargs rm -rf *.*
is NOT tied to what is found by the find . -name part. If someone took that command as a template but instead of find *.* did something like find myfile123.*, your command would still delete everything, not just myfile123.*.

Reply

3 tesla October 24, 2013 at 1:46 pm

Thanks a lot, that worked out very well.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: