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):

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

… and a few test directories (named dir1 and dir2):

greys@server:~ $ mkdir dir1
greys@server:~ $ mkdir dir2

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.

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

Renaming files with mv

Now let’s rename try3 into try33:

greys@server:~ $ mv try3 try33
greys@server:~ $ ls -la try*
-rw-rw-r-- 1 greys 0 Sep  4 14:45 try33

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.

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

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets