Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any shortcut to reference the path of the first argument in a MV command?

Tags:

linux

bash

mv

I often find myself using mv to rename a file. E.g.

mv app/models/keywords_builder.rb app/models/keywords_generator.rb

Doing so I need to write (ok, tab complete) the path for the second parameter. In this example it isn't too bad but sometimes the path is deeply nested and it seems like quite a bit of extra typing.

Is there a more efficient way to do this?

like image 793
Jack Kinsella Avatar asked Mar 31 '11 20:03

Jack Kinsella


People also ask

How do you use the mv command?

Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name. Attention: The mv command can overwrite many existing files unless you specify the -i flag.

What happens when you use the mv command where the source is a file destination is a directory?

If you specify a single file as SOURCE , and a single file as DESTINATION target then you're renaming the file . When the SOURCE is a directory and DESTINATION doesn't exist, SOURCE will be renamed to DESTINATION . Otherwise if DESTINATION exist, it be moved inside the DESTINATION directory.

What is mv bash command?

This bash command moves files and folders. mv source target mv source ... directory. The first argument is the file you want to move, and the second is the location to move it to.


3 Answers

You can use history expansion like this:

mv app/modules/keywords_builder.rb !#^:h/keywords_generator.rb
  1. ! introduces history expansion.
  2. # refers to the command currently being typed
  3. ^ means the first argument
  4. :h is a modifier to get the "head", i.e. the directory without the file part

It's supported in bash and zsh.

Docs:

  • bash history expansion
  • zsh history expansion
like image 193
Mikel Avatar answered Oct 02 '22 20:10

Mikel


One way is to type the first file name and a space, then press Ctrl+w to delete it. Then press Ctrl+y twice to get two copies of the file name. Then edit the second copy.

For example,

mv app/models/keywords_builder.rb <Ctrl+W><Ctrl+Y><Ctrl+Y><edit as needed>
like image 32
geekosaur Avatar answered Oct 02 '22 20:10

geekosaur


or cd apps/models && mv keywords_builder.rb keywords_generator.rb && cd -

like image 38
glenn jackman Avatar answered Oct 02 '22 21:10

glenn jackman