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.

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

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!