-1

Basically I have this directory structure in Linux.

Backup
 Dir_1 
     Sub_Dir_1
          file_1  90 GB
          file_2  10 GB
     Sub_Dir_2
          file_1  20 GB

Restore
 Dir_1
     Sub_Dir_1
         file_1 0 bytes
         file_2 0 bytes
     Sub_Dir_2
         file_1 0 bytes

And the list goes on.
I can tar the whole directory but that would take a while because of the file sizes of the files inside each sub directory.

What I want is to create a script that will backup the whole directory structure and be able to restore the same directory on different computer but with all files inside the sub directory zeroed out.
All I want is their directory name and file names. Is this possible?

5
  • Welcome on SuperUser. Take a minute to read How to Ask, and maybe how to format the question, so that you can help people to better answer you. How complex is your directory structure, and which is the number of files (1 10 100 1000 10000...1M) ? You can edit your post to complete the question.
    – Hastur
    Jun 30, 2016 at 7:02
  • One important difference is if you need or not to preserve timestamp ownership and attributes for each file/directory.
    – Hastur
    Jun 30, 2016 at 7:50
  • a workaround but if you're a big linux bash wiz then you could install cygwin on windows and write a linux bash script to do it
    – barlop
    Jun 30, 2016 at 10:10
  • @barlop I suppose he already is under Linux...
    – Hastur
    Jun 30, 2016 at 11:23
  • @oLiVeR:... and so? did you try the solution? did it worked for you? Give some feedback.
    – Hastur
    Jul 1, 2016 at 12:00

1 Answer 1

0

Divide in sub-problem

In general when you cannot find a fast complete solution to your problem you can divide it in sub-problems more easy to be solved.

  • A way is to create a tar file of the directory structure and a list of files to be touched inside the directory structure that you can compress after. You do not need to create the whole directory structure and you will have only two files.
    With the touch command[1] you can update the modification time of a file, and if not exists you will create an empty one(!). After you have created the copy of the directory structure you can touch the files inside.

    This script can create the files (tar+txt) in your current directory.

    #!/bin/bash
    HomeDir=$(pwd)
    BackupDIR="Backup"      # Change here
    
    TarFileName="$HomeDir/Directory_Structure.tar"
    ListFileName="$HomeDir/File_List.txt"
    
    cd "$BackupDIR"
    
    tar -cf "$TarFileName"  --no-recursion --files-from <(find . -type d)
    find . -type f -exec echo touch \"{}\" \; > "$ListFileName"
    gzip --best "$ListFileName" "$TarFileName"    # this to compress them
    

    Then to extract them from the Restore directory with your two files inside you can write

    tar -zxf Directory_Structure.tar.gz
    zcat File_List.txt.gz | /bin/bash
    

    Note that are implicit the assumptions that you don't care of the creation time, ownership and attributes of the files, that your names are not pathological, you have no links...

  • Another way is to create a local copy of the directory and structure. Again after that you have created the copy of the directory structure you can touch the files inside.
    Then you can backup all with tar.

    The disadvantages are that the original creation & modification times, ownerships and attributes will be lost. If needed you have to put after manually, and this will complicate your script. (touch -d "$(date -R -r filename)" Restore/filename...)
    You will need a lot of inodes not cosy if you have zillions of files.

    A good point is that you can create the local directory structure copy in an intelligent way (via tar or rsync for example; with the right options it will preserve all taking care of some pathology too).

  • Last but not least is to use something like the tarfile module for python [2] to create your tar file, managing each type (file and directory in a different way): be brave!

You must log in to answer this question.

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