[Linux Command Explanation Encyclopedia] 042. Detailed explanation and examples of necessary CP commands in Linux

cp

Copy a source file or directory into a destination file or directory

Supplementary Note

cpThe command is used to copy one or more source files or directories to the specified destination files or directories. It can copy a single source file into a specific file with a specified filename or an existing directory. cpThe command also supports copying multiple files at the same time. When copying multiple files at a time, the target file parameter must be an existing directory, otherwise an error will occur.

grammar

cp [选项] [参数]

options

  • -a: The effect of this parameter -dpRis the same as specifying the parameter at the same time;
  • -d: When copying a symbolic link, create the target file or directory as a symbolic link, and point to the original file or directory linked to the source file or directory;
  • -f: Forcibly copy a file or directory, regardless of whether the target file or directory already exists;
  • -i: Ask the user before overwriting existing files;
  • -l: establish a hard link to the source file instead of copying the file;
  • -p: preserve the attributes of the source file or directory;
  • -R/r: Recursive processing, processing all files and subdirectories under the specified directory together;
  • -s: Create a symbolic link to the source file instead of copying the file;
  • -u: After using this parameter, the file will be copied only when the change time of the source file is newer than that of the target file or when the target file with the corresponding name does not exist;
  • -S: When backing up files, use the specified suffix "SUFFIX" instead of the default suffix of the file;
  • -b: Back up the target file before overwriting the existing file target;
  • -v: Detailed display of the operations performed by the command.

parameter

  • 源文件: Specify a list of source files. By default, cpthe command cannot copy directories, if you want to copy directories, you must use -Rthe option;
  • 目标文件: Specify the target file. When 源文件is multiple files, it is required 目标文件to be the specified directory.

example

The first line below is cpthe command and specific parameters ( -rit is "recursive", -uit is "update", -vit is "detailed"). The next three lines display information about the copied files, and the last line displays the command line prompt. This way, only new files are copied to my storage device, and I use cpthe "Update" and "Detailed" options.

In general, parameters -rare also available in a more verbose style --recursive. But in a short way, it can also be used like this -ruv.

cp -r -u -v /usr/men/tmp ~/men/tmp

The version backup --backup=numberedparameter means "I want to make a backup, and it is a serial backup with numbers". So one backup is number 1, the second is number 2, and so on.

$ cp --force --backup=numbered test1.py test1.py
$ ls
test1.py test1.py.~1~ test1.py.~2~

If a file is copied into a destination file, and the destination file already exists, then the contents of the destination file will be destroyed. All parameters in this command can be either absolute path names or relative path names. .Usually the dot or dot form is used ... For example, the following command copies the specified file to the current directory:

cp ../mary/homework/assign .

All directories specified by the object file must already exist, cpthe command cannot create a directory. If you do not have permission to copy files, the system will display an error message.

Copy the file fileto the directory /usr/men/tmpand rename it to file1:

cp file /usr/men/tmp/file1

/usr/menCopy all files under directory and its subdirectories into directory /usr/zh:

cp -r /usr/men /usr/zh

Interactively copy all files /usr/menstarting with in directory to directory :m.c/usr/zh

cp -i /usr/men m*.c /usr/zh

When using the command to copy files under Linux cp, sometimes you need to overwrite some files with the same name. When overwriting files, there will be a prompt: you need to keep pressing Yto confirm the execution of overwriting. It’s okay that there are not many files, but if there are hundreds of files, it is estimated that you Ywill vomit blood, so after a long time, I summed up a method:

cp aaa/* /bbb
# 复制目录 aaa 下所有到 bbb 目录下,这时如果 bbb 目录下有和 aaa 同名的文件,需要按 Y 来确认并且会略过 aaa 目录下的子目录。

cp -r aaa/* /bbb
# 这次依然需要按 Y 来确认操作,但是没有忽略子目录。

cp -r -a aaa/* /bbb
# 依然需要按 Y 来确认操作,并且把 aaa 目录以及子目录和文件属性也传递到了 bbb。

\cp -r -a aaa/* /bbb
# 成功,没有提示按 Y、传递了目录属性、没有略过目录。

Recursively force copy directories into the specified directory overwriting existing files:

cp -rfb ./* ../backup
# 将当前目录下所有文件,复制到当前目录的兄弟目录 backup 文件夹中

Copy the hidden files in the directory such as .babelrc:

cp -r aaa/.* ./bbb
# 将 aaa 目录下的,所有`.`开头的文件,复制到 bbb 目录中。

cp -a aaa ./bbb/
# 记住后面目录最好的 `/` 带上 `-a` 参数

Copy to current directory:

cp aaa.conf ./
# 将 aaa.conf 复制到当前目录

Learn from scratchpython

[Learn python from zero] 92. Use Python's requests library to send HTTP requests and process responses
[Learn python from zero] 91. Use decorators and dictionaries to manage simple web applications for request paths
[Learn python from zero] 93. Use dictionary management Request path
[Learn python from zero] 89. Use WSGI to build a simple and efficient web server
[Learn python from zero] 88. Detailed explanation of WSGI interface: realize simple and efficient web development
[Learn python from zero] 87. Manually build HTTP server Python Implementation and multi-threaded concurrent processing
[learning python from zero] 86. In-depth understanding of HTTP protocol and its role in browser and server communication
[learning python from zero] 85. Parallel computing technology application of Python process pool
[learning python from zero ] 84. In-depth understanding of threads and processes
[learning python from zero] 83. Python multi-process programming and the use of process pools
[learning python from zero] 82. Realization of chat programs based on multi-threading
[learning python from zero] 81. Python and more Application of thread communication and queue
[learning python from zero] 80. Thread access to global variables and thread safety issues
[learning python from zero] 79. Thread access to global variables and thread security issues
[learning python from zero] 78. File download cases
[ Learning python from zero] 77. TCP server programming and precautions
[learning python from zero] 76. Server and client: key components of network communication
[learning python from zero] 75. TCP protocol: reliable connection-oriented transmission layer communication protocol
[Learn python from zero] 74. UDP network program: detailed explanation of port problems and binding information
[Learn python from zero] 73. UDP network program - send data
[Learn python from zero] 72. In-depth understanding of Socket communication and socket creation The method of
[learning python from zero] 71. Network ports and their functions
[learning python from zero] 70. Network communication methods and their applications: from direct communication to routers to connect multiple networks
[learning python from zero] 69. Network communication and IP address classification analysis
[learning python from zero] 68. Greedy and non-greedy modes in Python regular expressions
[learning python from zero] 67. re module in Python: regular replacement and advanced matching technology
[learning python from zero] 66 .In-depth understanding of regular expressions: a powerful tool for pattern matching and text processing
[Learn python from zero] 65. Detailed explanation of Python regular expression modifiers and their applications
[Learn python from zero] 64. The re.compile method in Python regular expressions Detailed explanation
[learning python from zero] 63. Introduction to the re.Match class in regular expressions and its attributes and methods
[learning python from zero] 62. Python regular expressions: a powerful string matching tool
[learning python from zero] 61. Detailed explanation and application examples of property attributes in Python
[learning python from zero] 60. Exploration generator: a flexible tool for iteration
[learning python from zero] 59. Iterator: an efficient tool for optimizing data traversal
[learning python from zero] 58. Custom exceptions in Python and methods of raising exceptions
[Learn python from zero] 57. Use the with keyword in Python to correctly close resources
[Learn python from zero] 56. The importance and application of exception handling in programming
[Learn python from zero] 55. Serialization and processing in Python Deserialization, application of JSON and pickle modules
[learning python from zero] 54. Writing data in memory
[learning python from zero] 53. CSV files and Python CSV modules
[learning python from zero] 52. Reading and writing files - Python file operation guide
[learning python from zero] 51. Opening and closing of files and its application in Python
[learning python from zero] 49. Object-related built-in functions in Python and their usage
[learning python from zero] 48 .Detailed explanation of inheritance and multiple inheritance in Python
[learning python from zero] 47. The concept and basic use of inheritance in object-oriented programming
[learning python from zero] 46. __new__ and __init__ method analysis and singleton in Python Design patterns
[learning python from zero] 45. Class methods and static methods in Python
[learning python from zero] 44. Private attributes and methods in object-oriented programming
[learning python from zero] 43. Examples in Python object-oriented programming Attributes and class attributes
[Learn python from zero] 42. Built-in attributes and methods in Python
[Learn python from zero] 41. Python magic method (2)
[Learn python from zero] 40. Python magic method (1)
[Learn python from zero] 39. Object-oriented basic grammar and application examples
[Learn python from zero] 38. Use and import of Python packages
[Learn python from zero] 37. Use and precautions of Python custom modules
[From zero Learning python] 36. Methods and skills of using pip in Python for third-party package management
[learning python from zero] 35. Python common system modules and their usage
[learning python from zero] 34. Detailed explanation of the import and use of Python modules
[ Learn python from zero] 33. The role of decorators (2)
[Learn python from zero] 32. The role of decorators (1)
[Learn python from zero] 31. In-depth understanding of higher-order functions and closures in Python
[from Zero learning python] 30. In-depth understanding of recursive functions and anonymous functions
[learning python from zero] 29. "Detailed explanation of function parameters" - understand the different usages of Python function parameters
[learning python from zero] 28. Local variables and globals in Python Variables
[Learn python from zero] 27. Use and nested calls of Python functions
[Learn python from zero] 25. Functions: a tool to improve code writing efficiency
[Learn python from zero] 24. String operations and traversal methods in Python
[Learn python from zero] 23. How to use and common operations of sets (set) in Python
[Learn python from zero] 22. Addition, deletion, modification and query of dictionaries in Python and variables of dictionaries
[Learn python from zero] 21. In Python tuples and dictionaries for
[Learn python from zero] 20. Python list operation skills and examples
[Learn python from zero] 19. Application of looping through lists and list nesting
[Learn python from zero] 18. Detailed explanation of basic operations of Python lists (1)
[From Zero learning python] 17. Format method of Python string (2)
[Learn python from zero] 16. Format method of Python string (1)
[Learn python from zero] 15. In-depth understanding of string and character set encoding
[from Zero learning python] 14. Common operations of Python strings (2)
[Learn python from zero] 13. Common operations of Python strings (1)
[Learn python from zero] 12. Python string operations and applications
[Learn python from zero] 11.Python loop statement and control flow
[learn python from zero] 10. Detailed explanation of Python conditional statement and if nesting
[learn python from zero] 09. Conditional judgment statement in Python
[learn python from zero] 08. Python understands bit operations Operators, operator priority
[learn python from zero] 07. Detailed explanation of Python operators: assignment, comparison and logical operators
[learn python from zero] 06. Use arithmetic operators in Python for calculation and string concatenation
[learn from zero python] 05. Output and input in Python
[learn python from zero] 04. Basics of Python programming: variables, data types and identifiers
[learn python from zero] 03. Python interactive programming and detailed annotations
[Learn python from zero] 02. Introduction to development tools
[Learn python from zero] 01. Install and configure python

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132616780
Recommended