How to Move Files and Directories in Linux Using mv Command
mv
We can move files and directories through the Linux terminal using the command with various parameters .
Use mv
the command to move files and directories
mv(move)
The command can move files and directories from one location to another. It can also be used to rename files and directories.
mv
Command Syntax
mv [OPTIONS] source destination
The in the above command source
can be one or more files or directories, destination
but is always one file or directory.
If we have multiple files or directories as source, the target is always a directory. In this case, all source files and directories will be moved to the target directory. If we have a single file as source and a directory as target, the file will be moved to the target directory.
One thing to note when moving files and directories is that if we do not have write permissions on the source file and destination directory, we will get a permission denied error.
Use mv
the command to move the file to the directory
We can use the following command to move files within a directory.
mv <filename> <path_of _destination_directory>
mv ILLUMEE.svg SVG
It moves the files in the current working directory ILLUMEE.svg
to the folder in the current working directory SVG
.
If the destination directory does not exist, the source file is renamed to the destination file.
If the directory does not exist in the current working directory SVG
, the file is ILLUMEE.svg
renamed to SVG
.
If the target path is also a file name, the source and target file names are renamed.
mv ILLUMEE.svg 1.svg
It will ILLUMEE.svg
rename the file to 1.svg
.
In some cases, the destination file may already exist mv
and it will be overwritten if we use the -r command. In order to prompt for confirmation before overwriting, we use -i
the -p option with mv
the -r command.
mv -i ILLUMEE.svg 1.svg
If the file name 1.svg
already exists, the terminal will prompt us before overwriting it.
mv -i ILLUMEE.svg 1.svg
Output:
mv: overwrite '1.svg'? n
If you want to prevent overwriting, press N
the key and then press Enterthe key, otherwise press Y
the key and then press Enterthe key.
We can also prevent overwriting by using the -p option mv
of the command .-n
mv -n ILLUMEE.svg 1.svg
If 1.svg
it already exists, it will prevent overwriting.
Use mv
the command to move a directory into another directory
To move a directory inside another directory, we can use the following command.
mv <path_of_source_directory> <path_of _destination_directory>
mv Python_Scripts Python_Scripts_New
It moves a directory in the current working directory Python_Scripts
to another directory in the current working directory Python_Scripts_New
.
If the destination directory does not exist, the source directory is renamed to the destination directory.
Use mv
the command to move multiple files to another directory
To move multiple files to another directory, we specify the destination directory path after specifying all the source files.
mv <source_filepath_1> <source_filepath_2> <source_filepath_3>
<path_of_destination_directory>
mv 1.jpg 2.jpg 2.png Images
It moves the file in our current working directory 1.jpg
, 2.jpg
, to another directory under the current working directory .2.png
Images
We can also move multiple files in a directory by using regular expressions to match the file names that need to be moved.
mv *.jpg JPG-Images
.jpg
It moves all files ending with to JPG-Images
the folder.
Back up existing files
To make a backup of the existing file, we use -b
the -r option. It will create a backup of the file that is being overwritten and append -r characters to the name of the backup file ~
.
mv -b abc.jpg 123.jpg
ls
Output:
123.jpg 123.jpg~
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Copy Files and Directories Using Linux Terminal
Publish Date:2025/04/05 Views:122 Category:OPERATING SYSTEM
-
cp We can use the and commands in Linux Terminal rsync to copy files and directories. cp The command is generally used to copy files, while rsync the command is generally used to copy directories. Use cp the command to copy files We use com
Read CPU flags from Cpuinfo
Publish Date:2025/04/05 Views:78 Category:OPERATING SYSTEM
-
This article will explain how to read the information in Linux cpuinfo . Later, we will look at what are CPU flags and what they mean. Reading in Linux cpuinfo /proc/cpuinfo The file contains detailed information about the CPU in your compu
Rsync Exclude Files and Directories in Linux
Publish Date:2025/04/05 Views:92 Category:OPERATING SYSTEM
-
rsync Is a powerful command line tool for synchronizing files and directories between two sites using a remote shell. Using rsync the command, you can copy data and files between systems and make additional backups. Additionally, you can ex
使用 PowerShell 查找 CPU 和 RAM 使用情况
Publish Date:2024/03/01 Views:146 Category:编程语言
-
本教程将教你使用 PowerShell 查找 CPU 和 RAM 使用情况。
Python rsync 同步
Publish Date:2023/06/22 Views:264 Category:Python
-
本文将探讨 rsync 以及我们如何从 Python 脚本中使用它。Python同步如上所述,rsync 是一个强大的工具
使用 Python 获取 CPU 数量
Publish Date:2023/06/15 Views:399 Category:Python
-
CPU 可以包含单核或多核。 单核只处理一个进程,而多核同时处理多个进程。本篇文章将介绍使用 Python 程序查找 CPU 内核总数的不同方法。使用 multiprocessing 模块获取 Python 中的 CPU 数量
Python获取CPU温度
Publish Date:2023/06/15 Views:290 Category:Python
-
本文的主要目的是演示如何借助 Python 中的 pythonnet 库读取和显示 CPU 温度。Python获取CPU温度
MATLAB CPU 时间
Publish Date:2023/04/23 Views:196 Category:MATLAB
-
本教程将讨论在 MATLAB 中使用 tic、toc 和 cputime 命令检查 CPU 时间。
如何在 Linux 中使用 mv 命令移动文件和目录
Publish Date:2023/03/17 Views:612 Category:操作系统
-
我们可以通过 Linux 终端使用带有各种参数的 mv 命令来移动文件和目录。