Linux command operations

Source: Internet
Author: User
Tags file copy

The function of this command is to copy the given file or directory to another file or directory, just like the Copy command under DOS, which is very powerful.

Syntax: CP [option] source file or directory destination file or directory

Note: This command copies the specified source files to the destination file or copies multiple source files to the destination directory.

The options for this command have the following meanings:

-A This option is typically used when copying a directory. It retains links, file attributes, and recursively copies the directory, which is equal to the combination of the DPR option.

-Keep links when copying D.

-F Delete a target file that already exists without prompting.

-I and F options, in contrast, will prompt the user for confirmation before overwriting the target file. Answer y when the target file will be overwritten, is an interactive copy.

-P At this point, the CP will also copy the modified time and access rights to the new file in addition to the contents of the source file.

-R If the source file is a directory file, the CP will recursively replicate all subdirectories and files in that directory. The destination file must be a directory name at this time.

-L do not make copies, just link files.

It should be explained that in order to prevent the user inadvertently using the CP command to destroy another file, such as the user specified the target file name is an existing file name, the CP command after copying the file, the file will be the new copy of the source file overwritten, it is recommended that users use the CP command to copy files, it is best to use the I option.

$ cp-i exam1.c/usr/wang/shiyan1.c

This command copies the file exam1.c to the/usr/wang directory and renames it to shiyan1.c. If you do not want to rename it, you can use the following command:

$ CP exam1.c/usr/wang/

$ cp-r/usr/xu//usr/liu/copies all files and their subdirectories in the/usr/xu directory to the directory/usr/liu.

MV Command

Users can use the MV command to rename files or directories or to move files from one directory to another. This command is like a combination of Ren and move under DOS.

Syntax: MV [options] source file or directory destination file or directory

Description: Depending on the second parameter type in the MV command (target file or target directory), the MV command renames the file or moves it to a new directory. When the second parameter type is a file, the MV command completes the file rename, at which point the source file can only have one (or the source directory name), which renames the given source file or directory to the specified destination file name. When the second parameter is a directory name that already exists, the source file or directory parameter can have more than one, and the MV command moves the source files specified by each parameter to the destination directory. When you move a file across a file system, the MV copies the original file, and the link to the file is lost.

The meanings of the options in the command are:

-I interact with the mode of operation. If the MV operation will cause an overwrite of the target file that already exists, the system asks whether to rewrite and asks the user to answer Y or N, which avoids overwriting the file by mistake.

-F prohibit interactive operation. When the MV operation is to overwrite an existing target file without giving any indication, when this option is specified, the I option will no longer work.

If the given destination file (not the directory) already exists, the contents of the file will be overwritten by the new file. To prevent users from inadvertently destroying another file with the MV command, it is recommended that users use the I option when moving files using the MV command.

It is important to note that the MV is different from the CP results. MV as if the file "move", the number of files and the end of the increase, and the CP file copy, the number of files increased.

Example 1: Move all files in the/usr/xu to the current directory (with ".") Representation) in:

$ mv/usr/xu/*.

Example 2: Renaming a file wch.txt to Wjz.doc

$ mv Wch.txt Wjz.doc

RM command

It's easy to create files in Linux, and files are obsolete and useless at any time in the system. The user can delete it using the RM command. The function of this command is to delete one or more files or directories in a directory, and it can delete all files and subdirectories under a directory. For linked files, only the links are deleted and the original files remain unchanged.

The general form of the RM command is:

RM [Options] File ...

If the-r option is not used, RM does not delete the directory.

The options for this command have the following meanings:

-F Ignore nonexistent files and never give hints.

-R instructs RM to delete all directories and subdirectories listed in the parameter recursively.

-I do an interactive delete.

Use the RM command with extreme caution. Because once a file is deleted, it cannot be restored. For example, when entering CP,MV or other commands, the user accidentally entered the RM command inadvertently, and when the user presses the ENTER key and realizes his error, it is too late and the file is gone. To prevent this from happening, you can use the I option in the RM command to confirm each file that you want to delete. If the user enters Y, the file is deleted. If you enter anything else, the file will be retained. In the next example, the user wants to delete the file test and example. You will then be asked to confirm each file. The user finally decides to delete the example file and keep the test file.

$ rm-ii Test Example Remove test? n

Remove example? y

Linux command operations

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.