Move / Rename files, Directory – MV Command in Unix / Linux

  • Updated
  • Posted in Programming
  • 3 mins read


Q. How to rename a file or listing in unix (or linux) and how you can transfer a file or listing from the present listing to a different listing?

Unix offers a easy mv (transfer) command which can be utilized to rename or transfer recordsdata and directories. The syntax of mv command is

mv [options] oldname newname

The choices of mv command are

f : Do not immediate earlier than overwriting a file. 
i : Prompts for the person enter earlier than overwriting a file.

If the newname already exists, then the mv command overwrites that file. Let see some examples on how you can use mv command.

Unix mv command examples

1. Write a unix/linux command to rename a file?

Renaming a file is among the primary options of the mv command. To rename a file from “log.dat” to “unhealthy.dat”, use the beneath mv command

> mv log.dat unhealthy.dat

Note that if the “unhealthy.dat” file already exists, then its contents shall be overwritten by “log.dat”. To keep away from this use the -i possibility, which prompts you earlier than overwriting the file.

mv -i log.dat unhealthy.dat
mv: overwrite `unhealthy.dat'?

2. Write a unix/linux command to rename a listing?

Just as renaming a file, you should use the mv command to rename a listing. To rename the listing from docs to paperwork, run the beneath command

mv docs/ paperwork/

If the paperwork listing already exists, then the docs listing shall be moved in to the paperwork listing.

3. Write a unix/linux command to maneuver a file into one other listing?

The mv command will also be used to maneuver the file from one listing to a different listing. The beneath command strikes the sum.pl file in the present listing to /var/tmp listing.

mv sum.pl /var/tmp/

If the sum.pl file already exists in the /var/tmp listing, then the contents of that file shall be overwritten.

4. Write a unix/linux command to maneuver a listing in to a different listing?

Just as transferring a file, you’ll be able to transfer a listing into one other listing. The beneath mv command strikes the paperwork listing into the tmp listing

mv paperwork /tmp/

5. Write a unix/linux command to maneuver all of the recordsdata in the present listing to a different listing?

You can use the common expression sample * to maneuver all of the recordsdata from one listing to a different listing.

mv * /var/tmp/

The above command strikes all of the recordsdata and directories in the present listing to the /var/tmp/ listing.

6. mv *

What occurs for those who merely kind mv * after which press enter?

It will depend on the recordsdata you’ve gotten in the listing. The * expands to all of the recordsdata and directories. Three situations are attainable.

  • If the present listing has solely recordsdata, then the contents of all of the recordsdata (besides one file) shall be written in to the one file. The one file is the final file which will depend on the sample *.
  • If the present listing comprises solely directories, then all of the directories (besides one listing) shall be moved to a different listing.
  • If the present listing comprises each recordsdata and directories, then it will depend on the enlargement of the *. If the sample * offers the final one as listing then all of the recordsdata shall be moved to that listing. Otherwise the mv command will fail.

Some Tips:

  • Try to keep away from mv *
  • Avoid transferring massive variety of recordsdata.

Leave a Reply