-   Linux - Newbie (
-   -   Removing the first n files with specific extensions (

berba9 10-09-2012 07:32 AM

I have a folder containing approximately:

100,000 files of type *.mol2

100,000 files of type *.pdbqt (with the same names as the *.mol2)

and around 16,000 folders named after the 1st 16,000 of these files.

I need to remove the 1st 16,000 files of *.pdbqt.
Is this possible?

The only idea I have is:
Take a folder name as my temp variable, and delete the *.pdbqt file with that name, and repeat...
Is there any script for this?

Please help.

Oops. The title should be removing not renoming.

Mr. Alex 10-09-2012 10:03 AM

You can do this in file manager like Dolphin. It shows how many files are selected at the moment. So you can select first, hold Shift and select last of those 16,000 (use scrollbar). And then delete.

suicidaleggroll 10-09-2012 10:16 AM

Something like this might work:


ls *.pdbqt | head -n 16000 | xargs rm -f
Note that this is untested, make a backup of the files in the folder just in case it does something unpredictable.

berba9 10-10-2012 10:10 AM

I just tried it, and it removed them all!

I hadn't done any backup, but thankfully it's just a tutorial (which had gone wrong and I thought this would be a quick-fix.)

unSpawn 10-10-2012 11:56 AM

A take on suicidaleggroll's suggestion (there's reasons why you shouldn't use 'ls'):

find /some/directory -type f -iname \*.pdbqt printf "rm -f \"%p\"\n"|head -n 16000|/bin/sh
(but then again piping to '/bin/sh' may be bad too in some cases.) And since you managed to fsck up the first time maybe try the elaborate and slower version allowing you to see what it does:

COUNTER=1; find /some/directory -type f -iname \*.pdbqt | while read ITEM; do
 echo rm -f "${ITEM}"
 echo $COUNTER
 [ $COUNTER -eq 1600 ] && exit 0

If you have ensured it does what you want then remove the "echo " in front of the 'rm' command and remove the whole "echo $COUNTER" line.

All times are GMT -5. The time now is 01:57 PM.