I want to move the file to a different directory in the shell.
If I fail for any reason after doing mv, is there a possibility of data loss?
As a typical work-level method, I would appreciate it if you could also tell me how to move files and confirm that they were successful.
linux sh
If you don't define data loss, you won't get along.For example,
$mv filea backup/filea
However, any backup/filea
that already exists is deleted and lost.Is this data loss or not?
Except for the deletion of the intended file, only consider whether the contents of the file will change or not
1. mv
to the same file is detected in advance and nothing happens.That is, no loss. It's easy to assume that you don't do anything stupid, but it's rare that you don't realize that you have a symlink and end up with mv
to the same file.
$mvfileb.
$ mv filec<simlink-to-this-directory>/filec
Error pointing out the same file
2.mv
that contradicts the directory structure is detected in advance and abbreviated below.
$mkdir hoge
$ mkdir hoge /piyo
$ mv hoge hoge /piyo
Moving directories to subdirectories is error
3. When used in the same disk device, the correspondence between inode
and the file name (which is called a directory entry) is only changed, and the file itself is not in touch, so you can think of no data loss.
$mv failed filee
Moving in the same directory appears to be in the same device, so it succeeds without loss.
4. A copy of the file body will be included when moving to another disk device in the same machine.If there is not enough space, it will fail during copying and the original file will not be deleted, so there will be no loss.
$mv filef/mounted_other_drive/fileg
5. Move to a different machine/structured file system above, but lose information that is not functional to the mounting destination.For example, if you mv
a sparse file to a non-sparse mounting destination, the size will explode (untested).
During copying, we do not consider data corruption due to a disk device failure (because it is not specific to mv
), and the behavior of comparing CRC values of file contents from mv
or cp
data copy source Data corruption on the network is corrected at the TCP level, so there is no problem.
If you feel that there is a risk in executing the mv
command, you should use the -i
or -n
options (-nv
is better).
https://eng-entrance.com/linux_command_mv
For example, running mv-ia.txt b.txt
causes an overwrite confirmation message when b.txt
already exists, and running mv-nva.txt b.txt
does not overwrite if b.txt
already exists.
mv-i
might be a good way to use it to suit your question.
In addition, if you want to avoid unintentionally overwriting files, we recommend backing up files using git
or rsync
.
mv
behaves differently depending on whether it is the same file system or not.
If you run mv/src/file/dst/file
, /src/
and /dst/
will take advantage of the rename()
system call if the file system is the same. rename()
will only rewrite directory entries.
If you are crossing the file system, do the following:
If kill
occurs in the middle of 1. above, /dst/file
may be copied half-heartedly.To avoid it, copy it into the same file system and then mv
to use the rename()
system call.
cp/src/file/dst/temporary
mv/dst/temporary/dst/file ← secure as it is in the same file system
rm/src/file
572 Understanding How to Configure Google API Key
566 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
595 GDB gets version error when attempting to debug with the Presense SDK (IDE)
878 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.