Copy and Move

Commands copy to and move to are among the most useful Perun2 has. However, using them requires a bit of caution. Here are some remarks about them.

move '*/*/*.txt' to 'f:/data'

This command is fine and will work as intended. It will find all text files 3 levels deep in the directory tree and move them to f:/data. Nothing unexpected here.

move '*/*/*.txt' to 'a/b'

This command also looks good, but there is a tiny problem. Text files in location a/b will be moved to the place they are already in. There is also a possibility, that the same thing will happen to files that freshly landed in a/b. Source (*/*/*.txt) and destination (a/b) are overlapping. However, in spite of all, this command will also be executed correctly and will do what it is supposed to do. As long as we copy and move only files and not directories, nothing undesirable is going to happen.

move 'a/b' to 'a/b/c'

Operations made on directories are more problematic because of their recursive nature. Command above will never run successfully. Directories cannot be moved or copied to themselves.

move 'a/b/c/*' to 'a'

This example is even worse. Let us imagine there is a directory a/b/c/b/c/e/f in current working location. The directory a/b/c/b is moved to a, so now there exists a path a/b/c/e/f. The command can be executed twice on the same data. Directory a/b/c/e can be moved again to a, resulting in an path a/e/f.

If directories are copied or moved, source and destination should be indepenent of each other. This is the only rule that should be kept in mind.

v = 'a/b/c/*';
move v to 'a'

However, there is a solution to this issue and we can still perform the imagined operation. Let us save the value of a/b/c/* to a variable. All paths to these files and directories are stored in memory in advance, so the collection never changes no matter what happens. Now, we can move them safely. Every directory will be moved only once.

move '**/data.txt'
  to 'c:/data'

All files with name data.txt in the entire directory tree are moved to c:/data. Where is the problem? Name collisions happen. As a result, relocation fails.

stack move '**/data.txt'
  to 'c:/data files'

The stack option solves this problem. When it happens, files are renamed using this pattern: data(2).txt, data(3).txt etc.

force copy '*.exe'
  to 'f:/'

There is another available option: force. It bypasses name collisions in a harsher way. If there exists a file or a directory with the same name, it is erased.