How to Move & Rename Files and Directories in Linux using "mv" Command

You can use the mv in linux command to:

  • Move files and directories within the directory hierarchy
  • Rename existing files and directories

The mv command does not affect the contents of the files or directories being moved or renamed.

Moving and Renaming a File

You can use the mv command to rename a single file within the same directory. For example, use the mv command to rename the dante file to dantenew and then back again.

$ pwd
/export/home/user1
$ mv dante dantenew

$ ls
dantenew dir10 dir4   feathers_6 file.3 file.3 fruit dante
dante_1 dir2 dir5   file.1   file1 fruit2 practice
dir1 dir3 feathers  file.2   file2 file4 tutor.vi
$ mv dantenew dante
$ ls
dante dir10 dir4   feathers_6 file.3 file.3 fruit
dante _1 dir2 dir5   file.1   file1 fruit2 practice
dir1 dir3 feathers  file.2   file2 file4 tutor.vi
$

Moving a File to Another Directory

You can use the mv command to move a file to a different directory. The source option is the old file or directory name. The target option is the new file or directory name. To move the brands file from thecoffees directory into the user1 directory, perform the mv command.

$ cd ~/dir1/coffees
$ pwd
/export/home/user1/dir1/coffees
$ ls
beans   brands  nuts

$ mv brands
$ ls
beans   nuts
$ cd
$ pwd
/export/home/user1
$ ls -l brands
-rw-r--r--   1 user1    staff          0 Jul 29 09:09 brands

Moving a File to Another Directory

You can use the mv command to move a file to a different directory. The -i option prevents you from overwriting the existing files accidentally. The syntax for the mv command is:

mv -i source target

The source option is the old file or directory name. The target option is the new file or directory name. When you use the -i option, you are prompted for a confirmation before existing files or directories are overwritten by new ones.

  • A yes response permits the overwrite.
  • A no response prevents the mv command from overwriting the target.

To move the brands file from the coffees directory into the user1 directory, perform the mv command.

$ cd ~/dir1/coffees
$ pwd
/export/home/user1/dir1/coffees
$ ls
beans   brands  nuts

$ mv brands ~
$ ls
beans   nuts
$ cd
$ pwd
/export/home/user1
$ ls -l brands
-rw-r--r--   1 user1    staff          0 Jul 29 09:09 brand

Moving a Directory and Its Contents

You can use the mv command to move a directory and its contents to a different directory. To movethe practice directoryand its contentstothe letters directory, perform the mv command.

$ cd
$ pwd
/export/home/user1
$ ls practice
mailbox     project2 projection research results

$ mv practice letters
$ ls letters
mailbox     project2 projection research results

When you move a single directory to a target directory that does not exist, you are actually renaming the current directory and its path. When you move multiple directories to a target directory that does not exist, the following error message appears:

 mv: target_directory not found.

Renaming a Directory

You can use the mv command to rename directories within the current directory. To rename the dir5 directory to dir5new, perform the mv command:

$ cd
$ pwd
/export/home/user1

$ mv dir5 dir5new
$ ls
brands      dir10       dir5new     file.2   file3   letters
dante       dir2         feathers   file.3   file4    tutor.vi
dante_1     dir3       feathers_6    file1   fruit
dir1        dir4        file.1       file2    fruit2

$ mv dir5new dir5
$ ls
brands      dir2    dir10       file.2  file3    letters
dante       dir3    feathers    file.3   file4    tutor.vi
dante_1     dir4     feathers_6  file1    fruit
dir1         dir5    file.1      file2    fruit2