I am fairly new using tar having used zip files in the past.
I want to just create the tar file without the directories being included.
I went here, but found it a bit confusing.
https://www.baeldung.com/linux/tar-archive-without-directory-structure
# Backup crontab
crontab -l > /home/andy/bin/crontab_backup.txt
tar -cvf /home/andy/bin/crontab_backup.tar /home/andy/bin/crontab_backup.txt
For your specific example, if you
cd
to the/home/andy/bin
and omit that path in the commands, you'll get the desired result:This is the suggestion in 3.2 Using the tar Command in the Target Directory in the page you linked to and is probably the simplest way for this particular problem.
The equivalent of 3.1. Using the -C Option for a Single Directory would be:
There is another option that's not mentioned in the linked page:
--transform
, which allows us to use as/<pattern>/<replacement>/
expression like insed
.With GNU
tar
you can use the-C
option to change directory partway through the command. Filenames are still recorded relative to the current directory, so it means so you can do this:and if you wanted to collect two files from different directories, you could do this:
The
tar
command really does not have options to not store the directory structure, in line with the linux philosophy of "do one thing and do it well".The page you refer to provides some trick to store files without the directory structure. However, these trick are very limited in their power, and require you to specify each lowest level directory that contains files. The trick will already fail if there also are files in directories in the middle of the tree. Tarring these will include the lower directories.
As such, it is not possible in a fully automated way. A more automated way would require preparing a directory containing all files from the directory tree at the top level directory first, then tar that directory. On a file system that supports linux permissions, that could be done using hard links, so no extra space is needed for the temporary copy.
You could do so with
find
:then you can tar the directory
dir
in/path/to/new
.