In this article, we will show you how to add two files to the same archive, but at different paths. This is a useful technique if you want to keep the files organized in the archive, just like they are in your file system. We will be using the tar command in the examples, but the technique can be applied to other archive formats as well.
Prerequisites
Before we start, make sure you have the following prerequisites:
- Two or more files that you want to add to the same archive
- The
tarcommand installed on your system. If you are using a Unix-like operating system, such as Linux or MacOS, it should already be installed.
Creating the Archive
To create an archive with two files at different paths, you can use the following command:
tar -cvf archive.tar --transform='s/^/path1/' file1.txt --transform='s/^/path2/' file2.txt
Let's break down the command:
tar: the archive command-c: create a new archive-v: verbose output, shows the progress of the command-f archive.tar: the archive file name--transform='s/^/path1/' file1.txt: transform the file name offile1.txttopath1/file1.txtin the archive--transform='s/^/path2/' file2.txt: transform the file name offile2.txttopath2/file2.txtin the archive
This command will create an archive named archive.tar with two files, path1/file1.txt and path2/file2.txt.
Adding More Files to the Archive
To add more files to the archive at different paths, you can use the following command:
tar -rvf archive.tar --transform='s/^/path3/' file3.txt --transform='s/^/path4/' file4.txt
This command will add path3/file3.txt and path4/file4.txt to the existing archive archive.tar.
Extracting the Files
To extract the files from the archive, you can use the following command:
tar -xf archive.tar
This command will extract all the files from the archive to the current directory. If you want to extract the files to a different directory, you can use the -C option:
tar -xf archive.tar -C /path/to/directory
This command will extract all the files to the directory /path/to/directory.
In this article, we showed you how to add two files to the same archive, but at different paths. This is a useful technique if you want to keep the files organized in the archive, just like they are in your file system. We used the tar command in the examples, but the technique can be applied to other archive formats as well.
References
| Title | Author | Date | URL |
|---|---|---|---|
| tar(1) - Linux man page | Linux man-pages project | 2021-02-09 | https://man7.org/linux/man-pages/man1/tar.1.html |
| tar - Archive and compress files | GNU coreutils | 2021-02-09 | https://www.gnu.org/software/coreutils/tar/ |