AndrewC AndrewC - 1 year ago 110
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download