Move/Rename Files and Directories

Move Files/Directories

To move the content of a file/directory to another directory, you just use the mv command. This command doesn’t create a copy/duplicate of files or directories.

To move the contents of a directory, use the following command:

Syntax:

mv <CurrentDirectory>/* <NewDirectory>

For example, to move all the contents of a directory called animals to a directory named birds you would type:

mv animals/* birds

In this case, if birds (destination directory) exists, the contents of animals directory will be moved to the birds directory and animals directory will disappear. However, if the birds directory doesn’t exist, it will be created and then the contents of animals directory will be moved. The animals directory will disappear after the contents are moved.

The Syntax for moving files is:

mv <FileName> <DestinationFolder>

For example, to move a file called owls into directory birds, you would type:

mv owls birds
The source location of the file to be moved must be different than the destination location of the file.

Rename Files and Directories

mv command is used to rename files and directories. Renaming files and directories is simply a case of "moving" a file or a directory from one name to another.

The general expression for renaming files is as follows:

mv <old-Name> <new-Name>

For example, to rename file owls to parrots, you would type:

mv owls parrots

To rename a directory named animals to birds, you simply type:

mv animals birds

The destination location must be the same with the source location. Also, the filename must be different.