TExcelTAD
Home › Linux & Bash › cp & mv: Copy and Move

cp & mv: Copy and Move

cp (copy) duplicates files and directories. mv (move) moves them to another place, and since a rename is just a move inside the same directory, it also renames them.

Copying files

cp SOURCE DEST copies one file. If DEST is an existing directory, the copy goes inside it with the same name:

bash · try it yourself
printf 'debug=false\n' > app.conf
cp app.conf app.conf.bak
mkdir backup
cp app.conf backup/
cp app.conf app.conf.bak backup/
ls . backup
.:
app.conf
app.conf.bak
backup

backup:
app.conf
app.conf.bak

With more than two arguments the last one must be a directory, and all the others are copied into it.

Copying directories

Directories need -r (recursive), just like with rm:

bash · try it yourself
mkdir -p site/css && touch site/index.html site/css/main.css
cp site site-copy
cp -r site site-copy
find site-copy | sort
cp: -r not specified; omitting directory 'site'
site-copy
site-copy/css
site-copy/css/main.css
site-copy/index.html

Note: A trailing slash matters with some tools (rsync), but not much with cp. What does matter: if the destination already exists as a directory, cp -r site dest creates dest/site; if not, it creates dest as the copy.

Useful cp options

Option Effect
-r Copy directories recursively
-i Ask before overwriting
-n Never overwrite an existing file
-u Copy only when the source is newer than the destination (or the destination is missing)
-v Report each copy
-p Preserve mode, ownership and timestamps
-a Archive: recursive + preserve everything (best for backups)
bash · try it yourself
echo "v1" > notes.txt
cp notes.txt saved.txt
echo "v2" > notes.txt
cp -n notes.txt saved.txt
cat saved.txt
cp -v notes.txt saved.txt
cat saved.txt
v1
'notes.txt' -> 'saved.txt'
v2

Without -n or -i, cp silently overwrites the destination, just like > does.

Moving and renaming

mv takes the same forms as cp but removes the original. It doesn't need -r for directories:

bash · try it yourself
touch report-draft.txt
mv report-draft.txt report.txt
mkdir -p archive inbox/photos
mv report.txt archive/
mv inbox/photos archive/
find archive inbox | sort
archive
archive/photos
archive/report.txt
inbox

Inside one filesystem mv is instant even for huge directories, because only the directory entry changes. Across filesystems (to a USB drive, say) it copies and then deletes.

mv overwrites existing files too. Use -i to be asked, -n to never overwrite, or -b to keep a backup of the file that would be replaced:

bash · try it yourself
echo "old" > data.csv
echo "new" > upload.csv
mv -b upload.csv data.csv
ls
cat data.csv data.csv~
data.csv
data.csv~
new
old

Renaming many files

mv renames one file at a time. To rename many, combine it with a loop and parameter expansion (covered in the strings lesson):

bash · try it yourself
touch img1.jpeg img2.jpeg img3.jpeg
for f in *.jpeg; do
  mv -- "$f" "${f%.jpeg}.jpg"
done
ls
img1.jpg
img2.jpg
img3.jpg

${f%.jpeg} removes .jpeg from the end of the name. Many distributions also ship a rename tool, but its syntax differs between systems.

Copying vs linking

Command Result
cp a b An independent copy: editing b doesn't change a
ln -s a b b is a symbolic link (shortcut) pointing to a
ln a b A hard link: two names for the same data
bash · try it yourself
echo "original" > config.yml
cp config.yml copy.yml
ln -s config.yml link.yml
echo "changed" > config.yml
cat copy.yml link.yml
readlink link.yml
original
changed
config.yml

Test yourself

How do you rename old.txt to new.txt?

  1. rename old.txt new.txt
  2. cp -r old.txt new.txt
  3. mv old.txt new.txt
  4. ln old.txt new.txt

Which option is needed to copy a directory with cp?

  1. -d
  2. -r
  3. -f
  4. -v

What does cp -n a.txt b.txt do if b.txt already exists?

  1. Overwrites it
  2. Asks for confirmation
  3. Leaves it unchanged
  4. Appends to it