×

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one location to another, or to rename a file or directory. The basic syntax for the 'mv' command is:

mv [source] [destination]

Where "source" is the file or directory that you want to move or rename, and "destination" is the location where you want to move the file or directory. If the destination is an existing directory, the source file will be moved into that directory. If the destination is a new name, the source file or directory will be renamed to that name.

For example, if you want to move a file named 'file.txt' from the directory '/home/user/documents' to the directory '/home/user/backup', you would use the command:

mv /home/user/documents/file.txt /home/user/backup

If you want to rename the file 'file.txt' to 'document.txt', you would use the command:

mv /home/user/documents/file.txt /home/user/documents/document.txt

The 'mv' command also supports moving multiple files at once. For example, if you want to move all the files with the '.txt' extension from the directory '/home/user/documents' to the directory '/home/user/backup', you would use the command:

mv /home/user/documents/*.txt /home/user/backup

Another important feature of the 'mv' command is the -i (interactive) option. This option will prompt you for confirmation before overwriting an existing file. For example, if you want to move a file 'file.txt' to a directory that already contains a file with the same name, the command 'mv -i file.txt /path/to/directory' will prompt you to confirm if you want to overwrite the existing file.

The 'mv' command also has a -f (force) option which can be used to overwrite files and directories without any prompt. Use this option with caution as it can cause data loss.

The 'mv' command also has a -n (no-clobber) option, it is similar to the -i option but it will not prompt the user before overwriting an existing file. Instead, it will simply skip the file and not overwrite it.

The 'mv' command also has a -b (backup) option which can be used to create a backup copy of the file before overwriting it. The backup copy will have the same name as the original file with a suffix of '~'.

It is important to note that when you use the 'mv' command to move or rename a file or directory, it will also update any symbolic links that point to that file or directory. This can have implications if other programs or processes are dependent on those links. Additionally, when you move or rename a directory, any files and subdirectories within that directory will also be moved or renamed. It is important to be aware of this when using the 'mv' command, as it can cause unexpected results if not used carefully.

Another important aspect of the 'mv' command is that it does not keep the original file permissions, ownership or timestamps. When you move or rename a file or directory, the permissions, ownership, and timestamps will be reset to the defaults for the new location.

It is also important to be aware that the 'mv' command is not able to move or rename files across different file systems. If you need to move or rename a file or directory from one file system to another, you will need to use the 'cp' command to copy the file or directory, and then use the 'rm' command to remove the original file or directory.

In conclusion, the 'mv' command is a powerful tool for managing files and directories in Linux. It allows you to move or rename files and directories, and it also provides options for interactive, force and backup. It is important to be careful when using the 'mv' command, as it can cause unexpected results if not used carefully. It is also important to be aware of the implications of moving or renaming files and directories, as it can affect other programs or processes that depend on them. With a good understanding of how the command works and its implications, you will be able to efficiently and effectively manage your files and directories on your Linux systems.


Related Topics

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.