General Computing
linux bash tar archiving
Updated Thu, 18 Aug 2022 15:13:27 GMT

Target directory structure for tar different than original directory structure


Is there any way to set a different target directory structure than the orignial directory structure the tar archive was created from without creating it by hand?

For example, I have many files in a directory structure like this:

~/yyyy/mm/dd/hh/mm/*.*

Organized by when the file was received. I already create a tar archive without preserving the directory structure above for a whole day's worth of files (I have a small script that loops thru every minute of every hour and appends to a single tar archive). I would like to specify to tar to extract the archive to directory structure like this:

~/yyyymmdd

Seems simple enough as I could create the yyyymmdd directory by hand and just extract my archive into it, but I give this tar archive to a different system (which I have no control over) that relies on this tar archive being extracted to a yyyymmdd directory and the yyyy/mm/dd/hh/mm/*.* directory structure gives me performance gains because its organized. Also, I cannot change the original directory structure or waste system resources creating the desired target directory structure before creating the tar archive.

Is there a simple way extract a tar archive under a directory it was never in in the first place?




Solution

If you have this structure

/home/yyyy/mm/dd/hh/mm/
 dir1
  file4
  file5
 dir2
  file6
 file1
 file2
 file3

and you want this structure

/home/yyyymmdd/
 dir1
  file4
  file5
 dir2
  file6
 file1
 file2
 file3

Create:

tar -cvf /tmp/archive.tar /home/yyyy/mm/dd/hh/mm/

Extract:

tar --strip-components=6 -C /home/yyyymmdd/ -xvf /tmp/archive.tar