1

So I have this function

ren_barrow(){
    for file in *.clu
    do
        mv -f $file ${file//RELEASE/"$1"}
    done
}

but when I run this, as you can see everyfile that ends in .clu will be renamed.

I want to put an if statement around the mv that says if the filename contains RELEASE in it then carry on. If I do not have this if statement I get errors saying

Cannot rename: $file and $file is the same file.

ren_barrow(){
    for file in *.clu
    do
        # if $file grep "RELEASE"; then
            mv -f $file ${file//RELEASE/"$1"}
        # fi
    done
}

2 Answers 2

4

You can have your mv command like this:

[[ "$file" != *"RELEASE"* ]] && mv -f "$file" "${file//RELEASE/$1}"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, would you mind explaining how this statement works?
This condition [[ "$file" != *"RELEASE"* ]] is using a glob pattern to determine whether $filename doesn't already contain text RELEASE in it.
Also see guide on BASH Globbing
2

Why not change your for loop so that you don't have to iterate over all the files?

for file in *RELEASE*.clu
do
    mv -f "$file" "${file//RELEASE/$1}"
done

Even better, use the rename command if you have it:

rename RELEASE "" *RELEASE*.clu

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.