I know how to rename files in Unix:

$ mv ~/folder/subfolder/file.txt ~/folder/subfolder/file.sh

     ^-------this part------^    ^------this part-------^

It takes too long time to repeat ~/folder/subfolder/file twice.

Is there a quicker way?


6 Answers 6


If your shell supported Brace Expansion (works with csh, tcsh, ksh, zsh, bash, mksh, lksh, pdksh, yash with brace-expand enabled by calling yash --brace-expand or set in interative shell with set -o brace-expand, or fish):

mv ~/folder/subfolder/file.{txt,sh}
  • 4
    simple is the best!
    – Braiam
    Commented May 27, 2014 at 18:05
  • 2
    awesome use of expansion in shell brilliant! Commented May 27, 2014 at 18:10
  • 3
    It is awesome, but really cryptic. It works but at the first glance I would think it does something different: moves two file somewhere. I don't think this is a great idea, especially since there is a much better solution using rename.
    – yo'
    Commented May 28, 2014 at 21:07
  • 5
    @tohecz: Why is it cryptic? It's a feature of shell and every one can know it by reading shell man page. And I don't think rename is a better solution because rename never ask you to override the existing file or not.
    – cuonglm
    Commented May 29, 2014 at 2:32
  • 1
    @Gnouc you're the man.
    – beginer
    Commented Sep 1, 2014 at 12:26

You can also use rename (part of the util-linux package).

rename .txt .sh ~/folder/subfolder/file.txt

See the rename man page for more details.

  • +1 This is the clear way how to do it, much better than relying on shells {a,b} functionality.
    – yo'
    Commented May 28, 2014 at 21:02
  • 5
    In some Linux distros (such as Ubuntu), rename is an entirely different, perl-based program. There, you would use rename 's/txt$/sh/' ~/folder/subfolder/file.txt.
    – evilsoup
    Commented Oct 4, 2014 at 9:34
  • 1
    On Debian (and Ubuntu), there is also mmv. Commented Apr 13, 2018 at 13:13
  • @ctrl-alt-delor mmv doesn't work in Ubuntu. Even after installing using sudo apt, this command just hangs and doesn't do anything. Commented Jan 26, 2020 at 1:40
  • @ShitalShah how did you call it? What arguments? Commented Jan 26, 2020 at 7:39

All the above are good. This would also work :

( cd ~/folder/subfolder && mv file.txt file.sh )
  • No. It doesn't work for me! Commented May 27, 2014 at 16:35
  • It will work if you remove the sudo part.
    – Cthulhu
    Commented May 27, 2014 at 17:03
  • 1
    @Cthulhu Yes your right. But why doesn't work with sudo? Commented May 27, 2014 at 17:29
  • 2
    @HamedKamrava Probably because with sudo it interprets ~ as the root's home directory.
    – Cthulhu
    Commented May 28, 2014 at 4:41
  • You can sudo the mv, but not cd. See why cd is a shell built in. And you can not sudo the ( Commented Apr 13, 2018 at 13:17

No. You need to give the full path to the file in order to rename it. The only alternative is to move into the target folder before running the mv:

cd ~/folder/subfolder/
mv file.txt file.sh

Alternatively, you could write a little function that renames the file in the target directory. For example, add these lines to your shell initialization file (~/.bashrc if you are using bash):

    _path=$(dirname -- "$1")
    mv -- "$1" "$_target"

Then, open a new terminal or just run source ~/.bashrc to re-read the init file and you can do:

lmv ~/folder/subfolder/file.txt file.sh
  • +1 for lmv() function. I didn't know that :) Commented May 27, 2014 at 16:20
  • 1
    @HamedKamrava nobody knew it. I just wrote it, it's not anything standard.
    – terdon
    Commented May 27, 2014 at 16:22
  • 2
    I know. I mean I didn't know I can write my own function! Commented May 27, 2014 at 16:23

Just to expand the usefulness of cuonglm's answer (NOT to take any credit as I love his solution) and his answer is a correct one.

The use case is that we often want to mv a file in a remote location (the real issue), e.g. /folder/subfolder/configFile.dat TO configFile.dat.orig

This form of the command adds a file extension (not replacing the original extension)

mv ~/folder/subfolder/file.txt{,.orig}

Explained: "{,.orig}" means replace (nothing) on the end of the file name with (something) ".orig"

OR to remove a file extension (reverse the rename)

mv ~/folder/subfolder/file.txt{.orig,}

Note: Still on topic for "Quickest way to rename files without retyping the dir path"


Yes. If you use bash, you do sudo pushd ~/folder/subfolder/ && sudo mv ./file.txt ./file.sh && popd.

Which is actually bigger and may fail if you lost access permissions to the original directory when you did the popd.

  • 1
    What's the point of pushd and popd here? How is this better than cd ~/folder/subfolder/ && sudo mv file.txt file.sh?
    – terdon
    Commented May 27, 2014 at 16:20
  • 1
    The point is, that you are after the command back in the folder you started, not in any subfolder. An alternative to pushd popd is to use cd and go back with cd -.
    – jofel
    Commented May 27, 2014 at 16:27
  • The questioner seemed to state an intention not to change the working directory, and popd is better than cd ../../, when available.
    – 41754
    Commented May 27, 2014 at 16:32
  • 7
    Use (cd /path/to/there && mv x y) then (with a subshell) Commented May 27, 2014 at 16:39
  • You really should not put sudo before pushd. So the full command should be pushd ...dir && sudo mv old new && popd. As an alternative, one could do (cd ...dir; sudo mv old new) because running in subshell will take care of directory changes automatically. Commented Mar 12, 2019 at 11:38

You must log in to answer this question.

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