In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Copying documents in the office used to require specialized staff and machines. Nowadays, replication is a task that computer users do not need to think about. Copying data on a computer is so trivial that it happens before you know it, such as when dragging a document to an external hard drive.
It is an indisputable fact that digital entities are so easy to copy that most modern computer users have never considered other ways to copy their work. In any case, there are still several different ways to copy a document in Linux. Each method is unique depending on your purpose.
Here is a series of ways to copy files on Linux, BSD, and Mac.
Copy in GUI
Like most operating systems, you can use GUI to manage files entirely if you want.
Drag and drop.
The most obvious way to copy files is probably the way you used to copy files on your computer: drag and drop. On most Linux desktops, dragging and dropping from one local folder to another is the default way to move files, and you can change this behavior by holding down Ctrl after dragging files starts.
Your mouse pointer may have an indication, such as a plus sign, to show that you are in copy mode.
Note that if the file is placed on a remote system, whether it is a Web server or another computer accessed by a file sharing protocol on your own network, the default action is often to copy rather than move files.
Right click
If you feel that dragging documents on your desktop is not accurate or a bit clumsy, or that doing so will keep your hands off the keyboard for too long, you can often use the right-click menu to copy files. It depends on the file manager you use, but in general, the context menu that pops up with the right button will include common actions.
The "copy" action of the context menu saves your file path (that is, the file in the system location) in your clipboard so that you can paste your file somewhere else: (LCTT Note: the description here and below is not accurate, it is not a "string" of the copied file path, but a copy of the object / pointer that represents the file entity.)
In this case, you did not copy the contents of the file to your cut version. Instead, you copied the file path. When you paste, your file manager will look at the path on the clipboard and execute the copy command, pasting the files on the corresponding path to the path you are going to copy.
Copy with the command line
Although GUI is usually a relatively familiar way to copy files, terminal replication is more efficient.
Cp
The most obvious way to be equivalent to copying and pasting files on the desktop on a terminal is the cp command. This command can copy files and directories, but also relatively straightforward. It uses familiar source and destination syntax (which must be in this order), so copy a file named example.txt to your Documents directory like this:
$cp example.txt ~ / Documents
Just like when you drag and drop files in a folder, this action does not replace Documents with example.txt. Instead, cp senses that Documents is a folder and puts a copy of example.txt in it.
You can also easily and effectively rename your copied documents:
$cp example.txt ~ / Documents/example_copy.txt
Importantly, it allows you to make a copy in the same directory as the original file:
$cp example.txt example.txtcp: 'example.txt' and' example.txt' are the same file.$ cp example.txt example_copy.txt
To copy a directory, you must use the-r option (stands for-- recursive, recursive). Run the cp command on the directory nodes with this option, and then all files in that directory will be affected. Without the-r option, cp does not treat the directory as a replicable object:
$cp notes/ notes-backupcp:-r not specified; omitting directory 'notes/'$ cp-r notes/ notes-backup
Cat
The cat command is the most misunderstood command, but only because it shows the extreme flexibility of the POSIX system. Of all the things cat can do, including its intended purpose of connecting files, it can also copy. For example, with cat you can create two copies of a file with just one command. You can't do that with cp.
One thing to note about copying documents using cat is the way the system interprets this behavior. When you use cp to copy a file, the properties of the file are copied along with the file, which means that the permissions of the copy are the same as the original.
$ls-l-G-G Murray RW Murray RW Murray Rafe Murray. 1 57368 Jul 25 23:57 foo.jpg$ cp foo.jpg bar.jpg-rw-r--r--. 1 57368 Jul 29 13:37 bar.jpg-rw-r--r--. 1 57368 Jul 25 23:57 foo.jpg
However, reading the contents of one file to another with cat allows the system to create a new file. These new files depend on your default umask settings. To learn more about umask, read Alex Juarez's article on umask and an overview of permissions.
Run unmask to get the current settings:
$umask0002
This setting means that newly created documents there are given 664 (rw-rw-r--) permission because the first few digits of the unmask setting do not mask any permissions (and the execution bit is not the default bit for file creation), and the write permission is masked by the final bit.
When you use cat to copy, you don't actually copy the file. You use cat to read the contents of the file and redirect the output to a new file:
$cat foo.jpg > baz.jpg$ ls-l-G-G Murray. 1 57368 Jul 29 13:37 bar.jpg-rw-rw-r--. 1 57368 Jul 29 13:42 baz.jpg-rw-r--r--. 1 57368 Jul 25 23:57 foo.jpg
As you can see, the default umask settings of the cat application create an entirely new file.
In the end, these methods don't matter when you just want to copy a file. But if you want to copy files and keep the default permissions, you can do everything with one command, cat.
Rsync
With its famous ability to synchronize source and destination files, the rsync command is a versatile tool for copying files. At its simplest, rsync can be used similar to the cp command.
$rsync example.txt example_copy.txt$ lsexample.txt example_copy.txt
The real power of this command lies in its ability not to make unnecessary copies. If you use rsync to copy files into a directory and they already exist in that directory, then rsync will not copy. This is not very different locally, but if you copy large amounts of data to a remote server, the meaning of this feature is completely different.
Even locally, the real difference is that it can distinguish between files with the same name but different data. If you ever find yourself facing two identical copies of the same directory, rsync can synchronize them to a directory that contains each of the latest changes. This configuration is common in the industry where the power of versioning has not yet been discovered, and is also used as a backup scheme that needs to be replicated from a trusted source.
You can consciously simulate this by creating two folders, one called example and the other called example_dupe:
$mkdir example example_dupe
Create a file in the first folder:
$echo "one" > example/foo.txt
Both directories now contain the same information:
$cat example/foo.txtone$ cat example_dupe/foo.txtone
If the file you use as the source branch changes, the destination file will also be updated:
$echo "two" > > example/foo.txt$ rsync-av example/ example_dupe/$ cat example_dupe/foo.txtonetwo
Note that the rsync command is used to copy data, not to act as a version management system. For example, if there is a destination file that has more changes than the source file, that file will still be overwritten because rsync compares the differences between the files and assumes that the destination file should always be mirrored as the source file:
$echo "You will never see this note again" > example_dupe/foo.txt$ rsync-av example/ example_dupe/$ cat example_dupe/foo.txtonetwo
If there is no change, then no copy action will occur.
The rsync command has many options that cp does not have, such as setting target permissions, excluding files, deleting obsolete files that do not appear in both directories, and more. Rsync can be used as a powerful substitute or effective supplement to cp.
Many ways to copy
There are many ways to achieve the same goal in POSIX systems, so the flexibility of open source is worthy of the name. Have I forgotten which effective way to copy data? Share your replication skills in the comments section.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.