跳到主要内容

Copying and Moving Files

cp

cp is used to copy files/directories to another directory. To overwrite the target directory, use the -rf option. For more information about the available options, see cp command.

cp [options] source destination

Command options (partial):

OptionDescription
-aarchive files
-fforce copy by removing the destination file if needed
-iinteractive - ask before overwrite
-llink files instead of copy
-Lfollow symbolic links
-nno file overwrite
-Rrecursive copy (including hidden files)
-uupdate - copy when the source is newer than the destination
-vverbose - print informative messages

rm

rm is used to remove (delete) files. For more information about the available options, see rm command.

rm [options] <filename>
rm -rf <directory/filename> # force delete files/directories recursively

Command options (partial):

OptionDescription
-f, --forceIgnore nonexistent files, and never prompt before removing.
-iPrompt before every removal.
-r, -R, --recursiveRemove directories and their contents recursively.
-v, --verboseVerbose mode; explain at all times what is being done.

mv

mv is used to move files or directories to another location.

警告

If the target directory is not empty, this command won't work. In this case, use cp and rm as described in the section below.

mv [options] source destination

Command options:

0ptionDescription
-fforce move by overwriting destination file without prompt
-iinteractive prompt before overwrite
-uupdate - move when the source is newer than the destination
-vverbose - print source and destination files

Using rm and cp rather than mv

To move a file to a directory that is not empty (i.e., move files/directory and delete the original one), in which case mv won't work, use the cp command together with rm:

cp -r ./backup/* ./backupArchives && rm -R ./backup/*
  • The first command: copies the directory ./backup/* and the files contained to ./backupArchives.

  • The second command: deletes the original directory ./backup/*.