How to Move Files in Linux

In Linux, moving files from one location to another is done using the mv command. This command can also be used to rename files or directories. It's a key part of file and folder management in the Linux terminal.

Basic Syntax

mv [options] source target

Common Use Cases

Options Explained

Example

$ ls
file1.txt  file2.txt  backup/

$ mv file1.txt backup/

$ ls backup/
file1.txt

Tips

How to Move Files in Linux – Complete Guide

Moving files in Linux is a fundamental task for organizing, managing, and manipulating your system’s filesystem. Whether you’re a beginner looking to understand the command-line interface or an experienced system administrator writing automation scripts, learning how to move files efficiently is essential. In this comprehensive guide, we’ll cover various methods to move files in Linux using the command line, GUI, automation scripts, and best practices.

Why Move Files in Linux?

Moving files is necessary for many use cases such as:

1. Using the mv Command

The most common way to move files in Linux is with the mv (move) command. It is used both to move files to new directories and to rename them.

Basic Syntax

mv [options] source destination

Example: Move a file to another directory

mv myfile.txt /home/user/Documents/

Example: Rename a file

mv report.txt summary.txt

Example: Move multiple files

mv file1.txt file2.txt /home/user/Backup/

Important mv Options

2. Moving Directories

You can also move entire folders, including their contents, with the mv command.

Example: Move a directory

mv old_folder/ /home/user/Projects/

Example: Rename a directory

mv temp_files/ archive_2024/

3. Using Wildcards to Move Files by Pattern

Wildcards like * and ? can be used to move multiple files matching a pattern.

Example: Move all .jpg files

mv *.jpg /home/user/Pictures/

Example: Move files starting with "log"

mv log* /var/log/old_logs/

4. Using GUI to Move Files (for Desktop Users)

If you're using a desktop environment like GNOME, KDE, or XFCE, you can move files with a drag-and-drop action in the file manager (Nautilus, Dolphin, Thunar, etc.).

Steps:

  1. Open the file manager
  2. Select the file or folder
  3. Right-click and choose “Cut”
  4. Navigate to the destination
  5. Right-click and choose “Paste”

5. Moving Files with rsync

Though rsync is typically used for copying, it can also be used to move files with the --remove-source-files option.

Example:

rsync -av --remove-source-files ./source_dir/ /destination_dir/

6. Moving Files Across Partitions or Disks

When you use mv to move files across partitions or external drives, Linux copies the file and then deletes the original.

Example: Move a file to USB drive

mv myvideo.mp4 /media/user/USB_Drive/

7. Automating File Movement with Scripts

In bash or shell scripts, you can automate moving files based on conditions like age, size, or name patterns.

Example: Move files older than 30 days


find /home/user/Downloads -type f -mtime +30 -exec mv {} /home/user/OldDownloads/ \;
  

Example: Move today's logs


TODAY=$(date +%F)
mkdir -p /backup/$TODAY
mv /var/log/*.log /backup/$TODAY/
  

8. Move Files with Cron Jobs

You can schedule file movements with cron for automated cleanup or archival.

Example Cron Job:


0 2 * * * mv /tmp/*.bak /home/user/backup/
  

9. Move Hidden Files

To move hidden files (starting with a .), you must reference them explicitly or use shell options to include them.

Example:

mv .env .config /backup/

10. Move Files Using Midnight Commander (MC)

mc is a terminal-based file manager that allows you to move files interactively with keyboard shortcuts.

sudo apt install mc

Launch with:

mc

11. Safe Practices While Moving Files

12. Troubleshooting Common mv Errors

13. Move Files Between Remote Systems (via scp or rsync)

To move files between systems, you can use:

Using scp:

scp myfile.txt user@remote_host:/home/user/

Using rsync with deletion:

rsync -av --remove-source-files myfile.txt user@remote:/home/user/

14. Move Files Based on File Type or Size

find . -type f -name "*.pdf" -exec mv {} ~/Documents/PDFs/ \;
find . -type f -size +100M -exec mv {} ~/BigFiles/ \;

15. Move Files Using xargs (for speed)

find . -name "*.zip" | xargs -I {} mv {} /archive/zips/

Final analysis

Mastering how to move files in Linux is a core skill for every user. Whether through simple mv commands or advanced automation with find, cron, and rsync, Linux provides numerous flexible and efficient ways to manage your files. Following best practices ensures safe operations and reduces the risk of file loss, especially in production systems or during administrative tasks. Once you’re familiar with the basic and advanced techniques covered in this guide, you’ll be able to move and organize your files in Linux like a pro.

Related Topics

16. Using find + mv to Organize Files by Extension

When you have a large number of files of different types, you can use the find command in combination with mv to sort files into folders by their extensions.

Example: Move all .mp4 files to “Videos” folder


mkdir -p ~/Videos
find . -type f -name "*.mp4" -exec mv {} ~/Videos/ \;
  

Example: Move .docx and .pdf files to “Documents”


mkdir -p ~/Documents
find . -type f \( -name "*.docx" -o -name "*.pdf" \) -exec mv {} ~/Documents/ \;
  

17. Move Files and Preserve Timestamps

By default, mv preserves the file's timestamps. However, when moving between filesystems, this may not hold. If preserving timestamps is important, consider using rsync:

rsync -a --remove-source-files file1.txt /destination/

18. Move Files Based on Modified Date

You can move files based on how recently they were modified using the -mtime or -newermt flag with find.

Move files modified in the last 7 days

find . -type f -mtime -7 -exec mv {} /recent-files/ \;

Move files modified before a specific date

find . -type f ! -newermt "2024-01-01" -exec mv {} /archive/pre2024/ \;

19. Using GUI Automation Tools to Move Files

On Linux desktops, you can also use automation tools like:

20. Move Files in a Loop with Bash

When automating with shell scripts, you may want to loop through files and move them based on custom logic:

Example: Move only files larger than 5MB


for file in *; do
  if [[ -f "$file" && $(stat -c%s "$file") -gt 5242880 ]]; then
    mv "$file" /large-files/
  fi
done
  

21. Dry Run Before Moving Files

To avoid mistakes, simulate file movement before actually doing it. Use echo in place of mv in scripts:


find . -name "*.bak" -exec echo mv {} /backup/ \;
  

Once verified, remove echo to execute the move.

22. Move Files Based on Ownership or Permissions

You can identify and move files based on their owner or permission settings.

Move all files owned by user 'john'

find /data -user john -exec mv {} /home/john/files/ \;

Move all files with write permission for others

find . -type f -perm -002 -exec mv {} /unsafe-files/ \;

23. Move Files While Renaming with Date/Time

To avoid overwriting or to timestamp files, you can rename them as you move them:


for file in *.log; do
  mv "$file" "/logs/${file%.log}_$(date +%F).log"
done
  

This moves and appends a date suffix to all .log files.

24. Handling Spaces in Filenames

When moving files with spaces, wrap variables in quotes to avoid syntax errors:


mv "file with spaces.txt" /destination/
  

25. Move All Except Certain Files

Sometimes you may want to move all files except specific types or names.

Example: Move everything except .zip files


shopt -s extglob
mv !(*.zip) /destination/
  

26. Check Disk Space Before Moving

Ensure you have space before moving large files:

df -h /destination/

27. Moving Files Using mc (Midnight Commander)

If you're working in a terminal and want a visual tool:

28. Secure File Movement Over SSH

If you're moving files securely between machines, scp or rsync over SSH are best:

Example:

scp file.sql user@remote:/var/backups/

29. Moving System Files – Use with Caution

When moving files in system-critical directories like /etc or /usr, always take a backup and double-check permissions.

Best Practice:

cp -rp /etc/important.conf /etc/important.conf.bak && mv /etc/important.conf /etc/config/

30. Recap: Essential Tips for Moving Files in Linux

Final analysis

Moving files in Linux is more than just shifting data—it’s about organizing your system, automating tasks, and maintaining efficiency. From using simple mv commands to advanced file movement strategies with find, rsync, and cron, Linux provides unparalleled flexibility. Whether you're building scripts, cleaning up your workspace, or managing servers, mastering file movement techniques will make you a more efficient and confident Linux user.

FAQs – How to Move Files in Linux

See Also