Basic Linux knowledge-file management commands (cp, mv, rm)

Source: Internet
Author: User
Tags rsyslog

Basic Linux knowledge-file management commands (cp, mv, rm)

In our daily work, we use the five most file operations: Copy, cut, move, rename, and delete. Therefore, we will introduce the three commands involved in the above five operations in Linux: cp, mv, and rm.

Cp is short for copy. We can know its role from the name. It is mainly used to copy files. Although this command is very simple, it has many details that need to be noted. If you ignore it a little, the command may not be able to achieve the desired result.

The following information is from the man manual.
NAME
Cp-copy files and directories

Single-source replication (the source file is a text file)
The command format is:

Cp [OPTION]... [-T] SOURCE DEST
If DEST does not exist: create this file in advance and copy the data stream of the source file to DEST;
If DEST exists: 1. If DEST is a non-directory file, it overwrites the target file. 2. If DEST is a directory file, create a file with the same name in the DEST directory and copy the data stream.

Multi-source replication (the source file is a directory file with multiple files under it)
Command Format:
Cp [OPTION]... SOURCE... DIRECTORY
Cp [OPTION]...-t directory source...
If DEST does not exist: an error is returned.
If DEST exists: 1. If DEST is a non-directory file, an error is returned. 2. If the directory is a directory file, copy each file to the target directory and keep the original name.

Common options:
-I, -- interactive
Prompt before overwrite (overrides a previous-n option)
Interactive replication, prompting the user to confirm before Overwriting

-R,-r, -- recursive
Copy directories recursively
Recursive copy directory
-D same as -- no-dereference -- preserve = links
Copy the symbolic link file itself, not the source file it points
-A, -- archive
Same as-dR -- preserve = all
Archive
-F, -- force
If an existing destination file cannot be opened, remove it and try again (this ignored when the-n option is also used)
Forcibly overwrite the target file
-- Preserve [= ATTR_LIST]
Preserve the specified attributes (default: mode, ownership, timestamps), if possible additional attributes: context, links, xattr, all
Mode: Permission ownership: owner and group
Timestamps: Timestamp context: security tag
Xattr: extended attribute links: Symbolic Link
All: all of the above attributes
-V, -- verbose
Explain what is being done
Detailed command running process


--------------------------------------------------------------------------------
After reading the above content, we will give you a more detailed understanding of the cp command and its options through several examples below.

1. Use the alias command to back up all files under the/etc/directory to an independent new directory under/testdir/every day. The new directory format must be backupYYYY-mm-dd, visible during Backup
[Root @ localhost ~] # Mkdir/testdir
[Root @ localhost ~] # Cp-rdv/etc // testdir/bakup $ (date + % F)
"/Etc/rsyslog. conf"-> "/testdir/bakup2016-08-01/rsyslog. conf"
& Quot;/etc/vimrc & quot;-& quot;/testdir/bakup2016-08-01/vimrc & quot"
"/Etc/pulse"-> "/testdir/bakup2016-08-01/pulse"
"/Etc/pulse/client. conf"-> "/testdir/bakup2016-08-01/pulse/client. conf"
"/Etc/pulse/daemon. conf"-> "/testdir/bakup2016-08-01/pulse/daemon. conf"
"/Etc/pulse/default. pa"-> "/testdir/bakup2016-08-01/pulse/default. pa"
"/Etc/pulse/system. pa"-> "/testdir/bakup2016-08-01/pulse/system. pa"
...
..
.
"/Etc/screenrc"-> "/testdir/bakup2016-08-01/screenrc"

Because there are many files under/etc/, you need to wait for a while.
12 [root @ localhost ~] # Ls/testdir/
Bakup2016-08-01

The results meet the requirements. The following explains the reasons for using the three cp options. The requirement is that all files under the/etc directory should be backed up first, therefore, we need to use the recursive option-r, which requires that the backup process be visible later, so we add-v. As for-d, it is necessary to maintain consistency between the backup file and the source file, although there is no clear requirement, but adding-d seems better.

2. Create the/testdir/rootdir directory, copy all/root files to the directory, and retain the original permissions.
[Root @ localhost ~] # Mkdir/testdir/rootdir
[Root @ localhost ~] # Cp-r -- preserve = mode, ownership/root/testdir/rootdir/
[Root @ localhost ~] # Ll/root/testdir/rootdir/root/
/Root:
Total usage 20
-Rw -------. 1 root 1172 July 20 00:38 anaconda-ks.cfg
Drwxr-xr-x. 2 root 6 July 21 18:52 CST
-Rw-r --. 1 root 14 Aug 17 10:49 file
-Rw -------. 1 root 1220 July 19 16:46 initial-setup-ks.cfg
-Rw-r --. 1 root 4282 August 1 12:17 test.txt
-Rw-r --. 1 root 0 August 1 12:20 tr
Drwxr-xr-x. 2 root 6 July 22 09:52 public
Drwxr-xr-x. 2 root 6 July 22 09:52 Template
Drwxr-xr-x. 2 root 6 July 22 09:52 video
Drwxr-xr-x. 2 root 6 July 22 09:52 Image
Drwxr-xr-x. 2 root 6 Aug 17 09:52 documentation
Drwxr-xr-x. 2 root 6 09:52 download
Drwxr-xr-x. 2 root 6 July 22 09:52 music
Drwxr-xr-x. 2 root 6 July 22 09:52 Desktop
/Testdir/rootdir/root /:
Total usage 20
-Rw -------. 1 root 1172 August 1 22:06 anaconda-ks.cfg
Drwxr-xr-x. 2 root 6 August 1 22:06 CST
-Rw-r --. 1 root 14 Aug 17 22:06 file
-Rw -------. 1 root 1220 August 1 22:06 initial-setup-ks.cfg
-Rw-r --. 1 root 4282 August 1 22:06 test.txt
-Rw-r --. 1 root 0 August 1 22:06 tr
Drwxr-xr-x. 2 root 6 August 1 22:06 public
Drwxr-xr-x. 2 root 6 August 1 22:06 Template
Drwxr-xr-x. 2 root 6 August 1 22:06 video
Drwxr-xr-x. 2 root 6 August 1 22:06 Image
Drwxr-xr-x. 2 root 6 Aug 17 22:06 documentation
Drwxr-xr-x. 2 root 6 22:06 download
Drwxr-xr-x. 2 root 6 August 1 22:06 music
Drwxr-xr-x. 2 root 6 August 1 22:06 Desktop

The copy directory still uses the recursive option-r. This time, -- preserve is used mainly to meet the permission retention requirement. The results show that the requirements are met.

3. Copy the/etc/system-release file and compare the difference between adding-d and without-d.

[Root @ localhost ~] # Cp/etc/system-release/test/t1
[Root @ localhost ~] # Cp-d/etc/system-release/test/t2
[Root @ localhost ~] # Ll/test/
Total usage 2
-Rw-r --. 1 root 38 Aug 17 22:22 t1
Lrwxrwxrwx. 1 root 14 August 1 22:22 t2-> CentOS-release
[Root @ localhost test] # cat t1
CentOS Linux release 7.2.1511 (Core)
[Root @ localhost test] # cat t2
Cat: t2: No file or directory

By comparing the results, we can know that we are copying the original file by adding-d. If the target is a link file, we are copying only a link file, not the original file.

--------------------------------------------------------------------------------
Mv is the abbreviation of move. It mainly serves to move and rename files.

Mv [OPTION]... [-T] SOURCE DEST
Mv [OPTION]... SOURCE... DIRECTORY
Mv [OPTION]...-t directory source...
In the same path, the mv is renamed.
In different paths, music videos are used to move or cut files.
Common options:
-V, -- verbose
Explain what is being done
-I prompt before every removal
The above two options are described in the previous example.

--------------------------------------------------------------------------------
The following describes the specific functions of music videos.

1. Change the t1 file name to t3 in the/test/directory.
[Root @ localhost test] # ll
Total usage 0
-Rw-r --. 1 root 0 August 1 22:36 t1
[Root @ localhost test] # mv t1 t3
[Root @ localhost test] # ll
Total usage 0
-Rw-r --. 1 root 0 August 1 22:36 t3

2. Move the t3 file in the/test/directory to the/testdir/directory.

[Root @ localhost test] # ll/testdir/
Total usage 0
[Root @ localhost test] # mv t3/testdir/
[Root @ localhost test] # ll
Total usage 0
[Root @ localhost test] # ll/testdir/
Total usage 0
-Rw-r --. 1 root 0 August 1 22:36 t3

--------------------------------------------------------------------------------
Rm-remove files or directories

Format: rm [OPTION]... FILE...
Common options:
-F, -- force Delete
Ignore nonexistent files and arguments, never prompt
-I prompt before every removal Interactive Mode

-R,-R, -- recursive Deletion
Remove directories and their contents recursively
-V, -- verbose display detailed deletion Process
Explain what is being done


--------------------------------------------------------------------------------
The following uses an experiment to demonstrate the role of rm.

1. Delete the t1 file in the/test/directory.
[Root @ localhost test] # rm t1
Rm: Do you want to delete a common empty file "t1 "? Y
[Root @ localhost test] # ll
Total usage 0
Drwxr-xr-x. 2 root 18 Aug 17 22:46 file

By default, "cp = cp-I" is used as the root user"
2. delete the file folder in the/test/directory.
[Root @ localhost test] # rm file/
Rm: "file/" cannot be deleted: it is a directory.
[Root @ localhost test] # rm-f file/
Rm: "file/" cannot be deleted: it is a directory.

We found that directories cannot be deleted only when rm is used and the Force delete option is added.

To delete a directory, add-r recursion. Delete the Directory and delete all the files under it.
[Root @ localhost test] # rm-rfv file/
Deleted "file/file1"
Deleted Directory: "file /"

The deletion process is to delete files from the bottom layer one by one, and then delete files from the top layer until the top layer.

This article permanently updates the link address:

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.