Linux mv Command
To see the mv
command in action with output, I'll go through some examples using both -v
(verbose) and -i
(interactive) options, so you can see what happens in various scenarios.
Example 1: Moving a File with Verbose Output
When using mv
with the -v
option, it shows each move operation.
Command:
Output:
This indicates that file1.txt
has been moved from the current directory to /home/user/Documents/
.
Example 2: Renaming a File
Renaming a file within the same directory can be done by specifying the current and new names.
Command:
Output:
This shows that oldname.txt
was renamed to newname.txt
.
Example 3: Moving Multiple Files to a Directory
You can move multiple files by listing them before the destination directory.
Command:
Output:
Each file is moved individually, and the output shows the source and destination paths.
Example 4: Interactive Mode with -i
Option
If a file with the same name already exists in the destination, mv -i
will prompt you to confirm overwriting.
Command:
Output:
If you type y
, it will overwrite the existing file. If you type n
, it will cancel the move for that file.
Example 5: Force Overwrite with -f
Option
If you want to move files without any prompt, use the -f
(force) option.
Command:
Output:
No output is shown because it forcibly overwrites the file without confirmation. If the destination file exists, it will be overwritten silently.
Example 6: Moving All Files with a Specific Extension
You can move all files of a certain type (e.g., .txt
) to a directory using a wildcard (*
).
Command:
Output:
This command is useful for batch operations. Each .txt
file in the current directory will be moved to /home/user/Documents/
.