Copying and moving files is part of the everyday activity of a Linux sysadmin, so How Hard Can It Be™?
As with many things, when we look under the covers a little there are some gotchas and corresponding hints and tips that can help us.
Largely the philosophy of Linux (and Unix before it) is “do what I say”. If you tell Linux to delete a file then, so long as you have the appropriate permissions, it will. By default, there’s no “are you sure?”. Linux treats us as adults. That’s fine, so long as “do what I say” is the same as “do what I mean”.
Gotchas With Copying and Moving Files
By default, the cp
and mv
commands will overwrite any existing destination files. The correct terminology for this behaviour is clobbering. Here’s an example: we start with two files, imaginatively called bigfile
and smallfile
. First, we copy smallfile
to outfile
, and we can verify that outfile
is the same size as smallfile
. Then we copy bigfile
to outfile
, and we can confirm that this second copy has clobbered outfile
. Note that there are no warnings: Linux just does what we ask:
$ ls -l total 5128 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $ cp smallfile outfile $ ls -l total 5128 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 2 May 25 11:13 outfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $ cp bigfile outfile $ ls -l total 10244 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 5242880 May 25 11:13 outfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $
That may be exactly what we want, but if we can overwrite existing files without confirmation or warning, one day we’ll get it wrong.
mv and cp Safety
There are two safety mechanisms we can use. Both mv
and cp
can take a -i
or a -n
option:
-n
is “no-clobber”, which makesmv
andcp
silently refuse to overwrite an existing output file-i
is “interactive”, which results inmv
orcp
prompting before overwriting an existing output file
Here they are in action. First, -n
: note how the second cp
command does not overwrite the output file, although neither does it tell us that it has failed to overwrite the output file:
$ ls -l total 5124 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $ cp smallfile outfile $ ls -l total 5128 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 2 May 25 11:25 outfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $ cp -n bigfile outfile $ ls -l total 5128 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 2 May 25 11:25 outfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $
This time I’m using -i
to prompt before overwriting. Note that the first copy does not prompt because, at that stage, outfile
does not exist:
$ ls -l total 5124 -rw-r--r-- 1 kae kae 5242880 May 25 11:12 bigfile -rw-r--r-- 1 kae kae 2 May 25 11:12 smallfile $ cp -i smallfile outfile $ cp -i bigfile outfile cp: overwrite 'outfile'? n $
In Practice
It’s rare that we intend to overwrite output files, although there are times when we do, of course. I find the -i
option more useful as it provides some feedback if I do try to overwrite files. The following in ~/.bash_profile
may be helpful:
alias cp="cp -i" alias mv="mv -i"
They mostly don’t interfere at all, but do warn me when I’m about to clobber a file. And if I really want to ignore my alias, quoting the cp
command works:
$ cp bigfile smallfile cp: overwrite 'smallfile'? n $ 'cp' bigfile smallfile $
Was This Linux Tip Helpful?
Let us know in the comments below.