This chapter describes the commands for basic file manipulation: copying, moving (renaming), and deleting (removing).
cp
: Copy files and directories
cp
copies files (or, optionally, directories). The copy is
completely independent of the original. You can either copy one file to
another, or copy arbitrarily many files to a destination directory.
Synopsis:
cp [option]... source dest cp [option]... source... directory
If the last argument names an existing directory, cp
copies each
source file into that directory (retaining the same name).
Otherwise, if only two files are given, it copies the first onto the
second. It is an error if the last argument is not a directory and more
than two non-option arguments are given.
If the source file contains holes, cp
copies them and other
blocks of zero bytes as holes. Otherwise, files are written just as
they are read. (A hole is a sequence of zero bytes that does not
occupy any physical disk blocks; the `read' system call reads these
as zeroes.)
By default, cp
does not copy directories. It also refuses to
copy a file onto itself.
The program accepts the following options. Also see section Common options.
cp
must be the name of an existing directory.
For example, the command:
cp --parents a/b/c existing_dircopies the file `a/b/c' to `existing_dir/a/b/c', creating any missing intermediate directories.
cp
may well hang indefinitely reading a FIFO, unless something else happens
to be writing it.
dd
: Convert and copy a file
dd
copies a file (from standard input to standard output, by
default) with a changeable I/O blocksize, while optionally performing
conversions on it. Synopsis:
dd [option]...
The program accepts the following options. Also see section Common options.
The numeric-valued options below (bytes and blocks) can be followed by a multiplier: `b'=512, `c'=1, `k'=1024, `w'=2, `xm'=m.
dd
truncates file to zero
bytes (or the size specified with `seek=').
dd
, unlike others, works
when an odd number of bytes are read--the last byte is simply copied
(since there is nothing to swap it with).
install
: Copy files and set attributes
install
copies files while setting their permission modes and, if
possible, their owner and group. Synopses:
install [option]... source dest install [option]... source... directory install -d [option]... directory...
In the first of these, the source file is copied to the dest target file. In the second, each of the source files are copied to the destination directory. In the last, each directory (and any missing parent directories) is created.
install
is similar to cp
, but allows you to control the
attributes of destination files. It is typically used in Makefiles to
copy programs into their destination directories. It refuses to copy
files onto themselves.
The program accepts the following options. Also see section Common options.
install
.
install
, which
gives directories that it creates the default attributes.)
chmod
, with 0 as the point of departure (see section File permissions). The default mode is 0755--read, write, and execute
for the owner, and read and execute for group and other.
install
has appropriate privileges (is run as root), set the
ownership of installed files or directories to owner. The default
is root
. owner may be either a user name or a numeric user
ID.
mv
: Move (rename) files
mv
moves or renames files (or directories). Synopsis:
mv [option]... source dest mv [option]... source... directory
If the last argument names an existing directory, mv
moves each
other given file into a file with the same name in that directory.
Otherwise, if only two files are given, it renames the first as
the second. It is an error if the last argument is not a directory
and more than two files are given.
mv
can move only regular files across filesystems.
If a destination file exists but is normally unwritable, standard input
is a terminal, and the `-f' or `--force' option is not given,
mv
prompts the user for whether to replace the file. (You might
own the file, or have write permission on its directory.) If the
response does not begin with `y' or `Y', the file is skipped.
The program accepts the following options. Also see section Common options.
rm
: Remove files or directories
rm
removes each given file. By default, it does not remove
directories. Synopsis:
rm [option]... [file]...
If a file is unwritable, standard input is a terminal, and the `-f'
or `--force' option is not given, or the `-i' or
`--interactive' option is given, rm
prompts the user
for whether to remove the file. If the response does not begin with
`y' or `Y', the file is skipped.
The program accepts the following options. Also see section Common options.
unlink
instead of rmdir
, and don't
require a directory to be empty before trying to unlink it. Only works
if you have appropriate privileges. Because unlinking a directory
causes any files in the deleted directory to become unreferenced, it is
wise to fsck
the filesystem after doing this.
One common question is how to remove files whose names being with a
`-'. GNU rm
, like every program that uses the getopt
function to parse its arguments, lets you use the `--' option to
indicate that all following arguments are non-options. To remove a file
called `-f' in the current directory, you could type either:
rm -- -f
or:
rm ./-f
The Unix rm
program's use of a single `-' for this purpose
predates the development of the getopt standard syntax.