≡ Menu

Linux / UNIX: Deleting Files In Many Subdirectories

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 {} \;

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 3 comments… add one }

  • satish dubey November 19, 2012, 8:58 am

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

    • dude May 14, 2013, 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.*.

  • tesla October 24, 2013, 1:46 pm

    Thanks a lot, that worked out very well.

Leave a Comment