0

I want to move the directory content of the folder foo at a specific moment including the subfolder structure to another directory. If files are added to the origin folder they should not be moved.

If I run a command like mv foo/* bar/ and add a file to the folder foo while the process is running, which behaviour can be observed?

  • file is moved

  • file is not moved

  • file is moved if the filename is ordered after the filename of the file currently being moved

  • other behaviour

Is the behaviour different in Windows/DOS? What's the easiest way to solve the problem described?

1 Answer 1

4

There is no caching because the mv command doesn't even see the wildcard.

What happens is that the command line expands the wildcard and passes the expanded list to mv

So if you had foo/1' andfoo/2andfoo/3` then running

mv foo/* bar/

will expand and actually run

mv foo/1 foo/2 foo/3 bar/

Once the shell has finished the expansion and started executing the mv command then any changes to the foo directory (eg foo/4 gets added) won't be part of the command and so will be left behind.

This process is known as globbing.

7
  • If dir "1" contains multiple other dirs, do they get expanded too? Commented Jul 28, 2016 at 23:40
  • No. Globbing is not recursive. If you want recursive then you'd need the find command in some form. But note that if 1 was a directory then mv foo/1 bar/ will move the whole directory anyway, so you probably don't need recursion here. The mv command doesn't really care :-) Commented Jul 28, 2016 at 23:44
  • so does the mv command register change if I add foo/1/newfile.txt during the process of the expanded command mv foo/1 bar/1? Commented Jul 28, 2016 at 23:55
  • Directory moves like this are atomic; the mv command doesn't recurse down. It literally moves the whole directory in one action. So if you can create foo/1/newfile.txt then when foo/1 is renamed to bar/1 the new file will appear in the new directory. NOTE: this is not true if foo and bar are on different filesystems. Then mv has to recurse and a race condition like you're thinking of is possible. But for a standard keep on the same disk case, there is no race condition. Commented Jul 29, 2016 at 0:00
  • in one of my cases the origin and destination are on different filesystems. Does the mv command look for file after file in the subfolder and moves the newfile.txt that is existent immediately after the start of the mv command? Commented Jul 29, 2016 at 0:14

You must log in to answer this question.

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