Linux / UNIX: Deleting Files In Many Subdirectories

Posted on in Categories last updated November 6, 2010

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

3 comment

    1. 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.*.

Leave a Comment