In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to move files without mv commands". In daily operation, I believe that many people have doubts about how to move files without mv commands. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to move files without mv commands". Next, please follow the editor to study!
Before moving away from mv, take a look at the default results of this command. First, create a directory and generate files with permissions of 777: the humble mv command is one of the useful tools you can find on every POSIX system you've ever seen. Its purpose is clearly defined and does a good job of moving files from one location in the file system to another. But Linux is very flexible, and there are other ways to move files. Using different tools can perfectly match some special use cases, which is a small advantage.
$mkdir example$ touch example/ {foo,bar,baz} $for i in example/*; do ls / bin > "${I}"; done$ chmod 777 example/*
You may not think so, but a file exists as an entry in a file system, called an Inode (usually called inode), and you can use the ls command and its-- inode option to view the inode occupied by a file:
$ls-inode example/foo7476868 example/foo
As a test, move the file from the sample directory to the current directory, and then view the properties of the file:
$mv example/foo. $ls-l-G-g-- inode7476868-rwxrwxrwx. 1 29545 Aug 2 07:28 foo
As you can see, the original file and permissions have been "moved", but its inode has not changed.
This is how the mv tool is used to move: leave the inode unchanged (unless the file is moved to a different file system) and retain its ownership and permissions.
Other tools offer different options.
Copy and delete
On some systems, the move operation is actually moving: bits are deleted from one location in the file system and reassigned to another location. This kind of behavior has fallen out of favor to a large extent. Now, the move operation is either a reallocation of attributes (inode now points to different locations in the file organization) or a combination of copy and delete operations. The philosophical intention of this design is to ensure that the file is not fragmented when the move fails.
Unlike mv, the cp command creates a completely new data object in the file system with a new inode location, depending on the umask. You can use the cp and rm (if any, or trash-LCTT tool) commands to mimic the mv command.
$cp example/foo. $ls-l-G-g-- inode7476869-rwxrwxr-x. 29545 Aug 2 11:58 foo$ trash example/foo
The new foo file in the example gets 755 permissions because the umask here explicitly excludes write permissions.
$umask0002
For more information about umask, read Alex Juarez's article on file permissions.
View and delete
Similar to copying and deleting, use the cat (or tac) command to assign different permissions when creating a "move" file. Suppose there is a new test environment without foo in the current directory:
$cat example/foo > foo$ ls-l-G-g-- inode7476869-rw-rw-r--. 29545 Aug 8 12:21 foo$ trash example/foo
This time, a new file is created without prior permissions, so the final permissions of the file depend entirely on the umask setting, which does not block the permission bits of users and groups (no executable permissions are granted to the new file, regardless of umask), but it prevents others from writing (a value of 2). So the result is a file with permissions of 664.
Rsync
The rsync command is a powerful and versatile tool for sending files between host and file system locations. There are many options available for this command, including making its target mirror the source.
You can use rsync copy with the-- remove-source-files option, then delete the file, and then take any other option you choose to perform synchronization (the common common option is-- archive):
$rsync-archive-remove-source-files example/foo. $ls examplebar baz$ ls-lGgi7476870-rwxrwxrwx. 1 seth users 29545 Aug 8 12:23 foo
Here, you can see that the permissions and ownership of the file are retained, but the timestamp is updated and the source file is deleted.
Warning: do not confuse this option with-- delete, which deletes files (which do not exist in the source directory) from the destination directory. Misuse-delete cleans up a lot of data, and it is recommended that you do not use this option unless you are in a test environment.
You can override some of these default values, change permissions and modify settings:
$rsync-chmod=666-times\-remove-source-files example/foo. $ls examplebar baz$ ls-lGgi7476871-rw-rw-r--. 1 seth users 29545 Aug 8 12:55 foo
Here, the target's umask takes effect, so the-- chmod=666 option produces a file with permissions of 644.
The benefit is not just permissions, the rsync command has many useful options compared to simple mv commands (the most important of which is the-- exclude option, so you can exclude certain items from a large mobile operation), which makes it a more powerful tool. For example, to exclude all backup files when you move a file collection:
Rsync-- chmod=666-- times\-- exclude'* ~'\-- remove-source-files example/foo.
Use install to set permissions
The install command is a copy command specifically for developers and is mainly called as part of the software compilation and installation routine. It's not known to users (I often wonder why it has such an intuitive name, while the rest of the package manager can only use acronyms and nicknames), but install is actually a useful way to put files where you want them.
The install command has many options, including the-- backup and-- compare commands (to avoid updating new copies of the file).
Unlike the cp and cat commands, but exactly like mv, the install command can copy files while retaining their timestamps:
$install-- preserve-timestamp example/foo. $ls-l-G-g-- inode7476869-rwxr-xr-x. 1 29545 Aug 2 07:28 foo$ trash example/foo
Here, the file is copied to a new inode, but its mtime (modification time) remains unchanged. However, the permission is set to the default value of 755 for install.
You can use install to set file permissions, owners, and groups:
Install-- preserve-timestamp\-- owner=skenlon\-- group=dialout\-- mode=666 example/foo. $ls-li7476869-rw-rw-rw-. 1 skenlon dialout 29545 Aug 2 07:28 foo$ trash example/foo
Move, copy, and delete
The file contains data, and the really important file contains your data. It's important to learn to manage them wisely, and now you have a toolkit to make sure you process the data the way you want.
At this point, the study on "how to move files without mv command" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.