How to Move Directories in Linux

September 8, 2021

Introduction

Managing files and directories is one of the most basic tasks when working in a Linux system. You can move directories in Linux using the GUI and system commands.

In this tutorial, we will cover the two methods you can use to move directories in Linux.

How to move directories in Linux

Prerequisites

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window/command line

How to Use mv Command to Move Directories in Linux

The mv command is a Linux system utility that allows you to move files and directories from one place to another. It uses the following command syntax:

mv [options] [source] [destination]

For instance, to move the Example directory to the Downloads directory use:

mv Example Downloads

There will be no output if the command has been successful.

Moving multiple directories works by changing the syntax to include multiple sources:

mv [source 1] [source 2] … [source n] [destination]

For example, to move the Example1, Example2, and Example3 directories to the Downloads directory use:

mv Example1 Example2 Example3 Downloads

Note: Learn how to create new directories in Linux using the terminal in our guide to the Linux mkdir command.

The mv command is also used to rename files and directories with the following syntax:

mv [old filename] [new filename]

For example, renaming the Example file into Test:

mv Example Test

Note: If the destination directory does not exist, the mv command will automatically rename the source directory to the destination name.

mv Command Options

The mv command uses the following options:

  • --backup: Creates a backup of the destination file. Allows you to pass additional arguments to change how the command behaves.
  • -b: Works like --backup but does not accept arguments.
  • -f: Automatically overwrite destination files.
  • -i: Enables interactive mode, with a prompt asking for input before overwriting files.
  • -n: Disables overwriting destination files.
  • --strip-trailing-slashes: Removes trailing slashes ("/") from source file or directory names.
  • -S: Provide a new suffix for the backup files.
  • -t: Move all provided sources into a destination directory.
  • -T: Treat the provided destination as a file instead of a directory.
  • -u: Update destination files.
  • -v: Show command output in verbose format.
  • -Z: Set the destination file security context to default.

If the destination file already exists, using the -b option allows you to create a backup of the existing file:

mv -b test.txt Example

In this example, the mv command creates a backup of the existing test.txt file in the Example directory before overwriting. The backup file has the same name as the original, with a tilde ("~") added to the end. Use the ls command to verify the backup file:

ls Example
Using the ls command to verify the backup destination file

Change the default suffix for backup files (tilde) by combining the --backup and -S options. The new suffix is passed as an argument after the -S option and before specifying the source and destination files or directories:

mv --backup -S 01 test.txt Example

In this example, the new backup filename suffix is <em>01</em>:

ls Example
Using the -S option to change the backup file name suffix

Using the -i option prompts the user to confirm overwriting existing files. Typing Y or N and pressing Enter confirms or cancels overwriting, respectively:

mv -i test.txt Example
The interactive mode causes a prompt to confirm overwriting files

The -f option automatically overwrites any existing files in the destination directory, while the -n option disables overwriting and skips existing files. Using the -u option only overwrites the file if the source file is newer than the destination file of the same name.

Have the mv command output show the step-by-step process of moving files and directories by using the -v option:

mv -v test01.txt test02.txt test03.txt Example
Enabling verbose command output with the -v option

Note: Be careful not to move a directory or file to /dev/null also known as "the black hole". We outline this and other dangerous terminal commands in our article 14 Dangerous Linux Terminal Commands.

How to Move Directories Using GUI in Linux

There are two methods of moving directories in Linux using the GUI.

Option 1: Copy and Paste

1. Select the directory you want to move and press Ctrl+X. Alternatively, right-click the directory and select Cut from the drop-down menu.

Right-click the directory and select Cut from the drop-down menu

2. Navigate to the destination and press Ctrl+V or right-click the empty space and select Paste from the drop-down menu to move the directory.

Select Paste from the drop-down menu to move the directory

Option 2: Move To... Option

1. Another method is to right-click the directory and select Move to… from the drop-down menu.

Right-click the directory and select Move to... from the drop-down menu

2. In the Select Move Destination window, navigate to the destination and click Select to move the directory.

navigate to destination and click Select to move directory

Note: If you are using Ubuntu, check out our tutorial on how to install a GUI on Ubuntu.

Conclusion

After reading this article, you should know how to move directories in Linux using the GUI and the built-in mv command.

Learn more about using Linux commands in our comprehensive overview of Linux Commands.

For more information about Linux directories, make sure to read our article how to rename a directory in Linux.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Get the Size of a Directory in Linux
February 8, 2024

Use the command line interface to display directory size and for display disk space. This guide will help you find the amount of disk space used by any given directory.
Read more
How to Remove (Delete) a File or Directory in Linux
September 8, 2021

This article lists the most commonly used commands and tools to remove unwanted files and directories from your system.
Read more
How To Use shred Linux Command
March 31, 2021

Deleting a file in Linux or any other OS does not actually remove the data from the drive. The best practice is to use the shred command to permanently destroy sensitive data so that it cannot be recovered.
Read more
How to Format Disk Partitions in Linux
December 2, 2020

Formatting and mounting disk partitions in Linux is common because partitions cannot be used without this process. In this tutorial you will learn how to format and mount disk partitions with ext4, FAT32, or NTFS file systems.
Read more