607

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?

802
mv /home/user/oldname /home/user/newname
| improve this answer | |
  • 4
  • Cool article. That's exactly the point. – Rafał Cieślak Aug 9 '11 at 9:02
  • 82
    This 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 4:56
  • 10
    If 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 5 '14 at 16:30
  • 10
    @cxrodgers: pass --no-target-directory (-T), to avoid treating newname as a target directory. – jfs Dec 7 '16 at 4:52
131

mv can do two jobs.

  1. It can move files or directories
  2. 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.

If is a directory you should add -R before the directory name:

mv -R directory_name ~/Desktop
| improve this answer | |
  • 5
    mv on Ubuntu is from GNU coreutils. AFAICT it doesn't have a -R option, nor it is necessary when moving a directory. – arielf Sep 9 '15 at 7:46
  • 1
    -r doesn't even work – user1735921 Sep 26 '16 at 6:19
  • The correct option is -T. See @bmaupin's answer for its behavior. – chronometric Mar 23 '17 at 10:09
43
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/newname doesn't exist, it will rename /home/user/oldname to /home/user/newname
  • If /home/user/newname exists, it will move /home/user/oldname into /home/user/newname, i.e. /home/user/newname/oldname

Source: How to decide that mv moves into a directory rather than replacing directory?

| improve this answer | |
  • 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_). – basilikode 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
  • did you try this? – Mahmoud S. Marwad Jul 21 '19 at 14:12
8

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
| improve this answer | |
5

This gvfs-move command will also rename files and directories.

gvfs-move /home/user/oldname /home/user/newname
| improve this answer | |
4

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.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.