0

I want to move a folder (test) on our server from /home/data to /home/projects/data. What I did is to use mv.

It did not move the folder in the end for some permission problems. However, the folder was still created in data with no files in it. When I run du -sh on that empty folder it shows 21MB.

Why is it not zero?

I am afraid that something still got moved from that test folder since I need to move test with all files in the end and don't want to lose anything.

Is it correct to conclude that nothing has been moved when the source test folder has not been changed today?

ls -la shows .profile, .ssh, .vnc and .config as well as the links to the folders up the tree. Does that mean that those are only “hidden files” and this is where the non zero size comes from?

4
  • 2
    If you ran mv and it worked to a point and then stopped due to permissions issues, then you have partially moved files and directories. What was moved and what wasn’t? Hard to say easily. You should look at the contents of the destination and see if you can manually move them back to the source directory. Apr 5, 2022 at 16:52
  • 1
    What is the output of du -ah /path/to/allegedly_empty_dir? Or at least ls -la /path/to/allegedly_empty_dir? Please edit the question and add this information. Apr 5, 2022 at 16:56
  • Thanks for your answers. However, if I would have moved sth it must be in the new directory and the old folder must have changed right? Both is not the case--the old folder has not been changed today and there are no files in the new directory.
    – Laurie1989
    Apr 5, 2022 at 17:03
  • sorry,ls -l gives me zero files in the new directory. ls -la shows the files mentioned above
    – Laurie1989
    Apr 5, 2022 at 17:06

2 Answers 2

1

How mv works

Below there are excerpts from the POSIX specification of mv and my comments.

SYNOPSIS

mv [-if] source_file target_file
mv [-if] source_file... target_dir

You may have used non-POSIX options; irrelevant. I'm citing the synopsis here because the later citations mention source_file and it's good to know what exactly they mean. Note a directory is also a file.

[…]

For each source_file the following steps shall be taken:

[…]

  1. The mv utility shall perform actions equivalent to the rename() function […]

    […]

    If this succeeds, mv shall do nothing more with the current source_file and go on to any remaining source_files. If this fails for any reasons other than those described for the errno [EXDEV] […], mv shall write a diagnostic message to standard error, do nothing more with the current source_file, and go on to any remaining source_files.

This basically means mv tries to rename the source_file first. Inside a single filesystem this (usually, not always (example)) works and then mv doesn't need to do anything more. EXDEV means this cannot work and then mv tries a different approach. In your case the fact the old directory is still there along with the (partial) new directory means rename threw EXDEV and mv tried the different approach. The main point of the different approach is as follows:

[…]

  1. The file hierarchy rooted in source_file shall be duplicated as a file hierarchy rooted in the destination path. […]

    […]

    If the duplication of the file hierarchy fails for any reason, mv shall write a diagnostic message to standard error, do nothing more with the current source_file, and go on to any remaining source_files.

    If the duplication of the file characteristics fails for any reason, mv shall write a diagnostic message to standard error, but this failure shall not cause mv to modify its exit status.

So mv acts similarly to cp -Rp. If there is any problem with duplicating data (but not characteristics, metadata) then it doesn't proceed to the next step, which is:

  1. The file hierarchy rooted in source_file shall be removed.

So after successfully doing the cp part at least for data, mv acts like rm -r for the current source_file before proceeding to the next (if any).

The conclusion is: if your mv follows the POSIX specification (e.g. GNU mv basically should) and if you specified a single source_file (it seems so) and if the error prevented any data from being duplicated (it seems so) then everything you tried to move should stay intact in the source location.

But see the caveats below.


Caveats

  • If the error you observed was from step 7 (as opposed to step 6) then the source location may or may not be intact. E.g. imagine your mv was not allowed to remove anything; or maybe it was not allowed to remove the directory in question from /home/data after removing everything from the directory itself. Unfortunately you did not tell us the exact error(s) you observed. Note any error from step 7 means step 6 has already duplicated the data successfully (if it didn't, step 7 would be skipped). If the destination is obviously missing some files, the error was probably from step 6 and the removal of the source location was not tried in the first place.

  • In the context of your question there is a huge difference between

    mv foo /destination/
    

    and

    mv foo/* /destination/foo/
    

    In the former command there is exactly one source_file, step 6 is performed at most once and step 7 is performed at most once. If foo is not replicated in the destination then nothing will be deleted. In the latter command foo/* is expanded by the shell to possibly many source_files and mv processes each one separately; so if step 6 fails and this is going to suppress step 7 then it will suppress step 7 for the currently processed source_file only.

    Unfortunately you did not tell us the exact command you used. You wrote "the folder was still created" and I interpret this as "created by mv". The latter of the two commands requires /destination/foo/ to exist beforehand, so I conclude your command was not like it and this caveat is not relevant to your particular case.


Your explicit questions

How to check what was moved when using mv and the process stops due to permissions issues?

In general: by carefully examining what's in the target location and what's missing in the source location. It's way easier if the source location was initially empty. As explained above, if you specified exactly one source_file and the error was from the copying phase (step 6), not from the deleting phase (step 7), then probably you don't need to check anything, the source is intact.

Is it correct to conclude that nothing has been moved when the source test folder has not been changed today?

In general it depends on what you mean by "the source test folder has not been changed today". If you judge solely by mtime of this single directory or/and files directly in it, then the important fact is mtime does not propagate from a file to its parent directory and further. If a file deep in the directory tree changes then you won't know unless you examine things deep enough.

Still in your case you probably don't need to check anything, the source is intact. If you're not sure, see how to get "recursive" last modified date for a directory. Removing a file from its directory changes the mtime of the directory, so this is a right way to check if your mv may have removed anything.

Why is it [the output from du -sh] not zero?

Because there are files in the directory. ls -la confirmed this.

ls -la shows .profile, .ssh, .vnc and .config as well as the links to the folders up the tree. Does that mean that those are only "hidden files" and this is where the non zero size comes from?

Yes. Use du -ah to confirm this.

1
  • Sorry about that and thank you so much for your answer! The exact command I used was mv /home/data/ /home/projects/data/. I stopped the process when I got the error messages because I wanted to avoid to only move parts of the folder. So I can only reproduce " Permission denied" and I am a bit reluctant to run again for the same reason. I copy everything now and remove it then manually.
    – Laurie1989
    Apr 6, 2022 at 8:58
0

“Is it correct to conclude that nothing has been moved when the source test folder has not been changed today?”

That is the correct conclusion. If the source folder was not touched, then nothing was moved.

The only risk is if you ran mv and it worked to a point and then stopped due to permissions issues, then you have partially moved files and directories.

In a case like that what was moved and what wasn’t moved? Hard to easily say. If that were to have happened, you should look at the contents of the destination and see if you can manually move them back to the source directory.

1
  • 1
    "If the source folder was not touched, then nothing was moved" – In general I would be careful, depending on what the OP means by "the source test folder has not been changed today". If they judge solely by mtime of this single directory or/and files directly in it, then the important fact is mtime does not propagate from a file to its parent directory and further. In general if a file deep in the directory tree changes then you won't know unless you examine things deep enough. Apr 5, 2022 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .