Post Thumbnail

mv - move and rename files and directories

mv mv command examples

mv command is a basic Unix command for moving and renaming files and directories.

As you can see on the screenshot above, mv command can be used to move files from one directory to another, or to simply rename places. Naturally, both moving and renaming can be done at the same time - mv will figure out what’s required to ensure a file with one path becomes relocated into file with another path.

Let’s first setup a few test files (named try1, try2 and try3 in this example): {% highlight console %} greys@server:~ $ touch try1 try2 try3 greys@server:~ $ ls -la try* -rw-rw-r– 1 greys 0 Sep 4 14:45 try1 -rw-rw-r– 1 greys 0 Sep 4 14:45 try2 -rw-rw-r– 1 greys 0 Sep 4 14:45 try3 {% endhighlight %}

… and a few test directories (named dir1 and dir2): {% highlight console %} greys@server:~ $ mkdir dir1 greys@server:~ $ mkdir dir2 {% endhighlight %}

Moving files from one directory to another

Now let’s try moving some files. First command moves try1 file into dir2. Second command moves try2 into dir1.

{% highlight console %} greys@server:~ $ mv try1 dir2 greys@server:~ $ mv try2 dir1 greys@server:~ $ ls -la dir*/try* -rw-rw-r– 1 greys 0 Sep 4 14:45 dir1/try2 -rw-rw-r– 1 greys 0 Sep 4 14:45 dir2/try1 {% endhighlight %}

Renaming files with mv

Now let’s rename try3 into try33:

{% highlight console %} greys@server:~ $ mv try3 try33 greys@server:~ $ ls -la try* -rw-rw-r– 1 greys 0 Sep 4 14:45 try33 {% endhighlight %}

Moving AND renaming file with mv

Let’s now move try2 file back from dir1 subdirectory into current directory, but also rename it into newtry2 at the same time. As you can see, there’s nothing left in dir1 subdirectory, because mv moved the file from it, and new file is called newtry2 now.

{% highlight console %} greys@server:~ $ mv dir1/try2 ./newtry2 greys@server:~ $ ls -la dir1* total 8 drwxrwxr-x 2 greys 4096 Sep 4 14:50 . drwxr-xr-x 21 greys 4096 Sep 4 14:50 .. greys@server:~ $ ls -al newtry2 -rw-rw-r– 1 greys 0 Sep 4 14:45 newtry2 {% endhighlight %}

See Also