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

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

4 Answers 4


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;
    f2=`echo "$f"|sed -e 's/\//:/g'`  # replace / with :
    ln "$f" "directory2/$f2"  # create hardlinks in directory2/

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
  • 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" Commented Dec 13, 2014 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
    Commented Dec 13, 2014 at 21:41

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
> 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.


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/ \;


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,

You must log in to answer this question.

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