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:
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:
mkdir -p site/css && touch site/index.html site/css/main.css
cp site site-copy
cp -r site site-copy
find site-copy | sortcp: -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 withcp. What does matter: if the destination already exists as a directory,cp -r site destcreatesdest/site; if not, it createsdestas 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) |
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.txtv1
'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:
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 | sortarchive
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:
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):
touch img1.jpeg img2.jpeg img3.jpeg
for f in *.jpeg; do
mv -- "$f" "${f%.jpeg}.jpg"
done
lsimg1.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 |
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.ymloriginal
changed
config.yml
Test yourself
How do you rename old.txt to new.txt?
- rename old.txt new.txt
- cp -r old.txt new.txt
- mv old.txt new.txt
- ln old.txt new.txt
Which option is needed to copy a directory with cp?
- -d
- -r
- -f
- -v
What does cp -n a.txt b.txt do if b.txt already exists?
- Overwrites it
- Asks for confirmation
- Leaves it unchanged
- Appends to it