Linux/Unix’s mv Command with Examples

Posted by

Have there ever been times when you needed to transfer files or folders on your Linux/Unix system? Fear not, though! To save the day, use the mv command. We shall study the mv command’s many features and capabilities in this post by plunging into its deep waters.

Simple Use
Let’s begin with the fundamentals. The mv command has the following syntax:

mv [options] source destination

In this case, the source denotes the file or directory that you wish to move, and the destination designates the desired place for the file or directory to be moved.

As an illustration, suppose you want to move a file called file.txt from your current directory to a new directory called new_directory. The following command can be used:

mv file.txt new_directory/

Simply change file.txt with the name of the directory you wish to relocate in lieu of the file.

Renaming Directories and Files

You may also rename files and directories with the mv command. You can choose the new name as the destination to rename a file or directory.

For illustration, suppose you want to change a file called old_name.txt to new_name.txt. The following command can be used:

mv old_name.txt new_name.txt

Similar to this, you can specify the new name of a directory as the destination if you want to rename it.

File Overwriting

The mv command does not, by default, overwrite already-existing files or directories. The -f option can be used, nevertheless, to replace a file that already exists and has the same name.

Say, for instance, that you wish to replace the file in the destination directory with the file from the source directory and that you have a file named file.txt in both the source and destination directories. The following command can be used:

mv -f source/file.txt destination/
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
()
x