André Foote
André Foote

Reputation: 368

How to rename a copied file without renaming the original

I copy the files in my home directory to a new directory. When I attempt to rename a file in this new directory, the file in the home directory is changed and the file in the new directory retains its name. My code is shown below.

srcDir=$HOME
newDir=$1
mkdir $1

cp $srcDir/* $newDir

for file in newDir/*; do
    filename=$(basename $file)
    if [[ SOMETHING ]]; then
        mv $filename newname
    fi
done

If anyone can tell me where I'm going wrong it'd be much appreciated.

Upvotes: 0

Views: 75

Answers (1)

choroba
choroba

Reputation: 241868

You are renaming files in the current working directory, as with

mv ./$filename newname

You need to prepend the path:

mv newDir/"$filename" newDir/newname

Or, change the working directory:

cd newDir
for file in * ; do
    mv "$file" newname
done

Upvotes: 2

Related Questions