Linux copy, delete, move files command: CP, RM, MV

Source: Internet
Author: User
Tags file copy

The CP command copies the source file or directory to the destination file or directory. If the source file is a normal file, the command copies it to the specified destination file, and if it is a directory, you need to use the "-r" option to copy all files and subdirectories in the entire directory to the target location.

General format: CP [option] source file or directory destination file or directory

Options:

-A This option is usually used when replicating directories. It recursively copies all subdirectories and their files from the source directory to the destination directory, and retains the file link and file properties unchanged. It is equivalent to-DPR

-D preserves file links when copying.

-F overwrites the existing target file and does not give a hint.

Unlike the-f option, a prompt is given before the target file is overwritten, requiring the user to confirm it. Answer y to overwrite the target file. This is interactive replication.

-P Copies the contents of the source file, as well as its modification time and access rights to the new file.

-r,-r recursively replicates directories, copying all files in the source directory and their levels of subdirectories to the target location.

-L does not copy, but instead creates a linked file to the source file, which is given by the destination file.

Note: The CP command copies a file, and the original file remains unchanged!

If a file is copied to a target file and the target file already exists, the contents of the target file are corrupted. All parameters in this command can be either an absolute or a relative path name. Usually use the point (.) or dot (...) The form. For example, the following command copies the specified file to the current directory:

# CP.. /mary/homework/assign.

The directory specified by all target files must exist, and the CP command cannot create a directory. If there is no file copy permission, the system displays an error message.

Example:

Copy file files to the directory/usr/men/tmp and rename to File1

# CP File/usr/men/tmp/file1

Copy all files under the directory/usr/men and their subdirectories to the directory/usr/zh

# Cp-r/usr/men/usr/zh

Copy all the files and subdirectories under the directory/root/web/to the directory/www/web, and note that this code simply copies the files and does not copy the Web folders past

# cp-r/root/web/*/www/web

interactively copy all. c files in the directory/usr/men to the directory/usr/zh

#cp-I/usr/men M*.c/usr/zh

RM command, which deletes one or more files or directories in one directory, or deletes a directory and all of its subordinate files and subdirectories. For linked files, only the entire linked file is deleted, and the original file remains unchanged.

General format: RM [options] File list

Options:

-d deletes the directory, regardless of whether it is empty (only superuser can use it).

-F ignores files that do not exist and does not give prompt information.

-R recursively deletes the specified directory and its subordinate level subdirectories and corresponding files.

-I delete files interactively.

Note: Use RM commands with extra care. Because once you delete a file, you cannot recover it. So before you delete a file, it's a good idea to look at the contents of the file to see if you really want to delete it. The RM command can use the-I option, which is especially useful when using a file name extension to delete multiple files. Using this option, you will be asked to determine whether you want to delete it. At this point, you must enter Y and press ENTER to delete the file. If you press the ENTER key or other characters only, the file is not deleted.

Example:

Interactively delete files in the current directory test and example:

# Rm-i Test Example

Remove Test? n (do not delete file test)

Remove example y (delete file example)

Delete all files and subdirectories in the current directory except for hidden files:

# Rm-r *

It should be noted that it is very dangerous to do so!

The MV command is used to rename a file or directory, or to move a file from one directory to another. Source represents the origin file or directory, and target represents the destination file or directory. If you move a file to an already existing destination file, the contents of the destination file are overwritten.

The MV command can be used to move a source file to a destination file, or to move a group of files to a target directory. There are two different results of the source file being moved to the destination file:

1. If the destination file is a path to a directory file, the source file is moved to this directory and the filename is unchanged.

2. If the destination file is not a directory file, the source file name (only one) will change to the target file name and overwrite the existing file with the same name. If the source and destination files are in the same directory, the MV function is to change the file name. When the destination file is a directory file, the source file or directory parameter can have multiple, all source files are moved to the destination file. All files moved to this directory will retain their previous file names.

General format: MV [options] source target

Options:

-I interactive operation. If the source file has the same name as a file in the destination file or destination directory, ask the user whether to overwrite the destination file. The user enters "Y", indicating that the target file will be overwritten, and "N" to cancel the move to the source file. This avoids overwriting the file incorrectly.

-F, contrary to "-I", prohibits interactive operation. Do not give any hints when overwriting your own target files.

Note: MV and CP results are different, MV as if the file "moving", the number of documents did not increase. The CP copies files, and the number of files increases.

Example:

Rename the file ex3 to New1:

# MV Ex3 New1

Move all files in the directory/usr/men to the current directory (with "." indicated):

# mv/usr/men/*.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.