One of the most basic tasks you’ll perform as a Linux user or administrator is moving files and directories. This article covers how to transfer files and directories in both the Linux GUI and the terminal.
For this guide, we’re using Ubuntu LTS 22.04, codenamed Jammy Jellyfish. The Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.
Note: you can only move files or directories you have appropriate permissions for and only move them to directories to which you have the appropriate permissions.
Moving Files in the GUI
There are several different ways to move files and directories in the Linux GUI. You can select multiple files and directories to move by using the <Ctrl> key when choosing them.
Move Files via Drag and Drop
In this example, we will move a file, yellow.txt, via drag and drop from the source directory, /techobservatory/orange, to the target directory, /techobservatory.
- Launch the file manager.
- Open both the target directory, /techobservatory, and source directory, /techobservatory.com, containing the specified file, yellow.txt.
- Select yellow.txt and while holding down the left mouse button, drag it to the target directory/techobservatory.
- Release, or drop the specified file.
- yellow.txt was successfully moved from /techobservatory/orange to /techobservatory.
Move via Cut and Paste
In this example, we will move both a specified file, blue.txt, and a specified directory, /techobservatory/pink, from the source directory, /techobservatory, to the target directory, /techobservatory/green.
-
- Launch your distro’s file manager.
- Navigate to the source directory, /techobservatory, containing blue.txt, and the specified folder, pink, you want to move. Select both while holding down the <Ctrl> key, then right-click them.
- Select Cut from the drop-down menu.
- Navigate to the target directory, right-click your mouse button and select Paste from the drop-down menu
Note: you use the <Ctrl>C and <Ctrl>V shortcuts to copy-paste them.
Move Files via Move To…
Linux distros using GNOME as their desktop environment usually use Nautilus as their default file manager. Nautilus has an option that allows users to right-click and select a Move To… option to move their files and directories.
In this example, we will move the file yellow.txt to the directory /techobservatory/orange.
-
- Launch Nautilus.
- Navigate to the source directory, select the file you want to move, and right-click your mouse button. Choose the Move To… option from the drop-down menu.
- Now, left-click the target directory, /techobservatory/orange.
- The file will automatically be moved to the target directory.
Moving Files via the Command Line
Linux’s only built-in command to move files is the mv command.
mv Command Purpose, Syntax, and Options
The mv command is used to move or rename files or directories. The syntax of the mv command is:
# mv [options] <source> <destination>
We will only be using the -v option in our examples in this section. The -v option, or verbose, explains what is being done with the moved files or directories.
Real-Life Scenario – Moving Files
In this scenario, we will move the specified file, purple.txt, to the source directory, /techobservatory/green.
- Open the terminal.
- Change directory to the target directory/techobservatory.
# cd /techobservatory
- Move the specified file, purple.txt, to the specified directory, /techobservatory/green.
# mv purple.txt /techobservatory/green
- Verify that the specified file, purple.txt, was moved to the target directory, /techobservatory/green.
# ls -Al /techobservatory/green
- The specified file, purple.txt, was successfully moved to the target directory, /techobservatory/green.
Real-Life Scenario – Moving Directories
- In this example, we will move the directory /techobservatory/green to the directory /techobservatory/pink.
- Open the terminal.
- Change to the source directory/techobservatory.
# cd /techobservatory
- Move the specified directory, /techobservatory/green, to the target directory, /techobservatory/pink.
# mv -v /techobservatory/green /techobservatory/pink
- Verify that the specified directory, /techobservatory/green, was moved to the target directory, /techobservatory/pink.
# ls -AL /techobsevatory/pink
- The specified directory, /techobservatory/green, was successfully moved to the target directory, /techobservatory/pink.
Real-Life Scenarios – Moving Multiple Files
Move Two or More Files
In this example, we will move the specified files to /techobservatory/auburn.
- Open the terminal.
- Change to the source directory/techobservatory.
# cd /techobservatory
- Move the specified files, red.txt, blue.txt, and brown.txt, to the target directory, /techobservatory/auburn.
-
# mv -v red.txt blue.txt brown.txt /techobservatory/auburn
- List the directory contents of the target directory, /techobservatory/auburn.
# ls -Al /techobservatory/auburn
- The specified files, red.txt, blue.txt, and brown.txt, were successfully moved to the target directory, /techobservatory/auburn.
Move Multiple Files Using the Wildcard (*)
In this example, we will move all files beginning with the letter b to the /techobservatory/pink directory using the wildcard (*) option.
- Open the terminal.
- Change to the source directory/techobservatory directory.
# cd /techobservatory
- Move the target files (all files beginning with the letter b to the target directory, /techobservatory/pink.
# mv -v b*.* /techobservatory/pink
- Verify that the specified files (all files beginning with the letter b) were moved to the target directory, /techobservatory/pink.
# ls -Al /techobservatory/pink
- All of the specified files (all files beginning with the letter b) were successfully moved to the target directory, /techobservatory/pink.
Commands Used In This Article
- mv – move or rename files.
- ls – list directory contents.
- cd – change directory.