Stripping Paths from Files in TAR Archives

There is no way to get tar to ignore directory paths of files that it is archiving. So, for example, if you have a large number of files scattered about in subdirectories, there is no way to tell tar to archive all the files while ignoring their subdirectories, such that when unpacking the archive you extract all the files to the same location. You can, however, tell tar to strip a fixed number of elements from the full (relative) path to the file when extracting using the “--strip-components” option. For example:

tar --strip-components=2 -xvf archive.tar.gz

This will strip the first two elements of the paths of all the archived files. To get an idea of what this will look like before extracting, you can use the “-t” (”tabulate”, or list) in conjunction with the “--show-transformed option:

tar --strip-components=2 -t --show-transformed -f archive.tar.gz

The “--strip-components” approach only works if all the files that you are extracting are the same relative depth. Files that are “shallower” will not be extracted, while files that are deeper will still be extracted to sub-directories. The only clean solution to this that I can think of would be to extract all the files to a temporary location and then move all the files to single directory:

mkdir /tmp/work
cd /tmp/work
tar -xvzf /path/to/archive.tar.gz
mkdir collected
find . -type f -exec mv  collected/ \;