UNIX mv Command Examples

How do I use mv command to rename or move file under UNIX operating systems?

You can move and rename files by using the mv command under UNIX.

ADVERTISEMENTS

Syntax

mv oldname newname
mv filename /dest/dir

Task: Rename A File

Type the mv command to rename foo.txt to bar.txt:

mv foo.txt bar.txt
ls

Task: Rename A Directory

Type the following command to rename a directory in the current directory:

mv oldDir newDir
mv letters letters.old

Task: Move Directory

Use the following sytax:
mv sourceDir destDir
In this example, move httpd directory and its contents to a new location /webroot in the file system so that a subdirectory named httpd resides in directory /webroot:

mv httpd /webroot
cd /webroot
ls -l

mv Options

The following options are supported:

  • -f : mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input is not a terminal.
  • -i : mv will prompt for confirmation whenever the move would overwrite an existing target. An affirmative answer means that the move should proceed. Any other answer prevents mv from overwriting the target.

Overwrite an existing file, enter:

mv -f file /dest
mv -i /etc/passwd /backup
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
13 comments… add one
  • WendiBarba Jan 5, 2011 @ 23:09

    How can I change the name of the company in a Unix system?

    • nilesh nanekar Aug 7, 2012 @ 6:53

      mv company newcompanyname

  • vignesh Jun 21, 2011 @ 9:32

    Plz give an example for mv -t command in unix

  • chandralekha Feb 2, 2012 @ 8:51

    plz give a program to implement move command

    • gayatri Mar 30, 2012 @ 10:11

      #include
      #include
      #include
      #include
      int main()
      {
      int fd,rc,fd1;
      char ch[5];
      fd=open(“sam1”,O_WRONLY|O_CREAT);
      fd1=open(“sam”,O_RDONLY);
      while((rc=read(fd1,ch,5))>0)
      {
      write(fd,ch,rc);
      }

      close(fd);
      close(fd1);
      printf(“FILE IS SUCCESSFULLY MOVED\n”);
      unlink(“sam”);
      return 0;
      }

  • Kashyap Jul 11, 2012 @ 8:28

    Can we use mv command to move one file to another remote machine..?

    • 🐧 nixCraft Jul 11, 2012 @ 18:07

      No. Use rsync command with –delete option. Read man page for more info.

  • Randy Feb 21, 2013 @ 1:29

    Let’s say I have a directory with FLAC and MP3 files. I would like to move all of the FLAC files into a folder or even a folder that has not been created yet. Is this possible?

  • shivi Jan 15, 2015 @ 10:35

    how to get the files back if i rename 5 different files with same name…????

  • Akilapriya Mar 19, 2015 @ 11:41

    Plz give me an example to execute mv and ch commands in LINUX….

    • sivaiahkallagunta Oct 12, 2015 @ 17:40

      mv oldname newname
      mv filename /dest/di
      mv -rf siva.pl sivaiah.pl
      Here r means recarsve and f means force.
      1) chmod o+r *.html
      This will grant Read permission to everyone for all the files in the current directory whose names end in .html.

      2) chmod o+r *.gif
      This will grant Read permission to everyone for all the files in the current directory whose “extension” is gif.

      3) chmod o+r *
      This will grant Read permission to everyone for ALL the files in the current directory.

      4) chmod o+r dir-name/*.html

      eg : chmod o+r personal/*.html
      To grant Read permission to everyone for all the .html files within a sub-directory of the current directory you would use the Above command:

      5)
      1) chmod o-r *.html
      2) chmod o-rx .
      The first command removes the read permission from all the .html files in the current directory. The second removes read and execute permission from the current directory (indicated by the dot at the end of the command).

  • Saurav pattnaik Feb 2, 2017 @ 10:31

    please give me all the options for move and rename command in unix

  • Saurav pattnaik Feb 2, 2017 @ 10:32

    please tell me all the options of move and rename in unix

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.