JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

How to Move Files and Directories in Linux Using mv Command

Author:JIYIK Last Updated:2025/04/05 Views:

mvWe can move files and directories through the Linux terminal using the command with various parameters .

Use mvthe 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.

mvCommand Syntax

mv [OPTIONS] source destination

The in the above command sourcecan be one or more files or directories, destinationbut 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 mvthe 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.svgto 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.svgrenamed 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.svgrename the file to 1.svg.

In some cases, the destination file may already exist mvand it will be overwritten if we use the -r command. In order to prompt for confirmation before overwriting, we use -ithe -p option with mvthe -r command.

mv -i ILLUMEE.svg 1.svg

If the file name 1.svgalready 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 Nthe key and then press Enterthe key, otherwise press Ythe key and then press Enterthe key.

We can also prevent overwriting by using the -p option mvof the command .-n

mv -n ILLUMEE.svg 1.svg

If 1.svgit already exists, it will prevent overwriting.

Use mvthe 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_Scriptsto 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 mvthe 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.pngImages

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

.jpgIt moves all files ending with to JPG-Imagesthe folder.

Back up existing files

To make a backup of the existing file, we use -bthe -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.

Article URL:

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

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 时间。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial