AndrewC AndrewC - 6 months ago 20
Linux Question

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 subdirectories

  • NOT files in these subdirectories.

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

There are thousands of files in this folder.

Any advice?


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.