UNIX mv Command Examples

by Vivek Gite on January 2, 2009 · 3 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 3 comments… read them below or add one }

1 WendiBarba January 5, 2011

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

Reply

2 vignesh June 21, 2011

Plz give an example for mv -t command in unix

Reply

3 chandralekha February 2, 2012

plz give a program to implement move command

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 7 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: