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?
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?
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}
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.
{a,b}
functionality.
– yo'
May 28 '14 at 21:02
rename
is an entirely different, perl-based program. There, you would use rename 's/txt$/sh/' ~/folder/subfolder/file.txt
.
– evilsoup
Oct 04 '14 at 09:34
mmv
doesn't work in Ubuntu. Even after installing using sudo apt, this command just hangs and doesn't do anything.
– Shital Shah
Jan 26 '20 at 01:40
All the above are good. This would also work :
( cd ~/folder/subfolder && mv file.txt file.sh )
sudo
it interprets ~
as the root's home directory.
– Cthulhu
May 28 '14 at 04:41
sudo
the mv
, but not cd
. See why cd is a shell built in. And you can not sudo
the (
– ctrl-alt-delor
Apr 13 '18 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
):
lmv(){
_path=$(dirname -- "$1")
_target="${_path%/}/$2"
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
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
.
pushd
and popd
here? How is this better than cd ~/folder/subfolder/ && sudo mv file.txt file.sh
?
– terdon
May 27 '14 at 16:20
pushd
popd
is to use cd
and go back with cd -
.
– jofel
May 27 '14 at 16:27
popd
is better than cd ../../
, when available.
– 41754
May 27 '14 at 16:32
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.
– Mikko Rantalainen
Mar 12 '19 at 11:38
rename
. – yo' May 28 '14 at 21:07rename
is a better solution becauserename
never ask you to override the existing file or not. – cuonglm May 29 '14 at 02:32~/fruits/bananas_and_mangos
into~/fruits/pineapples_and_mangos
? – Salomanuel Mar 17 '22 at 15:16