1. Overview

The Linux tar command is used for saving several files into an archive file. We can later extract all of the files or just the desired ones in the archive file. tar stands for “tape archive”.

tar, by default, keeps the directory structure of archived files. However, there might be cases when we want to archive files without keeping the directory structure. In this tutorial, we’ll discuss how we can achieve this goal.

2. tar Default Behavior

Let’s show that tar keeps the directory structure of archived files. We have the directory structure below:

$ tree
.
|___ directory1
|    |__ directory1.1
|    |    |_ file1.1.1
|    |    |_ file1.1.2
|    |__ directory1.2
|         |_ file1.2.1
|         |_ file1.2.2
|___ directory2
|    |__ directory2.1
|         |_ file2.1.1

5 directories, 5 files

Let’s archive the directory1/directory1.1 directory using tar:

$ tar -cf archive.tar directory1/directory1.1

Now, let’s list the contents of the archive file:

$ tar -tf archive.tar
directory1/directory1.1
directory1/directory1.1/file1.1.1
directory1/directory1.1/file1.1.2

As we can see, the created archive file keeps the directory structure. That means we’re going to have the same directory structure when we extract the contents of the archive file.

3. Usage Scenarios

We’re going to discuss several ways by examples.

3.1. Using the -C Option for a Single Directory

One way is to use the -C option of the tar command. We can envisage the operation of the tar command with the -C option as two steps:

  • Change the current directory to the one given by the -C option.
  • Archive the specified files in the new current directory.

Let’s archive directory1/directory1.1:

$ tar -cf archive.tar -C directory1/directory1.1 .

The dot (.) at the end of the tar command is important. It means that we want to archive the new current directory (i.e., directory1/directory1.1).

Now, let’s check the contents of the archive file, namely archive.tar:

$ tar -vf archive.tar
./
./file1.1.1
./file1.1.2

As we can see from the command’s output, the directory structure is not kept in the archive file.

We might also use the –directory option instead of the -C option. They are exactly the same.

3.2. Using the tar Command in the Target Directory

As the operation of tar suggests, we can just change the current directory to the target directory and archive all files in the directory:

$ cd directory1/directory1.1
$ tar -cf archive.tar .
$ tar -vf archive.tar
./
./file1.1.1
./file1.1.2

We have the same result as the one in the previous section.

3.3. Using the -C Option for Multiple Directories

What if we want to archive two or more different directories without keeping the directory structure? We can still use -C option. Let’s go on with an example. Let’s say we want to archive directories directory1/directory1.1 and directory2/directory2.1:

$ tar -cf archive.tar -C directory1/directory1.1 . -C ../../directory2/directory2.1 .

tar command in this example first changes the current directory to directory1/directory1.1. We specify it with “-C directory1/directory1.1 .. Then it archives the files in that directory. We have to specify the second directory relative to the first one. So we specify it with “*-C ../../directory2/directory2.1 .*” tar command changes the directory once more. Finally, it adds the files in that directory to the archive file. In short, the tar command processes the directories in the given order.

Let’s check the contents of the created archive file:

$ tar -tf archive.tar
./
./file1.1.1
./file1.1.2
./
./file2.1.1

Multiple current directories (./) in the output result from tar‘s operation in two different directories. It does not have any adverse effect when we extract the archive file.

3.4. Updating Archive File for Multiple Directories

When we have multiple directories to archive, we can form the archive file iteratively. We can create the archive file using the first directory and then add the other directories to the archive file one by one:

tar_file=archive.tar
dir_list="directory1/directory1.1 directory1/directory1.2 directory2/directory2.1"
tar_flag="-cf"
for dir in $dir_list
do
   tar $tar_flag $tar_file -C $dir .
   tar_flag="-rf"
done

In the above bash script snippet, tar_file is the name of the archive file to be created. dir_list contains the names of directories that we are going to archive.

When we enter the for loop for the first time, we create the archive file with the -cf option and add the files in directory1/directory1.1 to it. Then, in the successive iterations of the for loop, we use the –rf option to append the files in the remaining directories (directory1/directory1.2 and directory2/directory2.1) to the archive file.

Let’s check the contents of the archive file:

$ tar -tf archive.tar
./
./file1.1.1
./file1.1.2
./
./file1.2.2
./file1.2.1
./
./file2.1.1

As it is apparent from the output, the archive file does not include the directories.

3.5. Using the –strip-components Option

Let’s assume that we have an archive file, but it keeps the directory structure:

$ tar -tf archive.tar
directory1/
directory1/directory1.2/
directory1/directory1.2/file1.2.2
directory1/directory1.2/file1.2.1
directory1/directory1.1/
directory1/directory1.1/file1.1.1
directory1/directory1.1/file1.1.2

We can extract the contents of this archive file without the directory structure. For this purpose, we have to use –strip-components option of tar:

$ ls
archive.tar
$ mkdir tmp && cd tmp
$ tar --strip-components=2 -xf ../archive.tar
$ ls
file1.1.1  file1.1.2  file1.2.1  file1.2.2

–strip-components option expects to take a number. tar strips the given number of directories from file names when we extract the archive file. Since we give “–strip-components=2″ in our example, tar stripes directory1/directory1.1 and directory1/directory1.2 from the file paths. Therefore, it extracts the files to the current directory without the directory structure.

The syntax for –strip-components option may be a little different between tar implementations in different Linux distributions, but the basic idea is the same.

4. Conclusion

In this tutorial, we discussed several ways of creating an archive file without keeping the directory structure. We also handled the case of striping directories from full path names when we extracted archive files.