Rename files in linux / bash using mv command without typing the full name two times

Instantly share code, notes, and snippets.

Embed

What would you like to do?

Rename files in linux / bash using mv command without typing the full name two times

# Put this function to your .bashrc file.
# Usage: mv oldfilename
# If you call mv without the second parameter it will prompt you to edit the filename on command line.
# Original mv is called when it’s called with more than one argument.
# It’s useful when you want to change just a few letters in a long name.
function mv() {
if [ $# -ne 1 ]; then
command mv $@
return
fi
if [ ! -f $1 ]; then
command file $@
return
fi
read -ei $1 newfilename
mv -v $1 $newfilename
}

This comment has been minimized.

Example:

This comment has been minimized.

You don’t need a function as this is bash batteries included:

% mv photo/2020/01/01/{IMG,img}0001.jpeg

Also works in zsh shell.

This comment has been minimized.

In my example I also changed jpeg to jpg at the same time, but this is just an example. I wanted to have an equivalent of just pressing F2 in Windows and changing a small part of a long name easily in general

This comment has been minimized.

Makes sense and I like it. These kind of shortcuts are the best. Thanks for sharing.

This comment has been minimized.

You don’t need a function as this is bash batteries included:

% mv photo/2020/01/01/{IMG,img}0001.jpeg

Also works in zsh shell.

wow. how did this elude me. thank yoU!!!

Read More