shell command for deleting files contained in a batch of subdirectories

Discussion in 'Mac Programming' started by ign, Jul 30, 2009.

  1. ign
    macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #1
    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!
     
  2. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #2
    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. :)
     
  3. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    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
     
  4. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #4
    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?
     
  5. ign
    thread starter macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #5
    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?
     
  6. macrumors 6502a

    hobbbz

    Joined:
    Mar 8, 2005
    #6
    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.
     
  7. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #7
    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.
     
  8. ign
    thread starter macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #8
    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
     

Share This Page