How To Move Directory In Linux – Move Directory Recursively With Mv

Moving a directory in Linux uses the mv command followed by the source and destination paths. This simple command is one of the most essential tools for managing files and folders on any Linux system. Whether you are a beginner or a seasoned user, understanding how to move directory in Linux will save you time and prevent errors.

The mv command works for both files and directories. It can also rename a folder if the destination is on the same filesystem. In this guide, you will learn every detail about moving directories, from basic syntax to advanced options.

How To Move Directory In Linux

The mv command stands for “move”. It is a built-in command found in all Linux distributions. To move a directory, you type mv, then the source directory name, then the destination path. For example: mv myfolder /home/user/Documents/. This moves the folder named myfolder into the Documents directory.

One important thing to remember: if the destination already has a folder with the same name, mv will overwrite it without warning. Always double-check your paths before pressing Enter.

Basic Syntax For Moving Directories

The general syntax is: mv [options] source destination. The source is the directory you want to move. The destination can be another directory path or a new name if you want to rename it.

  • Source: The directory you are moving. Can be relative or absolute path.
  • Destination: Where you want the directory to go. Can be a folder path or a new name.
  • Options: Flags like -i for interactive mode or -v for verbose output.

For example, to move a folder named “projects” into your home directory: mv projects ~/. The tilde (~) represents your home directory. This is a quick and common way to move directories.

Moving A Directory To A Different Location

To move a directory to a completely different location, specify the full path. Suppose you have a folder called “data” in your current directory and want to move it to /var/backups/. The command would be: mv data /var/backups/. This moves the entire folder and all its contents.

If the destination directory does not exist, mv will rename the source directory to the destination name. This can be confusing. For instance, mv data /var/backups/ will move data into backups if backups exists. If backups does not exist, it will rename data to backups. Always ensure the destination folder exists if you want to move inside it.

Renaming A Directory With Mv

The mv command is also used to rename directories. To rename a folder, use the same syntax but with a new name as the destination. For example: mv oldname newname. This renames the directory from oldname to newname within the same parent folder.

Renaming works only if the new name does not already exist in that location. If it does, mv will overwrite it. Use the -i flag to get a confirmation prompt before overwriting: mv -i oldname newname.

Moving Multiple Directories At Once

You can move several directories at the same time by listing them all before the destination. The last argument must be a directory. For example: mv dir1 dir2 dir3 /target/location/. This moves dir1, dir2, and dir3 into the target location.

Be careful: if any of the source directories have the same name as an existing folder in the destination, they will be overwritten. Use the -n flag to prevent overwriting: mv -n dir1 dir2 /target/. This will skip moving if the destination already contains a folder with that name.

Using Options With Mv For Safety

The mv command has several options that make it safer and more informative. Here are the most useful ones:

  • -i (interactive): Prompts before overwriting any existing file or directory.
  • -u (update): Moves only if the source is newer than the destination or if the destination is missing.
  • -v (verbose): Shows each directory as it is moved.
  • -n (no-clobber): Prevents overwriting existing directories.

For example, to move a folder with confirmation: mv -i myfolder /backup/. If a folder named myfolder already exists in /backup/, you will be asked to confirm. This prevents accidental data loss.

Moving Directories With Special Characters

If your directory name contains spaces or special characters, you need to escape them. Use quotes or a backslash. For example: mv "my folder" /target/ or mv my\ folder /target/. Without escaping, the command will treat “my” and “folder” as separate arguments.

Another method is to use tab completion. Type the first few letters of the directory name and press Tab. The shell will automatically escape spaces for you. This is the safest way to handle tricky names.

Moving Directories With Wildcards

Wildcards like * and ? can help you move multiple directories that match a pattern. For example, to move all directories starting with “log” into an archive folder: mv log* /archive/. This moves log1, log_backup, log2024, etc.

Be careful with wildcards: they match files too. If you only want directories, use a combination with find or ls -d. For instance: mv $(ls -d */) /target/ moves all directories in the current folder.

Moving Directories Across Filesystems

When you move a directory to a different filesystem (like from /home to /mnt/usb), mv actually copies the data and then deletes the original. This can take time for large directories. The command remains the same: mv /home/user/data /mnt/usb/.

If the move fails partway through, you may end up with partial data in both locations. To avoid this, use rsync or cp followed by rm for large moves across filesystems. The mv command does not have a resume feature.

Moving Directories With Absolute And Relative Paths

You can use either absolute paths (starting from root /) or relative paths (starting from current directory). For example: mv /var/log/oldlogs ./ moves the oldlogs directory to your current location. The dot (.) represents the current directory.

Relative paths are shorter but can be confusing if you change directories often. Absolute paths are always precise. Choose whichever makes the command easier to read.

Moving Directories Containing Hidden Files

Hidden files (starting with a dot) are moved along with the directory. For example, if you move a folder that contains .config, the hidden file goes with it. No special flag is needed. The mv command treats hidden files like any other file.

To verify that hidden files were moved, use ls -a on the destination. This shows all files, including hidden ones. You can also use mv -v to see each file as it is moved.

Moving Directories With Sudo

If you need to move a directory that belongs to another user or is in a system location, use sudo. For example: sudo mv /etc/oldconfig /backup/. This gives you root privileges to perform the move.

Be extremely careful with sudo. A wrong move can break your system. Always double-check the source and destination paths. Use sudo mv -i for an extra layer of safety.

Common Mistakes When Moving Directories

Many beginners make these errors when learning how to move directory in Linux. Avoid them to keep your data safe:

  • Forgetting the trailing slash on the destination: mv dir /target might rename dir to target if target does not exist. Add a slash: mv dir /target/ to move inside.
  • Overwriting existing directories without backup: Always use -i or -n.
  • Moving a directory into itself: This creates a loop and can cause errors. For example, mv dir dir/subdir is invalid.
  • Using wildcards too broadly: mv * /target/ moves everything, including hidden files and parent directories. Be specific.

Moving Directories Using Find And Mv Together

For advanced moves, combine find with mv. This is useful for moving directories that match certain criteria. For example, to move all empty directories to a trash folder: find . -type d -empty -exec mv {} /trash/ \;. This finds empty directories and moves them.

You can also use find with -name to match patterns. For instance: find . -type d -name "temp*" -exec mv {} /tmp/ \;. This moves all directories starting with “temp” to /tmp.

Moving Directories With A Script

If you need to move directories regularly, write a simple bash script. For example:

#!/bin/bash
SOURCE="/home/user/projects"
DEST="/backups/projects"
mv -v "$SOURCE" "$DEST"

Save the script as move_projects.sh, make it executable with chmod +x move_projects.sh, and run it. This automates the process and reduces errors.

Checking If The Move Was Successful

After moving a directory, verify it with ls. Check both the source location (it should be gone) and the destination (it should be there). Use ls -la to see details. You can also use tree if installed to view the directory structure.

If you used the -v flag, the terminal will show each step. Look for error messages like “cannot move” or “permission denied”. These indicate problems that need fixing.

Recovering From A Mistaken Move

If you accidentally move a directory to the wrong place, you can move it back using the same command. For example, if you moved data to /wrong/, just run mv /wrong/data /correct/. There is no undo command, so act quickly.

If you overwrote an existing directory, recovery is harder. Check if you have backups. Some filesystems support snapshots. Otherwise, use data recovery tools like testdisk or extundelete, but success is not guaranteed.

Moving Directories With Graphical Tools

If you prefer a GUI, file managers like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE) allow drag-and-drop moving. Right-click a folder and select “Cut”, then navigate to the destination and “Paste”. This is the same as using mv behind the scenes.

Graphical tools are safer for beginners because they show confirmation dialogs. However, they are slower for bulk operations. For moving many directories, the command line is faster.

Moving Directories Over SSH

To move directories on a remote server, use SSH. First, connect: ssh user@server. Then run the mv command as usual. For example: mv /home/user/data /var/www/. This moves the directory on the remote machine.

If you need to move directories between two remote servers, use scp or rsync instead. The mv command works only within the same filesystem on the same machine.

Moving Directories With Rsync For Large Data

For very large directories (gigabytes or terabytes), consider using rsync instead of mv. Rsync copies data and can resume if interrupted. After copying, delete the source with rm -rf. Example: rsync -av /source/ /destination/ && rm -rf /source/. This is safer than mv across filesystems.

Rsync also preserves permissions, timestamps, and symlinks. Use the -a (archive) flag for a complete copy. This is the preferred method for system administrators.

Moving Directories With Symbolic Links

If you move a directory that contains symbolic links, the links will still point to the original locations unless they are relative. Absolute links break if the target is moved. To avoid issues, use cp -a with the --preserve=links option, or update the links manually.

You can also use readlink to check where a symlink points. After moving, recreate the links with ln -s if needed.

Moving Directories With Permissions

When you move a directory, its permissions and ownership remain the same. However, if you move to a different filesystem, the new copy may inherit the umask of the destination. Use chmod and chown after the move to adjust permissions if necessary.

For example, after moving a directory to a shared location, run: chmod -R 755 /destination/folder. This sets read, write, and execute permissions for the owner, and read and execute for others.

Moving Directories With Date Stamps

To move directories based on date, use find with -mtime. For example, to move directories older than 30 days: find . -type d -mtime +30 -exec mv {} /archive/ \;. This is useful for log rotation or cleanup tasks.

Test the command first with -print instead of -exec to see which directories will be moved. This prevents accidental moves.

Moving Directories With Case Sensitivity

Linux filesystems are case-sensitive. Moving a directory named “Data” to a location that already has “data” will create two separate folders. Use the -i flag to avoid confusion. If you want to merge, you need to copy contents manually.

On case-insensitive filesystems (like some external drives), mv may overwrite without warning. Always check the destination first.

Moving Directories With No Destination Specified

If you run mv without a destination, it will show an error: mv: missing file operand. Always provide both source and destination. If you only want to rename, the destination is the new name within the same directory.

For example, mv myfolder is invalid. You need mv myfolder newname or mv myfolder /path/.

Moving Directories With Interactive Prompts

The -i flag is your friend. It asks before overwriting. This is especially useful when moving multiple directories. For example: mv -i * /target/. You will be prompted for each conflict. Press y to overwrite, n to skip.

If you are moving many directories and want to skip all conflicts, use -n instead. This prevents any overwrites.

Moving Directories With Verbose Output

The -v flag shows what is being moved. For example: mv -v myfolder /target/ outputs: “renamed ‘myfolder’ -> ‘/target/myfolder'”. This is helpful for debugging or logging.

Combine -v with -i for a safe and informative move: mv -vi myfolder /target/. You see each step and get prompts.

Moving Directories With Backup Option

Some versions of mv support the --backup option. This creates a backup of any existing destination file or directory. For example: mv --backup myfolder /target/. If /target/myfolder exists, it is renamed to myfolder~.

This option is not available on all systems. Check your man page with man mv. If not