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.
mv [options] source target
mv notes.txt /home/user/Documents/mv oldname.txt newname.txtmv myfolder/ /home/user/Desktop/mv -f file.txt destination/-i: Prompt before overwrite (interactive mode)-f: Force overwrite without asking-n: Never overwrite existing files-v: Verbose mode; shows what is being moved$ ls
file1.txt file2.txt backup/
$ mv file1.txt backup/
$ ls backup/
file1.txt
mv -i when unsure to prevent accidental overwrites.mv *.txt backup/pwd before moving files.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.
Moving files is necessary for many use cases such as:
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.
mv [options] source destination
mv myfile.txt /home/user/Documents/
mv report.txt summary.txt
mv file1.txt file2.txt /home/user/Backup/
-i: Prompt before overwrite-f: Force overwrite without prompt-n: Do not overwrite existing files-v: Verbose – show what’s being movedYou can also move entire folders, including their contents, with the mv command.
mv old_folder/ /home/user/Projects/
mv temp_files/ archive_2024/
Wildcards like * and ? can be used to move multiple files matching a pattern.
mv *.jpg /home/user/Pictures/
mv log* /var/log/old_logs/
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.).
Though rsync is typically used for copying, it can also be used to move files with the --remove-source-files option.
rsync -av --remove-source-files ./source_dir/ /destination_dir/
When you use mv to move files across partitions or external drives, Linux copies the file and then deletes the original.
mv myvideo.mp4 /media/user/USB_Drive/
In bash or shell scripts, you can automate moving files based on conditions like age, size, or name patterns.
find /home/user/Downloads -type f -mtime +30 -exec mv {} /home/user/OldDownloads/ \;
TODAY=$(date +%F)
mkdir -p /backup/$TODAY
mv /var/log/*.log /backup/$TODAY/
You can schedule file movements with cron for automated cleanup or archival.
0 2 * * * mv /tmp/*.bak /home/user/backup/
To move hidden files (starting with a .), you must reference them explicitly or use shell options to include them.
mv .env .config /backup/
mc is a terminal-based file manager that allows you to move files interactively with keyboard shortcuts.
sudo apt install mc
Launch with:
mc
-i flag to avoid overwriting important filespwd and ls before movingTo move files between systems, you can use:
scp myfile.txt user@remote_host:/home/user/
rsync -av --remove-source-files myfile.txt user@remote:/home/user/
find . -type f -name "*.pdf" -exec mv {} ~/Documents/PDFs/ \;
find . -type f -size +100M -exec mv {} ~/BigFiles/ \;
find . -name "*.zip" | xargs -I {} mv {} /archive/zips/
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.
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.
mkdir -p ~/Videos
find . -type f -name "*.mp4" -exec mv {} ~/Videos/ \;
mkdir -p ~/Documents
find . -type f \( -name "*.docx" -o -name "*.pdf" \) -exec mv {} ~/Documents/ \;
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/
You can move files based on how recently they were modified using the -mtime or -newermt flag with find.
find . -type f -mtime -7 -exec mv {} /recent-files/ \;
find . -type f ! -newermt "2024-01-01" -exec mv {} /archive/pre2024/ \;
On Linux desktops, you can also use automation tools like:
When automating with shell scripts, you may want to loop through files and move them based on custom logic:
for file in *; do
if [[ -f "$file" && $(stat -c%s "$file") -gt 5242880 ]]; then
mv "$file" /large-files/
fi
done
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.
You can identify and move files based on their owner or permission settings.
find /data -user john -exec mv {} /home/john/files/ \;
find . -type f -perm -002 -exec mv {} /unsafe-files/ \;
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.
When moving files with spaces, wrap variables in quotes to avoid syntax errors:
mv "file with spaces.txt" /destination/
Sometimes you may want to move all files except specific types or names.
shopt -s extglob
mv !(*.zip) /destination/
Ensure you have space before moving large files:
df -h /destination/
If you're working in a terminal and want a visual tool:
sudo apt install mcF6 to move selected filesIf you're moving files securely between machines, scp or rsync over SSH are best:
scp file.sql user@remote:/var/backups/
When moving files in system-critical directories like /etc or /usr, always take a backup and double-check permissions.
cp -rp /etc/important.conf /etc/important.conf.bak && mv /etc/important.conf /etc/config/
mv for simple moves and renamesrsync for cross-disk or remote movesfind with mv for powerful batch operations-i flag or --dry-run in scripts for safetyMoving 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.
find . -maxdepth 1 -type f -exec mv {} /target/ \;-i (interactive) or -n (no-clobber) options.mv file.txt /newpath/ && echo "Moved at $(date)" >> move.log