I have got the directory /home/user/oldname and I want to rename it to /home/user/newname. How can I do this in a terminal?
- 20,204
- 7
- 56
- 86
- 17,939
- 18
- 57
- 92
-
See also: [How to move a directory](http://stackoverflow.com/questions/868198/how-to-move-a-directory) – kenorb Sep 19 '14 at 22:36
-
Use always `mv` or `rename`. – John Strood Jul 01 '16 at 20:11
-
1use `mv` and don't use `/` in end. Because your content will go inside it. – Tamil Nov 07 '20 at 08:08
7 Answers
mv /home/user/oldname /home/user/newname
- 20,204
- 7
- 56
- 86
-
4
-
-
101This will not work if the new name is already an existing directory. Instead, it will move the old directory inside the new one. – cxrodgers Apr 17 '14 at 04:56
-
10If the directory name is the same with capitalization you will get `No such file or directory`. To avoid this do something like `mv /home/user/Folder /home/user/temp; mv /home/user/temp/ /home/user/folder`. – DutGRIFF Dec 05 '14 at 16:30
-
13@cxrodgers: [pass `--no-target-directory` (`-T`)](http://askubuntu.com/a/763915/3712), to avoid treating `newname` as a target directory. – jfs Dec 07 '16 at 04:52
-
@jfs: I've been wondering what that option meant for a long time, as the `--help` description isn't particularly useful. Glad to finally know. – drmuelr Jun 29 '18 at 17:18
-
It doesn't work if you want to capitalize a folder on a case-insensitive filesystem (likely on MacOS). `mv $PWD/analisys $PWD/Analisys` results in `mv: cannot move '/Users/sixtykeys/Projects/murphy/tmp/analisys' to a subdirectory of itself, '/Users/sixtykeys/Projects/murphy/tmp/Analisys/analisys'` – acorello Feb 13 '19 at 13:08
-
The “_target directory_” on the man page means “_the directory into which files will be copied_.” As @jfs said, you don’t want to copy _into_ a directory in this case; always use `--no-target-directory` for safety. – Константин Ван Mar 29 '23 at 09:10
-
mv can do two jobs.
- It can move files or directories
- It can rename files or directories
To just rename a file or directory type this in Terminal:
mv old_name new_name
with space between the old and new names.
To move a file or directory type this in Terminal.
mv file_name ~/Desktop
it will move the file to the desktop.
mv -T /home/user/oldname /home/user/newname
That will rename the directory if the destination doesn't exist or if it exists but it's empty. Otherwise it will give you an error.
If you do this instead:
mv /home/user/oldname /home/user/newname
One of two things will happen:
- If
/home/user/newnamedoesn't exist, it will rename/home/user/oldnameto/home/user/newname - If
/home/user/newnameexists, it will move/home/user/oldnameinto/home/user/newname, i.e./home/user/newname/oldname
Source: How to decide that mv moves into a directory rather than replacing directory?
-
Doesn't work if you want to capitalize the directory name in a case-insensitive filesystem (likely on MacOS). `mv -T $PWD/analisys $PWD/Analisys` returns `mv: '/Users/sixtykeys/Projects/murphy/tmp/analisys' and '/Users/sixtykeys/Projects/murphy/tmp/Analisys' are the same file`. I worked around this by using an intermediate name (i.e. `analisys_`). – acorello Feb 13 '19 at 13:12
-
The command may not have been successful due to the limitations of the filesystem, but from another perspective it was successful in interpreting your intentions (renaming a directory, not moving it) :) – bmaupin Feb 13 '19 at 13:47
-
If you want to rename a directory at your level in the file system (e.g., you are at your home directory and want to rename a directory that is also in your home directory):
mv Directory ./NewNameDirectory
- 111
- 1
- 2
This gvfs-move command will also rename files and directories.
gvfs-move /home/user/oldname /home/user/newname
- 77,204
- 56
- 214
- 254
gvfs-rename will rename directories as well. It will give an error if a directory with the new name already exists. The only limitation is that you can't use a path with the folder name. So
gvfs-rename /home/boo /home/boo-the-dog
will not work, but
cd /home
gvfs-rename boo boo-the-dog
will work. Not as useful as mv -T but I read in the man that it was meant for network operations.
- 1,063
- 1
- 12
- 19
My preferred method is using: vidir because I love vi
Install moreutils
sudo apt update; sudo apt install moreutils
Call command vidir in your home-directory
vidir ~
Now search for the directory to change, using slash / e.g. /oldname make the changes, then press = ESC type :wq
Done!
- 3,292
- 2
- 22
- 28