1

I've been trying to use this code:

find documents -maxdepth 1 -type f -print0 | xargs -0 tar cvf documents.tar.gz

to create a backup of the directory documents.

The directory has the structure:

enter image description here

And I want to archive them into a single directory documents/:

enter image description here

But I fail to do so. How can I do this? Thank you

2
  • 1
    You should have used text instead of those pictures. Dec 13 '14 at 15:00
  • The -maxdepth 1 expression means that find won't go more than 1 directory deep. Is that what you want? Dec 13 '14 at 15:50
1

You could hardlink them on a different directory and than tar

# find all files in directory named directory
find directory -type f | \
while read f;
do
    f2=`echo "$f"|sed -e 's/\//:/g'`  # replace / with :
    ln "$f" "directory2/$f2"  # create hardlinks in directory2/
done

This will create the following links

directory/file1 => directory2/directory:file1
directory/file2 => directory2/directory:file2
directory/folder1/file3 => directory2/directory:folder1:file3
directory/folder1/file4 => directory2/directory:folder1:file4
directory/folder2/file5 => directory2/directory:folder2:file5
directory/folder3/folder4/file6 => directory2/directory:folder3:folder4:file6
directory/folder3/folder4/file7 => directory2/directory:folder3:folder4:file7
2
  • for f in $(command substitution) is not a good idea because even a space will break that. That should be done with e.g. find directory -type f -exec ./script.sh + with script.sh being your code starting with for f in "$@" instead. Furthermore you should always use quoting: echo "$f", ln "$f" "directory2/$f2" Dec 13 '14 at 21:07
  • Agreed @HaukeLaging I updated double quotes and converted from for f in to while read f so files with spaces should be handled fine.
    – Mehmet
    Dec 13 '14 at 21:41
0

I guess the way to do this is to create a new directory just as backup source and fill it with hardlinks. I hope the whole directory tree you want to backup is on the same volume.

> pwd
/path/to/documents
> cd /path/to/
> mkdir tar-documents
> mkdir tar-documents/documents
> ln /path/to/documents/file1
> ln /path/to/documents/file2
> ln /path/to/documents/folder1/file3 folder1:file3
[...]

If the number of files is high then this should be done by a script.

Afterwards you run tar on this directory:

> cd /path/to/tar-documents
> tar cvzf /backup/path/documents.tar.gz documents

At the end it may be useful to delete the tar directory.

0

You can try this.

Find all *tar files in current folder and copy to MainDirectory folder or wherever do you want.

find . -iname "*tar*" -exec cp {} ~/MainDirectory/ \;

0

Firstly, if you want to archive a directory, having the identical file structure in the archive is the best practice for taking that snapshot. What you are asking for is the renaming of your files to include its parent directories succeeded by colons followed by the filename. However strange to me this is out of context, I did manage to get pretty close using the --transform option:

 --transform=EXPRESSION, --xform=EXPRESSION
                         use sed replace EXPRESSION to transform file

An example of this is outlined in this stackexchange answer (Though be aware that it is not proper sed syntax). Using --transform your command would be this:

tar czvf documents.tar.gz --transform 's/\//:/g' documents

Unfortunately, this did not have the desired effect, as it keeps your subdirectories in the archive but they are empty, and the files are at the root of the archive, not in the first parent directory.

I thought I had a solution for you using -C, --directory option, but it would not be very dynamic, nor would it accomodate for maintaining the directory names of files in the new file name.

The solution I did find is a hybrid of what you have and the use of the transform command:

find documents -type f -print0 | xargs -0 tar czvf test.tar.gz --no-recursion --transform 's/\//:/g' --transform 's:\::/:1' documents/

Also, in your question you name your file with the .gz extension but it was lacking the -z, --gzip option.

Hope this helps,
Alex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.