Today I learned: How to create a TAR archive of only the contents of a directory, without the directory or the path itself. And this stackoverflow answer helped me get the job done.
The problem is rather simple: When creating a tar archive of any given directory, the directory becomes part of the archive.
# Create the source directoy and files
mkdir source
touch source/file1
touch source/file2
touch source/file3
# Create the archive the "usual" way
tar -caf archive.tar.gz source/
# Inspect the resulting archive
tar -tvf archive.tar.gz
drwxr-xr-x robin/robin 0 1970-01-01 01:01 source/
-rw-r--r-- robin/robin 0 1970-01-01 01:01 source/file1
-rw-r--r-- robin/robin 0 1970-01-01 01:01 source/file2
-rw-r--r-- robin/robin 0 1970-01-01 01:01 source/file3
This makes a lot of sense in a lot of use cases. But for me this was pretty annoying, when I just wanted to compress some files for sharing or as a backup.
The solution is not dead-simple, but it is easy enough and most importantly: It works.
find source/ \( -type f -o -type l -o -type d \) -printf "%P\n" | tar -caf archive.tar.gz --no-recursion -C source/ -T -
This results in the following archive:
tar -tvf archive.tar.gz
-rw-r--r-- robin/robin 0 1970-01-01 01:01 file1
-rw-r--r-- robin/robin 0 1970-01-01 01:01 file2
-rw-r--r-- robin/robin 0 1970-01-01 01:01 file3
In simple terms: The command above finds all files, directories and links in a given directory and passes them to the tar
command. Mission accomplished.