Linux basic file Management three brothers (CP, MV, RM)

Source: Internet
Author: User
Tags rsyslog

We use the most of the files in our daily work I think it should be copy, cut, move, rename, delete these five. So today we mainly introduce the three commands involved in implementing the above five operations in Linux: CP, MV, RM.


CP is a shorthand for copy, and from the name we can roughly know its role, it is mainly used to copy files. Although this command is simple, it has a lot of details that we need to be aware of, and if we ignore it, the command is likely to fail to achieve the results we want.

The following information is from the Man Handbook

NAME

cp-copy files and directories


Single source copy (source file is a text file)

Its command format is:

CP [OPTION] ... [-T] SOURCE DEST

If Dest does not exist: The file is created in advance and the data stream of the source file is copied to dest;

If dest exists: 1, if the dest is a non-directory file, overwrite the target file, 2, if dest is a directory file, first create a file with the same name in the Dest directory and copy the data flow


Multi-source replication (the source file is a catalog file with multiple files under it)

Its command format:

CP [OPTION] ... SOURCE ... DIRECTORY

CP [OPTION] ...-t DIRECTORY SOURCE ...

If Dest does not exist: error

If dest exists: 1, if the dest is not a directory file, error 2, if the directory is a directory file, copy each file to the target directory, and keep the original.


Common options:

-I.,--interactive

Prompt before overwrite (Overrides a previous-n option)

Interactive replication, overwriting before reminding user to confirm

-R,-R,--recursive

Copy directories recursively

Recursively replicating directories

-d Same as--no-dereference--preserve=links

Copy the symbolic link file itself, not the source file it points to

-A,--archive

Same AS-DR--preserve=all

Used to implement archiving

-F,--force

If an existing destination file cannot was opened, remove it and try again (this ignored when the-n option is also used)

Force overwrite target file

--preserve[=attr_list]

Preserve the specified attributes (default:mode,ownership,timestamps), if possible additional attributes:context, links , Xattr, all

mode: Permission ownership: Belong to and belong to group

Timestamps: Timestamp Context: Security label

Xattr: Extended Properties Links: Symbolic link

All: All of the above properties

-V,--verbose

Explain what's being done

Detailed display of the command's running process

Look at the above content a more specific understanding of the CP command and its options is given in a few examples below.

1, using the alias command, the daily/etc/directory of all files, backup to/testdir/under the new directory, and require the new directory format is BACKUPYYYY-MM-DD, the backup process is visible

[[email protected] ~]# mkdir/testdir[[email protected] ~]# cp-rdv/etc//testdir/bakup$ (date +%f) "/etc/rsyslog.conf"-&G T "/testdir/bakup2016-08-01/rsyslog.conf" "/ETC/VIMRC", "/TESTDIR/BAKUP2016-08-01/VIMRC", "/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 a lot of files under/etc/, we need to wait a while.

[Email protected] ~]# ls/testdir/bakup2016-08-01

Results to meet the requirements, the following explanation of the use of the CP three options, the requirement is that all files in the/etc directory should be backed up first, so you need to use the recursive option-R, which requires the backup process to be visible, and then plus-V, as for-D is to keep the backup file and the source file consistency, Although not explicitly required, but plus-D looks better.


2, first create the/testdir/rootdir directory, and then copy the/root all the next file into the directory, and ask to retain the original permissions.

[[EMAIL PROTECTED] ~]# MKDIR /TESTDIR/ROOTDIR[[EMAIL PROTECTED] ~]# CP  -r --preserve=mode,ownership /root /testdir/rootdir/[[email protected] ~]#   ll /root /testdir/rootdir/root/ /root: Total dosage  20-rw-------.  1 root  Root 1172 7 Month   20 00:38 anaconda-ks.cfgdrwxr-xr-x. 2 root root     6 7 month   21 18:52 cst-rw-r--r--.  1 root root    14 8 month    1 10:49 FILE-RW-------.  1 root root  1220 7 month   19 16:46 initial-setup-ks.cfg-rw-r--r--.  1 root root  4282 8 Moon    1 12:17 test.txt-rw-r--r--.  1 root root     0 8 Month    1 12:20 trdrwxr-xr-x. 2 root root     6 7 Month   22 09:52  Public drwxr-xr-x. 2 root root    6 7 Month   22  09:52  Template drwxr-xr-x. 2 root root    6 7 Month   22 09:52   Video drwxr-xr-x. 2 root root    6 7 Month   22 09:52  Photo Drwxr-xr-x. 2 root root    6 7 Month   22 09:52  Document DRWXR-XR-X. 2 ROOT ROOT    6 7 Month   22 09:52  Download Drwxr-xr-x. 2 root root    6 7 Month   22 09:52  Music drwxr-xr-x. 2 root root    6 7 Month   22 09:52  desktop/ testdir/rootdir/root/: Total dosage  20-rw-------.  1 root root 1172 8 month    1  22:06 anaconda-ks.cfgdrwxr-xr-x. 2 root root    6 8 month     1 22:06 cst-rW-r--r--.  1 root root   14 8 Month    1 22:06  FILE-RW-------.  1 root root 1220 8 Month    1 22:06  Initial-setup-ks.cfg-rw-r--r--.  1 root root 4282 8 Month    1 22:06  test.txt-rw-r--r--.  1 root root    0 8 Month    1  22:06 trdrwxr-xr-x. 2 root root    6 8 Month    1  22:06  Public drwxr-xr-x. 2 root root    6 8 Month    1  22:06  Template drwxr-xr-x. 2 root root    6 8 Month    1  22:06  Video drwxr-xr-x. 2 root root    6 8 Month    1  22:06  Picture drwxr-xr-x. 2 root root    6 8 Month    1  22:06  Document DRWXR-XR-X. 2 ROOT&NBsp;root    6 8 month    1 22:06  Download drwxr-xr-x. 2 root  root    6 8 Month    1 22:06  Music drwxr-xr-x. 2 root  root    6 8 Month    1 22:06  Desktop

Still the replication directory is still using the recursive option-R, this time using--preserve primarily to satisfy the requirement of reserved permissions. The result indicates that the requirement is met.

3, copy/etc/system-release file, compare plus-D with no-D what is the difference

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/4C/wKiom1efWu-DvvLBAAAwN0xmI5M864.gif "title=" Cp2.gif "alt=" Wkiom1efwu-dvvlbaaawn0xmi5m864.gif "/>

[[email protected] ~]# cp/etc/system-release/test/t1[[email protected] ~]# cp-d/etc/system-release/test/t2[[email Pro Tected] ~]# ll/test/total dosage 2-rw-r--r--. 1 root root 38 August 1 22:22 t1lrwxrwxrwx. 1 root root 14 August 1 22:22 T2-Centos-release[[email protected] test]# cat T1 centos Linux release 7.2.1511 (Core ) [[email protected] test]# cat t2cat:t2: no file or directory

By comparison of the results we can know that plus-D we copy the original file, if the target is a linked file we copy only the link file, not the original file.


MV is move abbreviations, the main role is to move and rename files

MV [OPTION] ... [-T] SOURCE DEST

MV [OPTION] ... SOURCE ... DIRECTORY

MV [OPTION] ...-t DIRECTORY SOURCE ...

Under the same path, the MV function is renamed

Under different paths, the MV action is to move files or cut

Common options:

-V,--verbose

Explain what's being done

-I prompt before every removal

The above two options are illustrated above and are no longer specifically demonstrated here.

The following experiment shows the specific role of MV

1. Change the T1 file name to T3 in the/test/directory

[[email protected] test]# ll total dosage 0-rw-r--r--. 1 root root 0 August 1 22:36 t1[[email protected] test]# mv T1 t3[[email protected] test]# ll total dosage 0-rw-r--r--. 1 root root 0 August 1 22:36 T3

2. Move the T3 file under the/test/directory to the/testdir/directory

[[email protected] test]# ll/testdir/total dosage 0[[email protected] test]# mv T3/testdir/[[email protected] test]# ll total dosage 0[[ema Il protected] test]# ll/testdir/total dosage 0-rw-r--r--. 1 root root 0 August 1 22:36 T3


rm-remove files or directories To remove a file or directory

Format: rm [OPTION] ... FILE ...

Common options:

-F,--force Force Delete

Ignore nonexistent files and arguments, never prompt

-I prompt before every removal interactive mode

-R,-R,--recursive recursive delete

Remove directories and their contents recursively

-V,--verbose display Delete detailed procedure

Explain what's being done

Here is an experiment to illustrate the role of RM

1. Delete the T1 file in the/test/directory

[Email protected] test]# RM t1rm: Do you want to delete the plain empty file "T1"? Y[[email protected] test]# ll total dosage 0drwxr-xr-x. 2 root root 18 August 1 22:46 file

Under Root, it is "cp=cp-i" by default

2. Delete the file folder under the/test/directory

[[Email protected] test]# RM file/rm: Cannot delete "file/": is a directory [[email protected] test]# rm-f file/rm: Cannot delete ' file/': is a directory

We found that it was not possible to delete the directory by simply using RM and adding the Force delete option.

Deleting a directory requires the-R recursion, deleting the directory and deleting all the files below it.

[[email protected] test]# RM-RFV file/deleted "File/file1" deleted directory: "file/"

The deletion process is first removed from the bottom of the file after deleting the top up and then delete it until the topmost.

This article is from "Zhang Fan-it's fantasy drifting" blog, please be sure to keep this source http://chawan.blog.51cto.com/9179874/1833275

Linux basic file Management three brothers (CP, MV, RM)

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.