How to move and rename files in Linux with mv

Mv can both rename and move files. It is a powerfull generical command, working on all GNU-unixes.

Watch Free Movies

In this article I will teach you how to move and rename files and folders, with practical examples:

How to rename files (and folders) with mv:

Syntax: mv oldname newname

$ touch naboo
$ mkdir deathstar
$ ls
deathstar naboo

I will rename the newly created file (naboo) and directory (deathstar).

$ mv deathstar newdeathstar
$ mv naboo newnaboo
$ ls
newdeathstar newnaboo

mv trick: rename a file with a very long name:
$ touch thisfilehasaverylongname
$ touch thisfilehasaverylongname{,.exe}
$ ls
thisfilehasaverylongname.exe

How to move files and folders with mv:

Syntax: mv source destination

I will move the non empty ~/naboo folder in ~/work. It is the same command for moving files.
$ mv ~/naboo ~/work
$ ls ~/work
naboo

By default, mv will replace the destination, if the source has the same name.

Use mv interactive in order to ask you if you want to overwrite files and to display what changes have been made:

$ mv -iv a ~/newdeathstar/
mv: overwrite `/home/razvan/newdeathstar/a'? y
`a' -> `/home/razvan/newdeathstar/a'

Type yes if you want to replace the file, and n if you don’t.

You can alias the interactive mv command: alias mv=’mv -iv’

mv trick: move multiple files with mv and the {} wildcard:

$ touch a b c
$ mv {a,b} ~
$ ls
c
$ ls ~
a b

Scroll to Top