Copying, Moving, and Renaming Files
Two commands handle nearly all file reorganization in Bash: cp copies items, and mv moves them. Renaming has no command of its own, because a rename is simply a move to a new name in the same directory. This lesson explains the rules both commands follow, how they treat existing files, and how to protect yourself from overwriting something by accident.
Understanding the Concept
Both commands take one or more sources followed by a destination:
cp SOURCE... DESTINATION
mv SOURCE... DESTINATION
What the destination means depends on what it is:
| Destination | Result |
|---|---|
| An existing directory | Each source is placed inside that directory, keeping its name |
| A name that does not exist | The single source is created (or renamed) under that new name |
| An existing file | The file is replaced by the source, silently unless you ask otherwise |
When you give more than one source, the destination must be a directory. Keeping this table in mind prevents most surprises.
The difference between the two commands is what happens to the original. cp leaves it in place and creates a second copy with its own data. mv leaves only one item, now at its new location or under its new name.
Copying Files with cp
Create a file and copy it:
printf 'hello\n' > a.txt
cp a.txt b.txt
ls
cat b.txt
Output:
a.txt b.txt
hello
b.txt is a separate file with the same content. Editing one does not change the other.
Copying Into a Directory
If the last argument is a directory, the sources are copied into it:
cp -v a.txt c.txt
mkdir backup
cp a.txt b.txt c.txt backup/
ls backup
Output:
'a.txt' -> 'c.txt'
a.txt b.txt c.txt
The first command used -v (verbose) to show each copy it made. The trailing slash in backup/ is optional, but it adds a safety check: if backup did not exist, the slash would turn the command into an error instead of creating a file named backup:
cp a.txt nodir/
Output:
cp: cannot create regular file 'nodir/': Not a directory
Overwriting Existing Files
By default cp replaces an existing destination without asking:
printf 'new\n' > a.txt
cp a.txt b.txt
cat b.txt
Output:
new
The earlier content of b.txt is gone. Three options control this:
| Option | Behavior |
|---|---|
-i | Interactive: ask before overwriting each existing file |
-n | No clobber: never overwrite; skip files that already exist |
-u | Update: copy only when the source is newer than the destination |
With -i, the command stops and waits for an answer:
cp -i a.txt b.txt
Interactive session:
cp: overwrite 'b.txt'? n
Typing y and pressing Enter replaces the file; anything else leaves it alone. Newer releases of GNU coreutils (9.3 and later) also accept --update=none as the clearer way to say "never overwrite", and the -n option prints a warning on coreutils 9.4 recommending it. Both forms keep the existing file:
echo A > ua
echo B > ub
cp --update=none ua ub
cat ub
Output:
B
Copying Directories
cp refuses to copy a directory unless you add -r (recursive), which copies the directory and everything inside it:
mkdir -p proj/src proj/docs
echo x > proj/src/main.c
echo y > proj/docs/readme.txt
cp proj proj2
Output:
cp: -r not specified; omitting directory 'proj'
cp -r proj proj2
find proj2 | sort
Output:
proj2
proj2/docs
proj2/docs/readme.txt
proj2/src
proj2/src/main.c
The destination name decides the result, just as in the table above. Because proj2 did not exist, it became a copy of proj. If the destination is an existing directory, the copy goes inside it:
mkdir dest
cp -r proj dest
find dest | sort
Output:
dest
dest/proj
dest/proj/docs
dest/proj/docs/readme.txt
dest/proj/src
dest/proj/src/main.c
Preserving Attributes
A plain copy gets a fresh modification time and the default permissions. The option -p keeps the original time, owner and permissions where possible, and -a (archive) preserves nearly everything and implies -r:
touch -d "2026-01-01 10:00" old.txt
cp old.txt copy1.txt
cp -p old.txt copy2.txt
ls -l --time-style=long-iso old.txt copy1.txt copy2.txt
Output (the date of copy1.txt is the day you run the command):
-rw-r--r-- 1 student student 0 2026-11-20 10:52 copy1.txt
-rw-r--r-- 1 student student 0 2026-01-01 10:00 copy2.txt
-rw-r--r-- 1 student student 0 2026-01-01 10:00 old.txt
Use cp -a when making backups of directory trees, so that times and permissions survive.
Moving and Renaming with mv
mv changes where an item lives or what it is called. Inside the same file system, it does not copy any data. It only updates the directory entry, so moving even a very large file is instant.
Renaming a File
Renaming means moving to a new name in the same directory:
mv a.txt renamed.txt
ls a.txt renamed.txt
Output:
ls: cannot access 'a.txt': No such file or directory
renamed.txt
The ls error confirms that a.txt no longer exists. Only renamed.txt does.
Moving Into a Directory
mv renamed.txt backup/
ls backup
Output:
a.txt b.txt c.txt renamed.txt
You can move and rename in one step by giving a full destination path:
mv backup/renamed.txt backup/final.txt
mv backup/final.txt .
ls backup
Output:
a.txt b.txt c.txt
The first command renamed the file inside backup. The second moved it to the current directory, which is written .. The listing shows that final.txt has left backup.
Moving Several Items
With several sources, the last argument must be a directory:
mv b.txt c.txt dest/
ls dest
Output:
b.txt c.txt proj
Moving and Renaming Directories
Unlike cp, mv needs no -r. A directory is moved or renamed with the same syntax as a file:
mv proj proj_moved
ls -d proj*
Output:
proj2 proj_moved
If the destination is an existing directory, the source goes inside it:
mkdir d1 d2
touch d1/f
mv d1 d2
find d2
Output:
d2
d2/d1
d2/d1/f
Protecting Existing Files
mv also overwrites silently. It supports the same safety options as cp:
echo one > x1
echo two > x2
mv -n x1 x2
cat x2
Output:
mv: not replacing 'x2'
two
With -n, nothing moved, and a message explains why. The option -i asks first, and --backup=numbered keeps the old file under a numbered name before replacing it:
echo hi > dup
echo ho > dest/dup
mv --backup=numbered dup dest/
ls dest | grep dup
Output:
dup
dup.~1~
The file dest/dup is now the new one, and the previous version is saved as dup.~1~.
Reporting Errors
Both commands print a message and return a non-zero status when a source is missing:
mv nosuch.txt dest/
echo "status=$?"
Output:
mv: cannot stat 'nosuch.txt': No such file or directory
status=1
Practical Applications
Renaming Many Files at Once
mv renames one item per call, but a loop can call it repeatedly. This loop changes every .jpeg extension to .jpg:
touch img1.jpeg img2.jpeg img3.jpeg
for f in *.jpeg; do mv -- "$f" "${f%.jpeg}.jpg"; done
ls img*
Output:
img1.jpg img2.jpg img3.jpg
The expression ${f%.jpeg} removes the shortest match of .jpeg from the end of the value of f, and .jpg is then appended. The quotes around "$f" keep names with spaces together, and -- marks the end of options so that a name beginning with a dash is not mistaken for one.
Files With Spaces in Their Names
Quote the names, as with every command:
touch "my file.txt"
mv "my file.txt" "your file.txt"
ls "your file.txt"
Output:
your file.txt
Without quotes, mv my file.txt would receive the two names my and file.txt, and print mv: cannot stat 'my': No such file or directory.