Here's what I came up with:
First, the directory structure I'm using as an example:
$ find dir1 dir2 dir3 -print
dir1
dir1/file1
dir1/file2
dir1/sub_dir1
dir1/sub_dir1/file1
dir2
dir2/file1
dir2/file2
dir2/sub_dir1
dir2/sub_dir1/file1
dir3
dir3/file1
$
Next, my solution which consists of three commands:
First command:
tar cvf archive.tar dir1
Pretty standard. Let's break it up:
c
- Create
v
- Verbose (let's see what's going on)
f
- the filename we want, in our case archive.tar
archive.tar
- that actual argument to our f
switch
dir1
- what we want to actually tar up
Second command:
find dir2 -maxdepth 1 -type f -print0 | xargs -0 tar rvf archive.tar
Let's break it up:
Third command:
tar rvf archive.tar dir3/file1
Append to archive.tar
a single file (file1
) from dir3
.
And finally, what the tar looks like after it's created. The t
option is used to print out it's contents.
$ tar tvf archive.tar
drwxrwxr-x mmallard/mmallard 0 2019-07-18 16:02 dir1/
-rw-rw-r-- mmallard/mmallard 0 2019-07-18 16:01 dir1/file1
-rw-rw-r-- mmallard/mmallard 0 2019-07-18 16:01 dir1/file2
drwxrwxr-x mmallard/mmallard 0 2019-07-18 16:02 dir1/sub_dir1/
-rw-rw-r-- mmallard/mmallard 0 2019-07-18 16:02 dir1/sub_dir1/file1
-rw-rw-r-- mmallard/mmallard 0 2019-07-18 16:02 dir2/file1
-rw-rw-r-- mmallard/mmallard 0 2019-07-18 16:02 dir2/file2
-rw-rw-r-- mmallard/mmallard 0 2019-07-18 16:02 dir3/file1
$