Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ign

macrumors member
Original poster
Mar 16, 2004
37
1
Firenze
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!
 
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. :)
 
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.

Code:
find . -name '*.txt' -delete
 
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.

Code:
find . -name '*.txt' -delete

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

Code:
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?
 
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.

Code:
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?
 
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.
 
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:

Code:
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.
 
Hello and thank you all for your help so far!

IT WORKED!!!

Code:
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.