{"id":67,"date":"2012-02-20T15:32:00","date_gmt":"2012-02-20T15:32:00","guid":{"rendered":"http:\/\/pheonixsolutions.com\/?p=67"},"modified":"2026-07-08T13:22:36","modified_gmt":"2026-07-08T07:52:36","slug":"script-to-grep-a-word-inside-a-tar-file","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/script-to-grep-a-word-inside-a-tar-file\/","title":{"rendered":"How to Search for a Word Inside a TAR Archive Using a Shell Script"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>TAR (Tape Archive)<\/strong> file is commonly used on Linux and Unix systems to bundle multiple files into a single archive. There may be situations where you need to search for a specific word or string inside the files contained within a TAR archive without manually extracting the entire archive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following shell script iterates through each file in a TAR archive, extracts it temporarily, searches for a specified string using <code>grep<\/code>, displays any matching lines, and restores any existing files with the same name.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before running the script, ensure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Linux or Unix-based system.<\/li>\n\n\n\n<li>A TAR archive (for example, <code>one.tar<\/code>).<\/li>\n\n\n\n<li><code>tar<\/code> and <code>grep<\/code> utilities installed.<\/li>\n\n\n\n<li>Read permission for the archive and write permission in the working directory.<\/li>\n\n\n\n<li>Basic knowledge of shell scripting.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Script<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">search=\"123\"\n\ntar -tf one.tar | while read filename\ndo\n    if [ -f \"${filename}\" ]\n    then\n        mv \"${filename}\" \"${filename}.sav\"\n    fi\n\n    tar -xf one.tar \"${filename}\"\n\n    found=$(grep -l \"${search}\" \"${filename}\")\n\n    if [ ! \"${found}X\" = \"X\" ]\n    then\n        echo \"Found \\\"${search}\\\" in \\\"${found}\\\"\"\n        grep \"${search}\" \"${filename}\"\n        echo \"\"\n    fi\n\n    if [ -f \"${filename}.sav\" ]\n    then\n        mv \"${filename}.sav\" \"${filename}\"\n    fi\ndone\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How the Script Works<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Define the Search String<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Specify the word or phrase you want to search for.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">search=\"123\"\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: List Files in the TAR Archive<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The script lists all files contained in the archive.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tar -tf one.tar\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Preserve Existing Files<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If a file with the same name already exists in the current directory, it is renamed with a <code>.sav<\/code> extension to prevent it from being overwritten.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Extract Each File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each file is extracted individually from the TAR archive.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tar -xf one.tar \"${filename}\"\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Search for the Specified Word<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The script uses <code>grep<\/code> to check whether the extracted file contains the search string.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">grep -l \"${search}\" \"${filename}\"\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If a match is found, the filename and matching lines are displayed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Restore Original Files<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If a backup file was created, it is restored after the search is completed.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This shell script provides a simple way to search for a specific word or string within files stored in a TAR archive. It extracts files one at a time, searches their contents using <code>grep<\/code>, and restores any existing files after processing. This approach is useful for inspecting archive contents without manually extracting the entire archive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction A TAR (Tape Archive) file is commonly used on Linux and Unix systems to bundle multiple files into a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1020],"tags":[211,210,212],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-containers-kubernetes","tag-grep","tag-script","tag-zip","psol-cat-containers-kubernetes"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-15","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":10495,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions\/10495"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}