Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    1

    Unanswered: bash - rename files in a loop

    Hi,
    I need to rename the resulted searched files from a loop

    I have the following code:

    find . -name DOC* | while read i
    do
    find $i -type f -name '*.txt'
    done

    basically, I am searching for all txt files inside any folder starting with DOC name.
    this code is working fine with me.
    I need to rename those .txt files to .txtOLD

    OS: Ubuntu 10.4
    Bash shell

    how can i do that , appreciate your help
    Thank you,

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    find $i -type f -name '*.txt' -exec mv {} {}OLD \;
    If there is a large number of files, consider using the -xargs option instead of -exec
    Or the long way, which gives you a chance to check that you have the right list.

    Code:
    find . -name DOC* | while read i
    do
    find $i -type f -name '*.txt' >list
    done 
    OLD=OLD
    while read $i
    do
    mv $i $i$OLD
    done <list

  3. #3
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    You could also use the "mmv" tool if that is available on your system. Untested:
    Code:
    mmv ";DOC*;*.txt" "#1DOC#2#3#4.txtOLD"
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

Posting Permissions

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