PDA

View Full Version : shell command for deleting files contained in a batch of subdirectories




ign
Jul 30, 2009, 09:06 AM
Hello,

I want to delete a bunch of text files that are in a couple hundred folders located in one folder!
I am looking for the unix shell command to be able to do it in a reasonable time!

>main directory: OUTCOME
>>subdirectories: OUTCOME1, OUTCOME2, OUTCOME3...., OUTCOME450
>>>each subdirectory contains a bunch of text files I want to delete
without deleting the subdirectories!

thank you for your help!



iShater
Jul 30, 2009, 09:16 AM
It won't really be a single command, it would be a script that iterates through the directories.

I am doing something similar at work, I will play around tonight and post back. :)

angelwatt
Jul 30, 2009, 09:22 AM
You can make use of the find command, which can delete items it finds. I use it to delete ._ files off external drives. All in one command.

find . -name '*.txt' -delete

iShater
Jul 30, 2009, 09:29 AM
You can make use of the find command, which can delete items it finds. I use it to delete ._ files off external drives. All in one command.

find . -name '*.txt' -delete

I think the issue is just iterating through the directories, something like:


DIR_LIST="OUTCOME\OUTCOME*`"

for DIR in $DIR_LIST
do
find DIR -name '*.txt' - delete
done


Of course this code IS NOT COMPLETE. The PATH information has to be correct. What is the default shell in OSX? KSH right?

ign
Jul 30, 2009, 09:31 AM
You can make use of the find command, which can delete items it finds. I use it to delete ._ files off external drives. All in one command.

find . -name '*.txt' -delete

But that would erase ANY file .txt it encounters on the way? Where do I specify the folders in which I want the files to be erased?

hobbbz
Jul 30, 2009, 09:33 AM
how many do you want saved? why don't you copy out the ones you want to keep and then do the delete all txt files thing on the remaining folders. then move your keepers back in.

iShater
Jul 30, 2009, 09:39 AM
But that would erase ANY file .txt it encounters on the way? Where do I specify the folders in which I want the files to be erased?

Actually angelwatt's approach works. You just have to specify the path where you want to run and make sure that your wild card captures the names of the files you want.

example:


find OUTCOME\OUTCOME* -name '*.txt'


should give you the full path for each .txt file in the OUTCOME* directories under the OUTCOME parent. Change the file wildcard and see if the resulting list is files you expect to want to delete.

ign
Jul 30, 2009, 10:13 AM
Hello and thank you all for your help so far!

IT WORKED!!!


find /home/ign/Desktop/output_all/* -name '*.txt' -delete


omitting the -delete I had a list of all the textfiles contained in the subfolders,
and with the above line I was able to erase all textfiles from the subfolders of the main output_all directory!!

thanks again,
cheers