tar -xvf example.tar --wildcards '*file1*'<\/pre>\n\n\n\nRemember to use single or double quotes around the glob expression. Otherwise, your shell might try to interpret the expression as something else before executing the command. If you get an error like this, you most likely forgot to add the quotation marks:<\/p>\n\n\n\n
tar: file1.txt: Not found in archive\ntar: Exiting with failure status due to previous errors<\/pre>\n\n\n\nExclude Specific Files<\/h2>\n\n\n\n The inverse is also true. You can exclude files when untarring. For this, we will use the –exclude<\/strong> option and extract everything except the example\/subdir2 directory:<\/p>\n\n\n\ntar -xvf example.tar --exclude=example\/subdir2<\/pre>\n\n\n\nYou can even use the *<\/strong> character (asterisk) to match multiple files. Let’s try if we can skip anything that contains file1 in it’s path:<\/p>\n\n\n\ntar -xvf example.tar --exclude=\"*file1*\"<\/pre>\n\n\n\nList Files in a Tar File<\/h2>\n\n\n\n Before you extract a tar file or specific file from it, you would need to know what is inside it. You can actually get the whole list of files without extracting them. To do this, we remove the -x option and add the -t<\/strong> option:<\/p>\n\n\n\ntar -tvf example.tar<\/pre>\n\n\n\nThis will output a file listing similar to what you would see in the output of the ls command.<\/p>\n\n\n\n <\/figure>\n\n\n\nCreate a Tar File<\/h2>\n\n\n\n Now we know how to untar an archive. Let’s attempt to create a file ourselves. To do that, we will replace the -x option with -c<\/strong>.<\/p>\n\n\n\nTo create a basic tar file, use the following command:<\/p>\n\n\n\n
tar -cvf archive_name.tar \/path\/to\/directory<\/pre>\n\n\n\n <\/figure>\n\n\n\nThe options used are the same as before. Except for the -c<\/strong> option:<\/p>\n\n\n\n\n-c: Create a new archive.<\/li>\n\n\n\n -v: Verbosely list files processed (optional).<\/li>\n\n\n\n -f: Filename for the created archive.<\/li>\n<\/ul>\n\n\n\nThis will create a basic tar file with no compression. To create a tar file with compression, we use the same compression options as when decompressing:<\/p>\n\n\n\n
For files ending with .tar.gz<\/strong> add the -z<\/strong> option:<\/p>\n\n\n\ntar -czvf file.tar.gz \/path\/to\/directory<\/pre>\n\n\n\nFor files with the .tar.bz2<\/strong> or .tar.tbz<\/strong>, lower case -j<\/strong> option:<\/p>\n\n\n\n tar -cjvf file.tar.bz2 \/path\/to\/directory<\/pre>\n\n\n\nFor files ending with .tar.xz<\/strong>, upper case -J<\/strong> option:<\/p>\n\n\n\ntar -cJvf file.tar.xz \/path\/to\/directory<\/pre>\n\n\n\nUse Untar Instead of Tar<\/h2>\n\n\n\n If you would rather not remember that the -x option stands for extract you can always create an alias so the untar command will work. Something like this would do the trick:<\/p>\n\n\n\n
alias untar='tar -xvf'<\/pre>\n\n\n\nNow you can untar a file with a much shorter command like this:<\/p>\n\n\n\n
untar example.tar<\/pre>\n\n\n\nTo make the alias permanent, just add it to your .bashrc file and then reload it:<\/p>\n\n\n\n
echo \"alias untar='tar -xvf'\" >> ~\/.bashrc\nsource ~\/.bashrc <\/pre>\n\n\n\nNow Go Do It!<\/h2>\n\n\n\n You should be able to handle most of the common tasks you would have to do with tar files. As I mentioned in the beginning, tar files are very common in Linux administration. You are also likely to have to deal with these files in all other Unix-like systems, such as MacOS, BSD, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"
“Untar” is a common term in the Linux world, referring to the process of extracting files…<\/p>\n","protected":false},"author":1,"featured_media":2163,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[47],"tags":[],"yoast_head":"\n
How to Untar a File - Linux Digest<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n\t \n\t \n\t \n