UNIX mv Command Examples

by on January 2, 2009 · 8 comments· LAST UPDATED November 2, 2009

in , ,

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.

Syntax

mv oldname newname
mv filename /dest/di

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
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 WendiBarba January 5, 2011 at 11:09 pm

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

Reply

2 nilesh nanekar August 7, 2012 at 6:53 am

mv company newcompanyname

Reply

3 vignesh June 21, 2011 at 9:32 am

Plz give an example for mv -t command in unix

Reply

4 chandralekha February 2, 2012 at 8:51 am

plz give a program to implement move command

Reply

5 gayatri March 30, 2012 at 10:11 am

#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;
}

Reply

6 Kashyap July 11, 2012 at 8:28 am

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

Reply

7 nixCraft July 11, 2012 at 6:07 pm

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

Reply

8 Randy February 21, 2013 at 1:29 am

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?

Reply

Leave a Comment

Tagged as: , , , , , , , , , , ,

Previous Faq:

Next Faq: