StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Linux Command: mv
Download
  1. Linux
Linux Command: cp : Linux Command: rm
Linux

What mv does

  • Move a file/dir to a new location or

  • Rename it (same location, new name).

  • Overwrites without asking (unless you use safety flags).

mv SOURCE DEST                 # rename or move single item

mv SRC1 SRC2 ... DEST_DIR/     # move many items into a directory

mv project/ ~/backup/          # move a whole directory (no -r needed)


1) Everyday examples

mv a.txt notes.txt                             # rename a file

mv *.pdf ~/Documents/                          # move many files

mv project ~/backup/                           # move a directory

mv img_*.jpg ~/Pictures/                       # wildcards work

mv "My File.txt" "My Renamed File.txt"         # names with spaces (quote!)


2) Safety & control flags (GNU mv on Linux)

  • -i – interactive: ask before overwrite (good while learning)
    mv -i a.txt dest/

  • -n – no-clobber: never overwrite existing files
    mv -n a.txt dest/

  • -u – update: move only if source is newer or dest is missing
    mv -u report.docx dest/

  • -v – verbose: show what’s happening
    mv -v *.csv data/

  • -f – force: don’t prompt, even if dest is write-protected

  • --backup[=numbered] – keep the old dest as a backup
    mv --backup=numbered a.txt dest/a.txt # creates a.txt.~1~, ~2~, ...

  • -t DIR – list target dir first, then sources (great in scripts)
    mv -t dest/ a.txt b.txt c.txt

  • -T – treat DEST as a normal file, not a directory
    (avoids ambiguity; errors if DEST is a dir)
    mv -T src_dir new_name

Student-safe default: use mv -iv (ask + show).


3) Rename vs move (what’s really happening)

  • Same filesystem → fast, atomic rename (just updates directory entries).
    Good for safe updates: write
    file.tmp then mv file.tmp file to replace instantly.

  • Different filesystems/partitions → behind the scenes it copies then deletes.
    Slower; inode changes; permissions/SELinux context may be re-applied.

Check where things live:

df -h source_path dest_path


4) Directories (no -r needed)

  • mv dir1 dir2/ → moves dir1 into dir2 if dir2 exists.

  • mv dir1 dir2 → if dir2 doesn’t exist, this renames dir1 → dir2.

  • Want to rename even if a dir named dir2/ exists? Remove/rename it first, or use -T to force treating dir2 as a file path (will error if it’s an existing dir).


5) Quoting, weird names, and hidden files

  • Always quote paths with spaces or special chars:

·         mv "My Project (v1)/" "My Project (final)/"

  • Filenames that start with - (dash)? End options with --:

·         mv -- -weird.txt normal.txt

  • Globs like * don’t match dotfiles by default; move hidden files explicitly:

·         mv .env .gitignore config/   # specify them


6) Useful patterns you’ll actually use

# keep older dest files safe

mv -ib a.txt dest/              # ask; if overwrite, make a backup

 

# move with structure:

mkdir -p dst && mv -t dst src/*.py src/*.md

 

# only move newer versions (sync-ish)

mv -uv out/*.csv ~/data/

 

# disambiguate DEST:

mv -T build build_old           # rename, not “into a dir named build_old/”


7) Common pitfalls (and quick fixes)

  • Accidental overwrite → use -i or -n, or --backup.

  • “No such file or directory” → parent folder missing; create it: mkdir -p dest/

  • Cross-filesystem is slow → that’s normal (copy+delete). For huge moves, consider:

·         rsync -a --remove-source-files SRC/ DEST/

  • Case-only rename (e.g., Readme → README):
    Works on Linux filesystems. On case-insensitive setups (WSL + Windows dir), do:

·         mv Readme tmp && mv tmp README


8) Scripting tips (robust mv)

#!/usr/bin/env bash

set -Eeuo pipefail

 

src=$1

dst_dir=$2

 

mkdir -p "$dst_dir"

if mv -iv -- "$src" "$dst_dir/"; then

  echo "Moved $src -> $dst_dir/"

else

  echo "Move failed" >&2

  exit 1

fi


9) Mini-lab (10–15 min)

mkdir -p ~/lab/mv/{src,dst} && cd ~/lab/mv/src

printf "A\n" > a.txt

printf "B\n" > b.txt

mkdir pics && printf "IMG" > pics/p1.jpg

 

# 1) rename

mv a.txt a_old.txt && ls -l

 

# 2) move files into dir

mv -v a_old.txt b.txt ../dst/ && ls ../dst

 

# 3) move a directory

mv -v pics ../dst/

 

# 4) safe overwrite

printf "B2\n" > ../dst/b.txt

mv -i b.txt ../dst/b.txt       # answer y/n and see behavior

 

# 5) backup on overwrite

printf "A2\n" > a.txt

mv --backup=numbered a.txt ../dst/a.txt

ls ../dst | grep a.txt


Exam-ready bullets

  • Syntax: mv SRC DST (rename/move one), mv SRC1 SRC2 … DIR/ (move many into DIR).

  • Overwrites by default → use -i (ask) or -n (no overwrite) or --backup.

  • Directories: mv handles them without -r.

  • Same FS = atomic rename; different FS = copy+delete.

  • Quote paths with spaces; use -- for dash-started names; -t for target-first in scripts; -T to treat dest as a plain path.

Want a one-page printable with cp vs mv side-by-side? I can format it.

 

Linux Command: cp Linux Command: rm
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website