Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you merge two directories, or move with replace, from the windows command line without copying?

So I just wrote a quick python script to move some large directories around (all on the same drive), incorrectly assuming windows command line tools weren't a complete joke and that move Root\Dir1 Root\Dir2 would, like windows explorer GUI, merge the contents. I really don't care whether it replaces or skips duplicate files within the folders because there aren't any.

Unfortunately (in an admin command prompt),

C:\>mkdir a

C:\>mkdir b

C:\>mkdir b\a

C:\>move b\a .
Overwrite C:\a? (Yes/No/All): yes
Access is denied.

... :O

... ?? really ??!?

... no, actually really really ???

It seems the only way is to copy and delete. Painfully pathetic.

Related:

  • How can I move the contents of one directory tree into another?

  • how to merge two folders by batch cmd

  • how do i fix: 'access denied' with the move command in windows 7?

I'm not writing code to copy files one by one. Is there any way to achieve a folder move with replace without copying?

I'd prefer to use some native executable if possible. I'd also be quite happy to use python if it supported it.

like image 727
jozxyqk Avatar asked Mar 23 '14 07:03

jozxyqk


People also ask

How do I merge two folders with the same name?

Windows 10 can automatically merge the contents of two folders that have the same name when you copy the folder from one location to another. Locate the two folders you want to merge. If they do not have the same name, rename them so that the names are identical. Select one of the two folders, and tap Ctrl+C.

How do I copy a folder from one directory to another?

rsync -av /source/ /destination/ (after checking) rm -rf /source/ You can use the -l option of the cp command, which creates hard links of files on the same filesystem instead of full-data copies. The following command copies the folder source/folder to a parent folder ( destination) which already contains a directory with the name folder.

How do I merge two folders in Ubuntu terminal?

Locate the two folders you want to merge. If they do not have the same name, rename them so that the names are identical. Select one of the two folders, and tap Ctrl+C. Navigate to the second folder’s location. Tap the Ctrl+V keyboard shortcut. The two folders will be merged automatically.

How to move files from one folder to another in Linux?

For example, if you want to move all text files from current folder to a new location you can use the below command. To move all files starting with letter ‘A’, you can use below command. Example: To move the directory ‘data’ to ‘D:\data\folder1\’ 1.


1 Answers

The move-all-files-manually workaround in python. I'm still reeling from the stupidity.

def moveTree(sourceRoot, destRoot):
    if not os.path.exists(destRoot):
        return False
    ok = True
    for path, dirs, files in os.walk(sourceRoot):
        relPath = os.path.relpath(path, sourceRoot)
        destPath = os.path.join(destRoot, relPath)
        if not os.path.exists(destPath):
            os.makedirs(destPath)
        for file in files:
            destFile = os.path.join(destPath, file)
            if os.path.isfile(destFile):
                print "Skipping existing file: " + os.path.join(relPath, file)
                ok = False
                continue
            srcFile = os.path.join(path, file)
            #print "rename", srcFile, destFile
            os.rename(srcFile, destFile)
    for path, dirs, files in os.walk(sourceRoot, False):
        if len(files) == 0 and len(dirs) == 0:
            os.rmdir(path)
    return ok

Please post a proper answer if there ever is one!

like image 165
jozxyqk Avatar answered Nov 03 '22 05:11

jozxyqk