1
I have a following structure:

-folder1
   -folder2
     -file1
     -file2
   -folder3
     -folder4
       -folder5
         -file3
         -file4

Using bash I need to create a .tar archive in the actual directory, which will contain 4 files(file1,file2,file3,file4). Everything that I've tried makes an archive of files with their paths. What I need is an archive of just files, which must contain:

file1
file2
file3
file4
Vlad
  • 127
  • 3
  • 9
  • 1
    Have you tried --strip-components=n or --strip=n (used when extracting), see http://stackoverflow.com/questions/8043579/how-to-strip-path-while-archiving-with-tar? – user2672165 Jan 03 '16 at 21:01
  • @user2672165 No, because I don't need to extract it, just to create – Vlad Jan 04 '16 at 02:33

4 Answers4

4

If you know the filenames and their directories you can use the -C/--directory option to have tar change its operating directory as it processes arguments.

So this command:

tar -cf files.tar.gz -C folder1/folder2 file1 file2 -C ../folder3/folder4 file3 file4

will create files.tar.gz in the current directory with just the files in it.

You can automate creating the necessary command line arguments from find output or similar with a little bit of scripting if necessary.

(You can't use globs like folder1/folder2/* in the file argument positions because the paths in those filenames will cause tar to fail to find them. You can use $(ls folder1/folder2/) in that position but that's obviously not safe for many valid filenames.)

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
0

Simple solution is create hard links to /tmp, then archive from there, something like this :

mkdir /tmp/allfiles
find /home/user/folder1 -type f | while read file
do
ln $file /tmp/allfiles
done
cd /tmp/allfiles
tar cvf /tmp/myarchive.tar *
rm /tmp/allfiles/*
rmdir /tmp/allfiles

Note that hard link will only work if both /tmp and ~/folder1 are on the same filesystem. This will only make links and not consume extra diskspace.

You might remove the rm command at first. a type in your script could have bad results :-).

  • Note that if `/tmp` is on a separate file system, then you cannot create hard links across file systems, but symlinks wouldn't be correct either. – Jonathan Leffler Jan 04 '16 at 07:59
0

Try this without creating any temp dir :

find .type f  | pax -wv -s '/.*\///g' -f file.tar

from man pax :

pax — read and write file archives and copy directory hierarchies

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

Rather than have to create clumsy links or do without wildcards, you could could create your archive and then change directory and append to it (with the r option) like this:

tar cvf ~/archive.tar file*                             # make initial tarball
(cd folder2 && tar rvf ~/archive.tar file* )            # append others to it
(cd folder3/folder4 && tar -rvf ~/archive.tar file*)    # and more...

Note that I do the above in a subshell to avoid having to cd back afterwards.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432