Log inSign up

Moving and renaming files and directories with mv

Linux has no separate rename command for single files: mv old-name new-name renames, and mv file /some/folder/ moves. The same command works on directories without any extra flag. Add -i to be asked before an existing file is replaced, or -n to never replace one, and use a short shell loop or the rename tool when many files need new names.

Topic: Linux command line · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does mv actually do?

mv changes the name or location of a file without copying its data, as long as source and target sit on the same filesystem. In that case it only rewrites a directory entry, so moving a 40 GB database dump inside one disk takes a fraction of a second. When the target is on a different filesystem, mv copies the data and then deletes the source, which takes as long as a copy and needs free space on the target.

mv --version | head -n 1
mv draft.txt final.txt
mv final.txt /home/deploy/documents/
mv a.txt b.txt c.txt /home/deploy/documents/

Like cp, mv belongs to GNU coreutils and is present on every Linux image in the console. The last argument is the destination; if it is an existing directory, the sources go inside it.

How do I rename a directory?

Exactly the same way as a file: mv old-dir new-dir. No recursive flag is needed because the directory entry is renamed and its contents follow automatically.

mv /var/www/shop /var/www/shop-old
mv releases/2026-11-30 releases/current

Watch for one trap. If new-dir already exists, mv does not rename; it moves old-dir into it, giving new-dir/old-dir. To make the intent explicit, add -T, which treats the destination as a plain name and fails instead of nesting:

mv -T build release

If release is an existing non-empty directory, you get Directory not empty rather than a surprise subfolder. Services that read from a renamed path, such as an Nginx root or a systemd unit with WorkingDirectory=, need their config updated and a reload afterwards.

How can I avoid replacing files that already exist?

mv overwrites without asking by default, so pick a guard flag when the destination might contain a file with the same name.

  • -i: prompt before every overwrite; answer y or n.
  • -n: never overwrite, skip quietly.
  • -u: move only when the source is newer than the target.
  • -b or --backup=numbered: keep the old target under a backup name.
  • -v: print what happened to each file.
mv -iv upload/*.png /srv/media/
mv --backup=numbered index.html /var/www/site/

An alias such as alias mv='mv -i' in ~/.bashrc gives you the prompt by default in interactive shells. Scripts do not read that alias, so write the flag into scripts explicitly. More on aliases is in the bash aliases guide.

How do I rename many files at once?

A shell loop with parameter expansion works on every distribution and needs nothing extra. This example changes the extension of all .log files to .txt:

for f in *.log; do
  mv -n -- "$f" "${f%.log}.txt"
done

${f%.log} strips the suffix, the quotes protect names with spaces, and -- stops names beginning with a dash from being read as options. Run the loop once with echo mv in place of mv to preview the result.

For pattern based renames there is also a rename command, but its syntax differs by family. On Ubuntu and Debian install the Perl version, which takes a substitution expression:

sudo apt install rename
rename -n 's/ /_/g' *.jpg
rename 's/ /_/g' *.jpg

The -n run only shows what would change. On Rocky and Alma the rename from util-linux is already installed and takes plain text: rename .jpeg .jpg *.jpeg replaces the first match in each name.

How do I move only some files, or a list of files?

Combine mv with a glob, with find, or with -t, which names the target directory first so that the sources can be appended by another command.

mv *.tar.gz /srv/backups/
find /var/tmp/uploads -type f -mtime +30 -exec mv -t /srv/old-uploads/ {} +
mv -t /srv/archive/ $(cat to-archive.txt)

The find line moves files untouched for more than 30 days. The last form breaks on names with spaces, so prefer xargs -d '\n' mv -t /srv/archive/ < to-archive.txt for lists you do not control. The find guide and the xargs guide go deeper into both tools.

Is it safe to move files a running service is using?

Renaming a file a process has open is safe for that process: it keeps writing to the same inode under the new name. That is why log rotation works, and also why a moved log can keep growing after you expected it to stop. Restart or reload the service so it reopens its files at the configured path.

sudo mv /var/log/app/app.log /var/log/app/app.log.1
sudo systemctl reload app
ls -l /var/log/app/

For live websites, a common pattern is to build a new release in its own folder and switch a symbolic link, which is atomic, instead of moving the web root while requests arrive. The symlinks guide shows that pattern. Moving system files such as anything under /usr or /etc/systemd without understanding the effect can stop the server from booting cleanly, so make a copy first.

How do I check what mv did?

Use -v during the move and list both locations afterwards. If you moved across disks, compare sizes too.

mv -v data.db /srv/db/
ls -lh /srv/db/data.db
test -e data.db || echo "source gone, move finished"

When a move between filesystems is interrupted, the target may hold a partial file while the source still exists. Delete the partial target and repeat the move, or use rsync -a --remove-source-files, which deletes each source file only after it has been transferred in full.

What do common mv errors mean?

  • Permission denied: you need write access to both the source directory and the target directory, not to the file itself. Use sudo for system paths.
  • cannot move to a subdirectory of itself: you tried mv dir dir/sub. Pick a target outside the tree.
  • Device or resource busy: the path is a mount point. Unmount it or move what is inside instead.
  • No space left on device: a cross-disk move needs room on the target. Check with df -h.
  • Directory not empty: with -T the destination already holds files; rename or clear it first.

Questions about Move and rename with mv

Is there a rename command for a single file?

You do not need one: mv old new is the standard way. The separate rename tool is meant for changing many names by pattern.

Does mv keep permissions and timestamps?

Yes. On the same filesystem nothing about the file changes except its path. Across filesystems mv copies attributes as well, like cp -a.

How do I undo a move?

There is no undo. Move the file back with a second mv. When you are unsure, add -b so any overwritten target is kept as a backup.

Why did my folder end up inside another folder?

The destination already existed, so mv placed the source inside it. Use mv -T source target when you mean a rename.

Can mv move files between servers?

No. Copy them with rsync or scp over SSH, verify the result, then delete the source.