Commands.page Logo

Ubuntu Tar Extract Strip Leading Directory Command

When working with compressed files in Ubuntu, you may need to extract contents without recreating the original folder hierarchy. This guide provides the specific tar command option required to remove the top-level directory during extraction. You will learn how to use the strip components flag to organize your files efficiently directly within your current working directory.

To extract a tar archive while ignoring the first directory level, you must use the --strip-components flag. Set this option to 1 to remove the leading folder from the path structure stored inside the archive.

The basic command syntax is as follows:

tar -xzf archive.tar.gz --strip-components=1

If you are working with a plain tar file without gzip compression, omit the z flag:

tar -xf archive.tar --strip-components=1

Here is the breakdown of the command options:

Using this command ensures that all files inside the archive are placed directly into your current folder, rather than inside a new subdirectory created by the archive’s root folder.