Score:3

Creating tar files without including the directories

kp flag

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
cn flag
What do you expect to happen if 2 files with the same name in 2 folders are in that tar file?
fixit7 avatar
kp flag
@Rinzwind Where do you get that I have 2 files with the same name? As far as I can see, crontab -l is creating a single file.
pk flag
You should be aware that a tar of one file isn't terribly useful. It's just the original file, with a header attached to say that yes, it's one file. It's not even compressed.
cn flag
@fixit7 you want dirs not in the tar file so /1/2/3/file /1/2/4/file will be a problem.
kero avatar
id flag
Are you sure you're not looking for [`gzip`](https://linuxize.com/post/gzip-command-in-linux/) instead? As mentioned, tar of a single file is doing mostly nothing beneficial
Score:8
us flag

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:

# Backup crontab
cd /home/andy/bin
crontab -l > crontab_backup.txt
tar -cvf crontab_backup.tar crontab_backup.txt

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:

crontab -l > /home/andy/bin/crontab_backup.txt
tar -cvf /home/andy/bin/crontab_backup.tar -C /home/andy/bin crontab_backup.txt

There is another option that's not mentioned in the linked page: --transform, which allows us to use a s/<pattern>/<replacement>/ expression like in sed.

crontab -l > /home/andy/bin/crontab_backup.txt
tar -cvf /home/andy/bin/crontab_backup.tar /home/andy/bin/crontab_backup.txt --transform 's,.*/,,'
Score:4
pk flag

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:

tar -cvf /home/andy/bin/crontab_backup.tar -C /home/andy/bin crontab_backup.txt

and if you wanted to collect two files from different directories, you could do this:

tar -cvf /home/andy/bin/crontab_backup.tar -C /home/andy/bin crontab_backup.txt -C /home/andy other_file.txt
Raffa avatar
jp flag
How is this different from "The equivalent of 3.1. Using the -C Option for a Single Directory would be:" in @muru answer?
Score:2
cn flag

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:

find /path/to/parent/dir -type f -exec ln {} /path/to/new/dir \;

then you can tar the directory dir in /path/to/new.

fixit7 avatar
kp flag
Thanks, I will look for another program that compresses files and keeps file permissions.
oakad avatar
gr flag
" really does not have options to not store the directory structure" - tar simply appends files to an archive "stream". Thus, a simple combo of `find` + `tar -r` will do just that: create an archive without any directory structure whatsoever. You can even add the same file with the same name arbitrary number of times. :-)
vanadium avatar
cn flag
@oakad the question here is indeed "how". None of the answers here seems to present the more obvious solution that may be there, so do not hesitate to post an answer: I will upvote and delete mine in that case.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.