Where to start First steps track · 15 stages
Linux Labs

mv — move or rename

5 min Ubuntu 24.04 · GNU coreutils 9.4

The mv command renames a file or directory and moves it elsewhere; it silently replaces an existing target.

Cheat sheet

9 commands Printable version
  • mv old.txt new.txt rename
  • mv report.txt ~/archive/ move into a directory, same name
  • mv a.txt b.txt docs/ several files into a directory
  • mv project ~/old-project a directory — no options, whole
  • mv -i new.conf app.conf ask before overwriting
  • mv -n new.conf app.conf never overwrite an existing file
  • mv -v *.log /tmp/logs/ print each move
  • mv -b new.conf app.conf the old one becomes app.conf~
  • mv -- -file.txt file.txt a name that starts with a dash

Overview

mv (move) does two jobs with one command. If the destination is an existing directory, the file moves into it. Otherwise the file gets a new name.

text
student@ubuntu:~$ mv -v draft.txt notes/
renamed 'draft.txt' -> 'notes/draft.txt'
student@ubuntu:~$ mv -v notes/draft.txt notes/final.txt
renamed 'notes/draft.txt' -> 'notes/final.txt'

Within one filesystem mv doesn't copy data: only the directory entry changes, so even a large directory "moves" instantly. Across filesystems mv copies and then deletes the source, which takes longer.

mv moves directories without -r. It silently overwrites an existing destination file.

Options

7

The safety options are the same as in cp: -i, -n, -b. Full list — man mv.

Option What it does Example
-i Prompt before overwriting an existing file. mv -i new.conf app.conf
-n Never overwrite existing files. mv -n new.conf app.conf
-v Print each action — renamed 'a' -> 'b'. mv -v *.log /tmp/logs/
-f Don't prompt — overrides -i, including one from an alias. mv -f new.conf app.conf
-b Before overwriting, keep the old file with ~ appended to its name. mv -b new.conf app.conf
-u Move only when the source is newer than the destination or it's missing. mv -u *.conf /srv/conf/
-t dir Destination directory as the first argument — handy with find and xargs. mv -t /tmp/logs/ a.log b.log

Nothing found. Try a shorter query.

Recipes

4

Rename a file in another directory without going there

bash
mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.off
mv /var/log/app/{app.log,app.log.old}    # the same with shell {}

Move all files matching a pattern

bash
mkdir -p ~/archive
mv -v *.log ~/archive/

The shell expands *.log (see globbing). Check what it matches first: ls *.log.

Rename many files: .txt → .md

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

${f%.txt} cuts off the extension; the quotes keep names with spaces intact.

Replace a live file safely

bash
cp -a app.conf app.conf.new   # edit a copy
nano app.conf.new
mv app.conf.new app.conf      # swap in one step

Within one filesystem mv replaces the file atomically: a program sees either the old file or the new one, never half of it.

Troubleshooting

5

mv: cannot stat 'x': No such file or directory

There's no source at that path. Check pwd and the name with ls; mind the case.

mv: cannot move 'src' to 'dst/src': Directory not empty

dst already holds a non-empty directory with the same name. mv doesn't merge directories. Move the contents (mv src/* dst/src/) or pick another name.

Permission denied

mv needs write permission on both directories — source and destination; the file's own permissions don't matter. Check ls -ld on both; system directories need sudo mv.

The file is gone after mv

The destination was not a directory but a file with that name, and mv overwrote it. Or the destination directory didn't exist, and the file was simply renamed to that name. Check ls -l at the destination path.

mv: invalid option -- 'l' with the name -file.txt

mv reads a name starting with a dash as a bundle of options -f -i -l … and trips on the first unknown letter. Put -- before the names or give a path: mv ./-file.txt file.txt.

Danger zone

2

Overwriting without asking

mv new.conf app.conf replaces app.conf if it exists, and the old contents are gone. Until you're sure, use mv -i or mv -n.

A typo in the destination directory

If /tmp/lgos doesn't exist, mv *.log /tmp/lgos renames a single file to lgos, and with several files it refuses to run. End directory paths with / (/tmp/logs/): then mv checks that the directory exists.

See also

  • cp — copy and leave the source in place.
  • rm — delete a file or directory.
  • ls — check the result.
  • globbing — *, ?, {} patterns for many files.
  • files-basics — an overview of the basic file commands.
Loading…