Learning linux-Basic Six (Common command: CP,MV,RM)

Source: Internet
Author: User
Tags file copy

I. CP for file copy, divided into Tan Yuanwen replication and multi-source replication

Single source copy syntax: CP [Options] ... [-t] source dest.

Precautions:
1 if Dest does not exist, the file is created in advance and the data stream from the source file is copied to dest.

2 If the dest is a non-directory file, the destination file is overwritten.

3 If Dest is a directory file, create a file with the same name as the source file in the Dest directory and copy the source file data stream.

Cp/tmp/ou/111.txt files to the/ETC/HH directory

[[email protected] testdir]# mkdir/tmp/ou[[email protected] testdir]# cd/tmp/ou[[email protected] ou]# Ls[[email Protect Ed] ou]# touch Xixi hehe haha 111.txt[[email protected] ou]# ls111.txt  haha  hehe xixi[[email protected  ] ou]# m Kdir/etc/hh[[email protected] ou]# cp/tmp/ou/111.txt/etc/hh/[[email protected] ou]# ls/etc/hh/111.txt

  

Example 2: If the dest is a non-directory file, the destination file is overwritten.

[Email protected] ou]# cat/tmp/ou/111.txt Hello World!!!!! I am Study computer[[email protected] ou]# cat/etc/hh/222.txt ni hao ma zcgmy love girl[[email protected] ou]# cp-i/tmp /ou/111.txt/etc/hhal/         hosts        hp/          hh/          hosts.allow  htdig/       host.conf    hosts.deny   httpd/       

Example 3: If Dest does not exist, the file is created in advance and the data flow from the source file is copied to dest.

[Email protected] ou]# ls/etc/hh/111.txt  222.txt[[email protected] ou]# Cp/tmp/ou/111.txt/etc/hh/300[[email Protected] ou]# ls/etc/hh/111.txt  222.txt  300

  

Multi-source Replication syntax:
CP [OPTION] ... SOURCE ... DIRECTORY

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

Precautions:
1 displays an error if directory is a regular file.

2 if directory is a directory file, copy each file to the destination directory separately and keep the original.

CP 123,123.txt to/etc/aa/bbb.txt file, the error is displayed.

[Email protected] ou]# cat/etc/aaa/bbb.txt catcatcat!!!!!!!!!!!!!!!!!!!!!! 1!!!!!!! 2!!!!! 3[[email protected] ou]# ls/tmp/ou/111.txt  123  123.txt  haha  hehe  xixi[[email protected] ou]# CP- R/tmp/ou/{123,123.txt}/etc/aaa/bbb.txt cp:target '/etc/aaa/bbb.txt ' is not a directory

  

CP 123,123.txt to the/ETC/HH directory, copy the file to the directory and keep the original.

[[email protected] ou]# cp-r/tmp/ou/{123,123.txt}/etc/hh[[email protected] ou]# ls/etc/hh111.txt  123  123.txt  222.txt  300

  

CP Command Common Command options:

-I: Interactive replication, that is, before overwriting to remind the user to confirm.

-F: Forces the overwrite of the target file.

-R: Recursively replicate the contents and directories in the directory.

-D: Copies the symbolic link file itself rather than the source file to which it is pointing.

-A: Used to implement archiving.

MV is used to move the source file to the target file or directory.

Grammar:

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

Mv/tmp/ou/111.txt to/etc/hh.

[[email protected] hh]# ls/etc/hh123  123.txt  222.txt  300[[email protected] hh]# ls/tmp/ou111.txt  123   123.txt  haha  hehe  xixi[[email protected] hh]# mv/tmp/ou/1111.txt  123/     123.txt/[[email] Protected] hh]# Mv/tmp/ou/111.txt/etc/hh[[email protected] hh]# ls/etc/hh/111.txt  123  123.txt  222. TXT  300

  

MV Command common options:

-I: Interactive replication, that is, before overwriting to remind the user to confirm.

-F: Forces the overwrite of the target file.


RM command for deleting files or directories

Syntax: RM [OPTION] ... FILE ...


RM-RF (recursive directory enhancement Delete)/tmp/ou/123

[Email protected] hh]# ls/tmp/ou123  123.txt  haha  hehe  xixi[[email protected] hh]# rm-rf/tmp/ou/123[ [Email protected] hh]# ls/tmp/ou123.txt  haha  hehe  xixi

  

Common options:

-I: Interactive Delete, that is, before deleting to remind the user to confirm.

-F: Forces the deletion of the target file.

-R: Recursively deletes the contents and directories in the directory.

Iv. Performance of the bash command execution status return values and command-line expansion are involved in the content and its example demonstration

Command execution status and return value: Bash outputs this result with a status return value of 0 for success and 1-255 for failure. After the command execution is complete, its status return value is saved in Bash's special variable $?.

ls command after using echo $? Viewing the result return value of 0 means that the command execution succeeded.

[[email protected] ~]# ls/tmp/ou123.txt  haha  hehe  xixi[[email protected] ~]# echo $?0

ls command after using echo $? View Results The return value of 2 indicates that the command failed to execute.

[[email protected] ~]# ls/tmp/xxxls:cannot access/tmp/xxx:no such file or Directory[[email protected] ~]# echo $?2

  

Command line expand: ~: Expand to User's home directory, {}: can host a comma-delimited list and expand it to multiple paths


Switch to home directory, ~.

[[email protected] ~]# Cd/tmp/ou[[email protected] ou]# CD ~[[email protected] ~]# pwd/root

  

The TMP directory creates a,b,c,3 files at the same time.

[[email protected] ~]# Cd/tmp/[[email protected] tmp]# Touch {a,b,c}[[email protected] tmp]# LSA          ou                  vmwaredndb
   pulse-lc4gadmk6caz  VMWARE-ROOTC          pulse-on2an8esj49w  vmware-root-1848984901etc        PULSE-RPT7FGHJNOBKORBIT-GDM  ssh-jnwnun2843

  

Use the command line expansion feature to complete the following exercises

(1), create/tmp directory: A_c, A_d, B_c, B_d

[[email protected] tmp]# mkdir {a,b}_{c,d}[[email protected] tmp]# LSA    BC   etc                 Ssh-jnwnun2843ac   b_c< C12/>ORBIT-GDM           vmwarednda_c  BD   ou                  Vmware-rootad   b_d  Pulse-lc4gadmk6caz  Vmware-root-1848984901a_d  c    pulse-on2an8esj49wb    D    PULSE-RPT7FGHJNOBK

  

(2), create the/tmp/mylinux directory:

mylinux/

├──bin

├──boot

│?? └──grub

├──dev

├──etc

│?? ├──rc.d

│?? │?? └──init.d

│?? └──sysconfig

│?? └──network-scripts

├──lib

│?? └──modules

├──lib64

├──proc

├──sbin

├──sys

├──tmp

├──usr

│?? └──local

│?? ├──bin

│?? └──sbin

└──var

├──lock

├──log

└──run

[Email protected] ~]# mkdir-pv/tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.dsysconfig/network-scripts},lib/ Modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}

  

[Email protected] ~]# tree/tmp/mylinux//tmp/mylinux/└──{bin,boot    └──grub,dev,etc        └──{rc.d            └── Init.dsysconfig                └──network-scripts},lib                    └──modules,lib64,proc,sbin,sys,tmp,usr                        └──local                            ├──bin , var                            │├──lock                            │├──log                            │└──run                            └──sbin,var                                ├──lock                                ├──log                                └──RUN15 Director IES, 0 files

  

What are the metadata information for a file, what does it mean, and how to view it? How to modify timestamp information for a file.

Stat command

Stat–display file or file system status
Show the status of a file or file system

Stat FILE ...

Files: Two types of data
Meta data: metadata:access (last accessed), modify (last modified), change (last changed)
Statistics: Data

Time stamp:
Access time:2015-12-10 16:12:22.776423693 +0800
Modify Time:2015-12-10 16:12:22.776423693 +0800
Change time:2015-12-10 16:12:22.776423693 +0800

[[email protected] ~]# echo 123 >>/tmp/functions[[email protected] ~]# stat/tmp/functions   File: '/tmp/functio NS '  size:4               blocks:8          IO block:4096   regular filedevice:802h/2050d      inode:917667      Links: 1Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root) access:2016-12-09 16:35:10.187014284 +0800     #最近访问Modify: 2016-12-09 16:35:10.187014284 +0800      #最近更改: Data modification change:2016-12-09 16:35:10.187014284 +0800     #最近改动: Metadata modification, data modification

  

Change the timestamp command: Touch

Touch–change file Timestamps
Modify file Timestamp

Touch [OPTION] ... FILE ...
-C: The specified file path does not exist when not created;
-A: Modify access time only;
-M: Modify modify time only;
-T STAMP
[[Cc]yy] MMDDHHMM[.SS]

[[email protected] ~]# touch-m-t 1212020900.00/tmp/functions   #修改时间为2012年12月2日09:00[[email protected] ~]# stat/tmp /functions                        File: '/tmp/functions '  size:4               blocks:8          IO block:4096   Regular filedevice:802h/2050d      inode:917667      Links:1access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root) access:2016-12-09 16:35:10.187014284 +0800modify:2012-12-02 09:00:00.000000000 +0800change:2016-12-09 16:41:46.359015406 + 0800

How do I define an alias for a command, and how do I refer to the execution result of another command in a command?

To define a command alias using the alias command

Alias command

Alias

Get all the available alias definitions

~]# Alias

Alias

~]# alias Name= ' COMMAND '

Valid only for the current shell process
To revoke an alias:

~]# Unalias NAME

  

[[email protected] ~]# aliasalias cp= ' cp-i ' Alias grep= ' grep--color=auto ' Alias l.= ' ls-d. *--color=auto ' Alias ll= ' ls-l --color=auto ' Alias ls= ' ls--color=auto ' alias mv= ' mv-i ' Alias rm= ' rm-i ' Alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde ' [[email protected] ~]# type Aliasalias is a shell builtin[[ Email protected] ~]# alias Cls=clear[[email protected] ~]# CLS          #此时cls拥有了clear的功能
[[email protected] ~]# unalias cls     #取消了clear功能 [[email protected] ~]# Cls-bash:cls:command not found

Referencing the execution result of another command in a command, using a pipeline |

Pipeline

A connection program that directs the output of the previous command directly to the latter program as an input data stream
COMMAND1 | COMMAND2 | COMMAND3 | ...

Globbing: File name wildcard

(whole file name matches, not parts)

Match pattern: metacharacters
*: matches any character of any length
pa*, *pa*, *pa, *p*a*
PA, PAA, passwd

?: matches any single character
Pa??? PA, P?a, P?a?
PA, PAA, passwd

[]: matches any single character within the specified range
There are several special formats:
[A-z], [A-z], [0-9], [a-z0-9]
[[: Upper:]]: All uppercase letters
[[: Lower:]]: All lowercase letters
[[: Alpha:]]: All letters
[[:d Igit:]]: All numbers
[[: Alnum:]]: All letters and numbers
[[: Space:]]: all whitespace characters
[[:p UNCT:]]: All punctuation

PA[0-9][0-9], 2[0-9][0-9]
[^]: matches any single character outside the specified range

[^[:upper:]] All non-capital letters
[^0-9] all non-digital
[^[:alnum:]] All non-letters and numbers

Displays all files or directories in the/var directory that start with L, end with a lowercase letter, and have at least one digit (can have other characters) appear in the middle

[Email protected] var]# ls-d/VAR/1*[0-9]*[[:LOWER:]]/VAR/123ABC

Displays files or directories that start with any number in the/etc directory and end with a non-numeric

[Email protected] etc]# lls-d/etc/[0-9]*[^0-9]-bash:lls:command not found

Displays a file or directory that starts with a non-letter, followed by a letter and any character of any length, in the/etc directory

[Email protected] etc]# ls-d [^a-z][a-z]*ls:cannot access [^a-z][a-z]*: No such file or directory

In the/tmp directory, create the tfile beginning, followed by the current date and time of the file, the file name is as follows: tfile-2016-05-27-09-32-220

[Email protected] etc]# mkdir/tmp/tfile-$ (date +%y-%m-%d-%h-%m-%s) [[email protected] etc]# Cd/tmp/[[email protected] t mp]# LSA    b_d                 PULSE-ON2AN8ESJ49WAC   c                   pulse-rpt7fghjnobka_c  d                   ssh-wfbsgl2767ad   etc                tfile-2016-12-09-17-02-19a_d  functions           virtual-zcl1840. QM3SYSB    mylinux             vmwaredndbc   orbit-gdm           vmware-rootb_c  ou                  vmware-root-1848984901bd   Pulse-lc4gadmk6caz  vmware-zcl1840

Copy all files or directories in the/etc directory to the/tmp/mytest1 directory that begin with P and do not end with a number

[[email protected] tmp]# mkdir mytest1[[email protected] tmp]# cp-r/etc/p*[^0-9]/tmp/mytest1

Copy all files or directories ending with. D in the/etc directory into the/tmp/mytest2 directory

[Email protected] tmp]# cp-r/etc/*.d/tmp/mytest2

Copy all items in the/etc directory that start with L or M or N and end with. conf to the/TMP/MYTEST3 directory

[Email protected] tmp]# cp-r/etc/[1mn]*.conf/tmp/mytest3

  

  

  

  

 

 

  

  

Learning linux-Basic Six (Common command: 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.