Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file at some/long/path/to/file/myfiel.txt.

I want to rename it to some/long/path/to/file/myfile.txt.

Currently I do it by mv some/long/path/to/file/myfiel.txt some/long/path/to/file/myfile.txt, but typing the path twice isn't terribly effective (even with tab completion).

How can I do this faster? (I think I can write a function to change the filename segment only, but that's plan B).

share|improve this question
up vote 8 down vote accepted

To do this in a single command, you can simply do this:

mv some/long/path/to/file/{myfiel.txt,myfile.txt}

Which is an example for the full file name, given that it's a typo you can do something like:

mv some/long/path/to/file/myfi{el,le}.txt

Both will expand to the full command, these are called brace expansions. They are supported by zsh.

share|improve this answer
    
Oh my, this opens so much possibilities. Thanks! – Leonid Shevtsov Oct 12 '12 at 15:58

Here are several options:

Change to the directory:

cd /home/long/path
mv file1 file2
cd -

Change directories using the directory stack:

pushd /some/long/path
mv file1 file2
popd

Change to the directory using a subshell:

( 
  cd /some/long/path
  mv file1 file2
)   # no need to change back

Use brace expansion:

mv /some/long/path/{file1,file2}

Use a variable:

D=/some/long/path
mv "$D/file1" "$D/file2"
share|improve this answer
    
Beware that the last approach breaks when the path has a space, beter quote it. – slhck Oct 12 '12 at 15:57
    
@slhck ALL of the approaches break if you have spaces. – tylerl Oct 12 '12 at 15:58
1  
No, if you type them correctly they won't. Only the variable when expanded will look like multiple arguments to mv – slhck Oct 12 '12 at 16:09
    
@slhck there you go. – tylerl Oct 12 '12 at 16:50

Change to the directory, move the file, and change back to the previous directory; like so:

cd some/long/path/to/file
mv myfiel.txt myfile.txt
cd -
share|improve this answer

When I use the subshell method I would tend to do it on one line like so

(cd /some/long/path ; mv myfiel myfile )
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .