Search code examples
linuxrm

Remove only files in directory on linux NOT directories


What delete command can be run to remove only files in given directory

  • NOT directories
  • NOT sub-directories
  • NOT files in these sub-directories.

Some files don't have extensions so rm *.* wont work...

There are thousands of files in this folder.

Any advice?


Solution

  • What worked for me is a PERL script:

    perl -e 'chdir "subdirectory_name" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'
    

    Run this one level up from the directory you wish to clean: replace "subdirectory_name" with the directories name.

    Worked on millions of files without killing the CPU.