+ Reply to Thread
Results 1 to 3 of 3

Thread: Removing files in bulk

 
  1. #1
    Contributing User
    Join Date
    May 2011
    Posts
    166
    Rep Power
    103

    Default Removing files in bulk

    This morning I had to remove all .ttx files from a number of directories. The way I normally do it didn't work because there were spaces in the path (directories named "Batch 1", etc):

    pabloa:~/Development$ for x in `find * -name *.ttx`; do echo $x; done | head
    batch
    1_Bilingua
    TTX/Uncleaned/317.xml.ttx
    batch
    1_Bilingua
    TTX/Uncleaned/670.xml.ttx
    batch
    1_Bilingua
    TTX/Uncleaned/474.xml.ttx
    batch

    pabloa:~/Development$ for x in `find * -name *.ttx`; do rm $x; done
    rm: cannot remove `batch': No such file or directory
    rm: cannot remove `1_Bilingua': No such file or directory
    rm: cannot remove `TTX/Uncleaned/317.xml.ttx': No such file or directory
    rm: cannot remove `batch': No such file or directory
    rm: cannot remove `1_Bilingua': No such file or directory
    rm: cannot remove `TTX/Uncleaned/670.xml.ttx': No such file or directory
    rm: cannot remove `batch': No such file or directory
    rm: cannot remove `1_Bilingua': No such file or directory
    rm: cannot remove `TTX/Uncleaned/474.xml.ttx': No such file or directory
    rm: cannot remove `batch': No such file or directory

    Actually the proper way of working with results found by "find" is to instruct "find" itself to execute a command:

    pabloa:~/Development$ find . -name *.ttx -exec echo {} \;
    ./batch 2_Bilingual TTX/Uncleaned/4520.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4504.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4508.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4501.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4499.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4515.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4521.xml.ttx
    ./batch 2_Bilingual TTX/Uncleaned/4519.xml.ttx

    Although the syntax is a bit awkward, it's worth getting used to it. Here "{}" gets replaced by the names of the files found. And the semicolon has to be escaped "to protect them from expansion by the shell" as it says in the man page.

    Cheers.
    P.

  2. #2
    Senior Member
    Join Date
    May 2006
    Posts
    806
    Rep Power
    100

    Default Re: Removing files in bulk

    Guauuu! It's great to have a programmer in the gang! :-)

  3. #3
    Contributing User
    Join Date
    May 2011
    Posts
    166
    Rep Power
    103

    Default Re: Removing files in bulk

    Quote Originally Posted by gentle View Post
    Guauuu! It's great to have a programmer in the gang! :-)
    Gentle, I'm glad you like it and I hope you find it useful. But in any case, in the same way than friends that greet each other with "how do you do?" are really saying "I love you", when I say that I do things in this or that way I'm really saying "does anyone know a better way of doing it?".

    Cheers.
    P.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •