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

741 views Asked by At

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?

5

There are 5 answers

1
MC ND On BEST ANSWER
@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.

3
uudecode On

at first copy directory structure:

find aaa -type d -print  | sed 's/^aaa/bbb/' | xargs mkdir -p

at second, touch the files:

find aaa -type f -print | sed 's/^aaa/bbb/' | sed 's/$/\.zero/' | xargs touch

and result:

[trak_000.COGITATOR] ➤ ls -lR aaa bbb
aaa:
total 0
drwxrwxr-x    1 trak_000 UsersGrp         0 Jun 12 12:20 q1

aaa/q1:
total 1
-rw-rw-r--    1 trak_000 UsersGrp       194 Jun 12 12:20 test1.txt

bbb:
total 0
drwxrwxr-x    1 trak_000 UsersGrp         0 Jun 12 12:24 q1

bbb/q1:
total 0
-rw-rw-r--    1 trak_000 UsersGrp         0 Jun 12 12:24 test1.txt.zero
3
fedorqui On

Slightly better:

Being in the directory you want to copy:

  • Make the directories: find -type d -exec mkdir -p ../new_dir/{} \;

  • Touch the files: find -type f -exec touch ../new_dir/{}.zero \;

Original approach

I would do it in two steps:

Copy all files with rsync -r:

rsync -r origin_dir/ target_dir/

Loop through the files in target_dir/, empty them and rename to .zero:

find target_dir/ -type f -exec sh -c 'f={}; > $f; mv $f $f.zero' \;

This of course not very optimal if the files in the original dir are quite big, because we first copy and then empty them.

Test

$ tree aa
aa
├── a
├── a1
│   └── d
├── b
├── c
└── d

1 directory, 5 files

$ ll aa/*
-rw-r--r-- 1 me me  6 Jun 12 11:16 aa/a
-rw-r--r-- 1 me me  6 Jun 12 11:16 aa/b
-rw-r--r-- 1 me me  6 Jun 12 11:16 aa/c
-rw-r--r-- 1 me me  6 Jun 12 11:16 aa/d

aa/a1:
total 0
-rw-r--r-- 1 me me  0 Jun 12 11:16 d
$ rsync -r aa/ bb/
$ tree bb
bb
├── a
├── a1
│   └── d
├── b
├── c
└── d

1 directory, 5 files
$ find bb/ -type f -exec sh -c 'f={}; > $f; mv $f $f.zero' \;
$ tree bb
bb
├── a1
│   └── d.zero
├── a.zero
├── b.zero
├── c.zero
└── d.zero

1 directory, 5 files
$ ll bb/*
-rw-r--r-- 1 me me  0 Jun 12 11:23 bb/a.zero
-rw-r--r-- 1 me me  0 Jun 12 11:23 bb/b.zero
-rw-r--r-- 1 me me  0 Jun 12 11:23 bb/c.zero
-rw-r--r-- 1 me me  0 Jun 12 11:23 bb/d.zero

bb/a1:
total 0
-rw-r--r-- 1 me me 0 Jun 12 11:23 d.zero
0
noni On

For a Windows command line option, one can create a zero-length mirror copy of a directory structure (and contents) with Robocopy:

robocopy <source> <destination> /e /create

/e - Copy Subfolders, including Empty Subfolders.

/create - CREATE directory tree structure + zero-length files only.

One can also substitute /mir for /e

/mir - MIRror a directory tree - equivalent to /PURGE plus all subfolders (/E)

The above command would need modified to append '.zero' to each file, but it would appear this zero-length folder structure mirror might have been what was desired in the first place.

This has been quite helpful when testing some dangerous commands on a wide variety of data.

References:

How to copy/mirror files, but only as a placeholder with 0 file length?

https://superuser.com/questions/332845/easiest-way-to-copy-a-directory-recursively-creating-0-length-files

https://ss64.com/nt/robocopy.html

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

0
Ujjwal Kumar On

Use tree command to get recursive list of source folder and then to create zero size structure use mkdir for subdirs and touch for files.