ShareYourGroupShareYourGroupCommands

Linux and Mac commands

cp and mv: copy, move and rename

cp copies and mv moves. mv is also how files are renamed: there is no separate rename command for a single file. Both work quietly, and both will overwrite an existing file without asking unless told otherwise.

Copying a file

Linux or Mac terminal
asha@pc:~/demo$ cp notes.txt notes-backup.txt
asha@pc:~/demo$ ls
log.txt
marks.csv
notes-backup.txt
notes.txt
old
photos
report.txt

Source first, destination second. If the destination is a folder, the copy goes inside it and keeps its name.

Linux or Mac terminal
asha@pc:~/demo$ cp notes.txt old
asha@pc:~/demo$ ls old
draft.txt
notes.txt

Several files can be copied into a folder at once. The last name is the destination.

Linux or Mac terminal
asha@pc:~/demo$ mkdir backup
asha@pc:~/demo$ cp notes.txt report.txt marks.csv backup
asha@pc:~/demo$ cp *.txt backup
asha@pc:~/demo$ ls backup
log.txt
marks.csv
notes-backup.txt
notes.txt
report.txt

Copying a folder

cp refuses to copy a folder unless you add -r, for recursive, meaning "and everything inside".

Linux or Mac terminal
asha@pc:~/demo$ cp photos photos-copy
cp: -r not specified; omitting directory 'photos'
asha@pc:~/demo$ cp -r photos photos-copy
asha@pc:~/demo$ ls photos-copy
beach.jpg
hills.jpg

Moving

Linux or Mac terminal
asha@pc:~/demo$ mv report.txt old
asha@pc:~/demo$ ls old
draft.txt
notes.txt
report.txt

The file is gone from where it was. Moving within one disk is instant, however large the file, because only its entry is moved.

Renaming

Renaming is moving to a new name in the same place.

Linux or Mac terminal
asha@pc:~/demo$ mv notes.txt todo.txt
asha@pc:~/demo$ ls
backup
log.txt
marks.csv
notes-backup.txt
old
photos
photos-copy
todo.txt

It works on folders too.

Linux or Mac terminal
asha@pc:~/demo$ mv old archive
asha@pc:~/demo$ ls -F
archive/
backup/
log.txt
marks.csv
notes-backup.txt
photos/
photos-copy/
todo.txt

The danger: silent overwriting

If the destination file exists, both commands replace it. No question, no warning, no Recycle Bin.

Danger. cp new.txt report.txt destroys the old report.txt if there is one. mv does the same. The previous contents cannot be brought back.

Three options protect you.

OptionEffect
-iinteractive: ask before overwriting
-nno clobber: never overwrite, skip silently
-vverbose: say what is being done
Linux or Mac terminal
asha@pc:~/demo$ cp -v notes-backup.txt todo.txt
'notes-backup.txt' -> 'todo.txt'
asha@pc:~/demo$ cp -n marks.csv todo.txt
asha@pc:~/demo$ cat todo.txt
Buy milk
Call the plumber
Pay the electricity bill

The second command did nothing, because todo.txt exists and -n forbids replacing it.

Many people make -i the default by adding alias cp='cp -i' and alias mv='mv -i' to their shell's start-up file. Some Linux systems do this for the administrator account already.

Keeping dates and permissions

A plain copy is a new file with today's date. -p preserves the original's date, time and permissions. -a, for archive, does that and copies folders fully, and is the option to use for a faithful copy of a folder: cp -a project project-backup.

Checking a pattern first

Before a wildcard copy or move, look at what the pattern matches.

Linux or Mac terminal
asha@pc:~/demo$ ls *.csv
marks.csv

echo shows how the shell will expand it: echo mv *.txt archive/ prints the full command without running it.

Renaming many files

mv renames one thing at a time. To change many names, use a small loop:

Linux or Mac terminal
asha@pc:~/demo$ for f in *.txt; do mv "$f" "${f%.txt}.bak"; done; ls
archive
backup
log.bak
marks.csv
notes-backup.bak
photos
photos-copy
todo.bak

That renames every .txt file to .bak. The quotation marks keep names with spaces intact. Test such a loop with echo in front of mv first.

Copying to another computer

cp works on disks attached to this computer. For copying over a network, the tools are scp and rsync. rsync -av source/ destination/ is also excellent for local backups, since it copies only what has changed.

Danger. rsync --delete removes from the destination anything not in the source. With the two paths the wrong way round, it empties the folder you meant to protect. Add -n for a dry run, which only reports what would happen.

Common mistakes

The Windows equivalents are copy, move and ren.

Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.

This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.