bashcommand-linecmddir

How to copy a directory in a way that all files replaced by zero size files with the same filename


For example, I have a directory called aaa, and there are files named 11.txt,22.exe,33.mp3under aaa directory.

Now I want to make a mirror of this directory, but I only want the structure of the directory not the contents, That is to copy aaa to bbb. Files under bbb should be 11.txt.zero,22.exe.zero,33.mp3.zero. The .zero extension indicates that all the copied file should be of zero file size.

The directory generally contains subfolers.

How to achieve this using windows CMD and linux bash?


Solution

  • @echo off
        setlocal enableextensions disabledelayedexpansion
    
        set "source=c:\somewhere\aaaa"
        set "target=x:\anotherplace\bbbb"
    
        robocopy "%source%\." "%target%\." /s /e /create
        for /r "%target%" %%a in (*) do ren "%%~fa" "%%~nxa.zero"
    

    Here the robocopy command will handle the folder recursion and zero file generation. Once this is done, all the files in the target structure are renamed.