The tar is the short form of “Tap ARchive” designed to convert the files into an archive. The tar command has various uses, extracting the tar archive, displaying the files present in the archive, and adding files to the existing archive. Sometimes, while using the “tar” utility to extract files, an error “tar not found in archive” appears in the terminal.
This post will list the possible reasons and the solutions to fix the above-stated error. The content of this guide is as follows:
- Reason 1: Not Properly Extract into Target Directory
- Solution: Extract the Tar Files Into Specified Directory
- Reason 2: Files are not Properly Archived
- Solution: Archive Files By following the Correct Syntax
Let’s start with the first reason.
Reason 1: Not Properly Extracted Into Target Directory
One of the common reasons for the error is that the user does not extract the “.tar” file properly into the specified directory. Therefore, the “tar” utility of the operating system is not executed to unzip the archive into the defined directory.
Solution: Extract the Tar Files into Specified Directory
One of the best solutions is to extract the “tar.gz” file extension specifying the file name. In our case, a “compress.tar.gz” file is considered to be extracted into the specified directory. For this, the “ls” command is utilized to display the content in the directory:
The output shows that the “compress.tar.gz” file is located in the Home directory.
Extract the Tar Files
The “tar” utility is used with “-xf” which extracts the compressed file. The “-C” specifies the collection of the multiple files that extract into the “Emp” directory:
$ tar -xf compress.tar.gz -C Folder/Emp
Check the Extracted Files
To check the uncompressed files, the “cd” command is utilized to access the specified directory and visualize content via the “ls” command as below:
$ cd Folder/Emp
You can verify that “file2.txt” and “file.txt” have been successfully extracted in the “Emp” directory.
Reason 2: Files are not Properly Archived
Another reason for this error is that multiple files are not properly compressed/archived. Therefore, some dependent files that are missing may lead to this error.
Solution: Archive Files By following the Correct Syntax
One of the possible solutions to tackle the error is to compress files properly by specifying the extension name, i.e., “tar.gz” or “tar”. The correct syntax to create a tar archive is provided below:
$ tar -czf archive.tar.gz filename
In the above syntax, the “tar” command works with the “czf” option to create the archive file by specifying the filename.
As an example, two files, “file.txt” and “file2.txt”, are compressed into the “compress.tar.gz” file that is present in the same directory:
$ tar -czf compress.tar.gz file.txt file2.txt
After successfully executing the above command, the “file.txt” and “file2.txt” have been archived into the “compress.tar.gz”.
Verify the Archive File
You can verify the archive files with the help of the “-tf” option of the “tar” command specifying the name of the file name as “compress.tar.gz” as below:
$ tar -tf compress.tar.gz
The output shows that “file.txt” and “file2.txt” are present in the desired file.
These are all the reasons and solutions to the error “tar not found in the archive”.
The error “tar not found in archive” is caused if the target directory is not properly used or the syntax of the tar command is not followed. To solve this, the users must ensure that the syntax is followed correctly alongside the target directory name and its position in the syntax. This guide has provided all possible solutions to encounter the error mentioned above.