Skip to content

Instantly share code, notes, and snippets.

@premek

premek/mv.sh

Last active Sep 30, 2020
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.
#
# Also see:
# - imv from renameutils
# - Ctrl-W Ctrl-Y Ctrl-Y (cut last word, paste, paste)
function mv() {
if [ "$#" -ne 1 ] || [ ! -e "$1" ]; then
command mv "$@"
return
fi
read -ei "$1" newfilename
command mv -v -- "$1" "$newfilename"
}
@premek

This comment has been minimized.

Copy link
Owner Author

@premek premek commented Apr 13, 2020

Example:
asciicast

@namsral

This comment has been minimized.

Copy link

@namsral namsral commented Apr 13, 2020

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.

@premek

This comment has been minimized.

Copy link
Owner Author

@premek premek commented Apr 13, 2020

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

@kinduff

This comment has been minimized.

Copy link

@kinduff kinduff commented Apr 13, 2020

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

@kittles

This comment has been minimized.

Copy link

@kittles kittles commented Apr 13, 2020

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!!!

@dvhwgumby

This comment has been minimized.

Copy link

@dvhwgumby dvhwgumby commented Apr 13, 2020

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 is just standard shell glob behavior BTW so if it doesn't work in your shell plenty of other things probably won't either.

Note also the [] glob syntax.

@adamdonahue

This comment has been minimized.

Copy link

@adamdonahue adamdonahue commented Apr 13, 2020

There are even more flexible ways (in bash, at least), for example (here renaming .txt to .json):

$ touch config.txt
$ mv config.txt !#:1:s/txt/json
@cellularmitosis

This comment has been minimized.

Copy link

@cellularmitosis cellularmitosis commented Apr 13, 2020

There is also imv:

$ brew install renameutils || sudo apt-get install renameutils
$ imv IMG0001.jpeg
@lgommans

This comment has been minimized.

Copy link

@lgommans lgommans commented Apr 13, 2020

Looks like we keep reinventing this :)

I made this in 2018 (https://github.com/lgommans/vinamer) and recently discovered this was a reinvention of a 2017 project (https://github.com/thameera/vimv).

@premek

This comment has been minimized.

Copy link
Owner Author

@premek premek commented Apr 13, 2020

There is also imv

Hah, perfect, imv is exactly what I was missing for years :)

The imv ("interactive move") program, is trivial but useful when you are too lazy to type (or even complete) the name of the file to rename twice. It allows a file name to be edited in the terminal using the GNU Readline library.

@Congee

This comment has been minimized.

Copy link

@Congee Congee commented Apr 13, 2020

You can also do Ctrl-W followed by Ctrl-Y after typing the path, if you are using readline style keybindings. Mainstream shells come with readline style bindings by default.

@rileytg

This comment has been minimized.

Copy link

@rileytg rileytg commented Apr 13, 2020

You can also do Ctrl-W followed by Ctrl-Y after typing the path, if you are using readline style keybindings. Mainstream shells come with readline style bindings by default.

excellent! any good resources to learn more of these?

@lgommans

This comment has been minimized.

Copy link

@lgommans lgommans commented Apr 14, 2020

@rileytg man readline (sorry for the terse answer but that's the library bash uses and where you will find, in long form, what options are available)

@markstos

This comment has been minimized.

Copy link

@markstos markstos commented Apr 14, 2020

With the fish shell, you can type Alt-E to edit any command line in your favorite editor where you likely already know shortcuts for copying, pasting and modifying. The great thing is that is a general solution that works for any command line edits.

@kesor

This comment has been minimized.

Copy link

@kesor kesor commented Apr 14, 2020

@markstos if your bash is working in vi mode by using set -o vi you can also use ESC - v to enter vim and edit your command there

@it6c65

This comment has been minimized.

Copy link

@it6c65 it6c65 commented Apr 14, 2020

There are even more flexible ways (in bash, at least), for example (here renaming .txt to .json):

$ touch config.txt
$ mv config.txt !#:1:s/txt/json

You can do it even shorter, all is for the flexible style of bash and similaires, that generally works in zsh too, repeating with your same example:

$ touch config.txt
$ mv !$ !#:1:s/txt/json
@jlevy

This comment has been minimized.

Copy link

@jlevy jlevy commented Apr 14, 2020

Yeah, many ways to do this but would argue readline's Ctrl-W Ctrl-Y Ctrl-Y is cleanest since it's useful to know anyway and available almost anywhere. :)

Screen Recording 2020-04-13 at 06 43 PM

@alhirzel

This comment has been minimized.

Copy link

@alhirzel alhirzel commented Apr 14, 2020

I also want to point out roamer, where you can launch roamer . and then use $EDITOR to bulk edit multiple filenames at once. https://github.com/abaldwin88/roamer

@madprops

This comment has been minimized.

Copy link

@madprops madprops commented Apr 14, 2020

Just thinking that if you want to call the mv command again when pressing up, after using it to change the name of a file, it will use the original file name, forcing the user to adjust the name, for instance if you mv'd zoo to zoo2 it will say 'mv zoo' on up arrow (history up), which can't be used without adjusting it - maybe something could be done like storing the last filename and editing it when using 'mv' without arguments, for instance 'mv' will show 'zoo2' ready to be edited

@immanuelfodor

This comment has been minimized.

Copy link

@immanuelfodor immanuelfodor commented Apr 14, 2020

Welcome to HackerNews front page #1: https://news.ycombinator.com/item?id=22859935

@mjrosenb

This comment has been minimized.

Copy link

@mjrosenb mjrosenb commented Apr 14, 2020

Since we seem to be jumping on the 'alternate solution' bandwagon, I'll point out that typing C-x C-e at a bash prompt will open the currently typed command in an $VISUAL or $EDITOR, and execute whatever is saved when the editor exits.

@rohan-mo

This comment has been minimized.

Copy link

@rohan-mo rohan-mo commented Apr 14, 2020

C-x C-e gives me -bash: emacs: command not found

@vasergen

This comment has been minimized.

Copy link

@vasergen vasergen commented Apr 14, 2020

you can use as well rename

@niderhoff

This comment has been minimized.

Copy link

@niderhoff niderhoff commented Apr 14, 2020

C-x C-e gives me -bash: emacs: command not found

then you need to change $EDITOR to some editor that is installed on your system, I think?

@premek

This comment has been minimized.

Copy link
Owner Author

@premek premek commented Apr 14, 2020

Ctrl-W Ctrl-Y Ctrl-Y is good, I didn't know about that

@moviuro

This comment has been minimized.

Copy link

@moviuro moviuro commented Apr 14, 2020

https://gist.github.com/premek/6e70446cfc913d3c929d7cdbfe896fef#file-mv-sh-L18 will fail with files that start with a dash:

$ mv -lol
toto
mv: invalid option -- 'l'
Try 'mv --help' for more information.

L18 should read:

command mv -v -- "$1" "$newfilename"

And also: https://news.ycombinator.com/item?id=22863551

@premek

This comment has been minimized.

Copy link
Owner Author

@premek premek commented Apr 14, 2020

@moviuro ok I updated it, thanks! And I removed the file call (I wanted to have nicer arror messages)

@anisse

This comment has been minimized.

Copy link

@anisse anisse commented Apr 14, 2020

Looks like we keep reinventing this :)

I made this in 2018 (https://github.com/lgommans/vinamer) and recently discovered this was a reinvention of a 2017 project (https://github.com/thameera/vimv).

It looks like it's a reinvention of the much older (2006) vidir (works with any $EDITOR) ; packaged in debian, ubuntu or fedora in the package moreutils, along with other very useful tools:
https://joeyh.name/code/moreutils/

@nablaone

This comment has been minimized.

Copy link

@nablaone nablaone commented Apr 14, 2020

$ apropos rename
$ man rename

@Humandoodlebug

This comment has been minimized.

Copy link

@Humandoodlebug Humandoodlebug commented Apr 14, 2020

Fish shell equivalent:
~/.config/fish/functions/mv.fish

function mv --wraps mv
  if [ (count $argv) -ne 1 -o ! -f "$argv[1]" ]
    command mv $argv
    return
  end

  set newfilename (bash -c 'read -ei "$1" newfilename && echo "$newfilename"' '' "$argv[1]")
  command mv -v -- "$argv[1]" "$newfilename"
end
@karekaa

This comment has been minimized.

Copy link

@karekaa karekaa commented Apr 14, 2020

Just thinking that if you want to call the mv command again when pressing up, after using it to change the name of a file, it will use the original file name, forcing the user to adjust the name,

You think like this?
Anway, be aware that «history of arguments» are found by <ALT><.> (<ALT><Punctation>) so this:
~ $ touch zoo
~ $ mv <ALT><.><SPACE><ALT><.>

... will type out:
~ $ mv zoo zoo

... and by the way, continue <ALT><.> repeated times to get older «arguments»

@domyrtille

This comment has been minimized.

Copy link

@domyrtille domyrtille commented Apr 14, 2020

@rohan-mo
-bash: emacs: command not found

Meaning you don't have emacs installed or the bin is not in your $PATH
Easiest way is to apt install emacs or brew install emacs

Other way will be to do something like that:
export EDITOR=vim if you have vim or replace vim with what you have installed

@cameronsstone

This comment has been minimized.

Copy link

@cameronsstone cameronsstone commented Apr 14, 2020

Firstly, thank you - this is nice™.

zsh equivalent:

function mv() {
  if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
    command mv "$@"
    return
  fi

  newfilename="$1"
  vared newfilename
  command mv -v -- "$1" "$newfilename"
}

Kudos to Gilles 'SO- stop being evil' for StackExchange answer.

BTW, fish also has vared, so this should probably work for fish, too, and be lighter-weight than Humandoodlebug's excellent bash subshell solution.

@dbgrigsby

This comment has been minimized.

Copy link

@dbgrigsby dbgrigsby commented Apr 14, 2020

Yeah, many ways to do this but would argue readline's Ctrl-W Ctrl-Y Ctrl-Y is cleanest since it's useful to know anyway and available almost anywhere. :)

Screen Recording 2020-04-13 at 06 43 PM
That is quite nice! Biggest fan of this since it's very little typing and applies in way more situations than the OP

@rileytg

This comment has been minimized.

Copy link

@rileytg rileytg commented Apr 15, 2020

@rileytg man readline (sorry for the terse answer but that's the library bash uses and where you will find, in long form, what options are available)

thank you! i didnt realize readline was so foundational to a lot of what i use every day

@szkrd

This comment has been minimized.

Copy link

@szkrd szkrd commented Apr 16, 2020

I like it, it's especially useful if you have spaces in the file (or directory) name (btw the bash cut last word would not work then). Probably I would also flip the conditional and add directory checking:

if [ "$#" -eq 1 ] && ( [ -f "$1" ] || [ -d "$1" ] ); then
  read -ei "$1" newfilename; command mv -v "${1}" "${newfilename}"; return $?
fi
command mv "$@"; return $?
@b3n10

This comment has been minimized.

Copy link

@b3n10 b3n10 commented Apr 16, 2020

You can also do Ctrl-W followed by Ctrl-Y after typing the path, if you are using readline style keybindings. Mainstream shells come with readline style bindings by default.

Ctrl-W deletes the previous word for me

@premek

This comment has been minimized.

Copy link
Owner Author

@premek premek commented Apr 16, 2020

@szkrd

[...] and add directory checking

-e was what I wanted, not -f. Thanks

@NightMachinary

This comment has been minimized.

Copy link

@NightMachinary NightMachinary commented Apr 16, 2020

In zsh you need compdef mv=ls, it seems, or the completion breaks.

@fernandezpablo85

This comment has been minimized.

Copy link

@fernandezpablo85 fernandezpablo85 commented Apr 17, 2020

@premek your snippet is great, short, concise and to the point. Thanks for sharing it.

@collegeimprovements

This comment has been minimized.

Copy link

@collegeimprovements collegeimprovements commented Apr 19, 2020

Yeah, many ways to do this but would argue readline's Ctrl-W Ctrl-Y Ctrl-Y is cleanest since it's useful to know anyway and available almost anywhere. :)

Screen Recording 2020-04-13 at 06 43 PM

Excellent.

@zouhair

This comment has been minimized.

Copy link

@zouhair zouhair commented May 4, 2020

There are even more flexible ways (in bash, at least), for example (here renaming .txt to .json):

$ touch config.txt
$ mv config.txt !#:1:s/txt/json

This one eludes me, care to explain how it works?

@christian-oudard

This comment has been minimized.

Copy link

@christian-oudard christian-oudard commented Jun 25, 2020

I would recommend adding --interactive to the mv commands in the script. As it is now, it is possible to overwrite existing files with no confirmation using this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.