Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>tar</code> only knows how to process one .tar file at a time: the one specified with <code>f</code>. If you specify additional names on the command line they are interpreted as files to extract from the specified .tar file. </p> <p>Most of the answers so far do not account for unusual file names e.g., those containing spaces, newlines or other special characters).</p> <p>The correct answer is this:</p> <pre><code>for file in *.tar.gz ; do tar xzf "$file" fi </code></pre> <p>The other acceptable answer would be to use <code>find</code></p> <pre><code>find . -maxdepth 1 -type f -name '*.tar.gz' -exec tar xzf {} \;` </code></pre> <p>Why can't I...</p> <ul> <li><p>Use <code>ls *.tar.gz | ...</code> ?</p> <p>Because <code>ls</code> will clobber certain characters in file names (ie, it is not safe). If you must use <code>ls</code> for this you should be careful to use <code>ls -1</code> and then be careful that you treat newline and only newline as a record seperator. This is still not safe, but it's safer.</p></li> <li><p>Use <code>$file</code> without the quotes?</p> <p>Because a file with spaces in the name, among other things, will be interpreted in ways you do not expect. Example: A file named <code>monthly backup.tar.gz</code> will be interpreted as <code>tar xzf monthly backup.tar.gz</code> and you will get an error because the archive <code>monthly</code> does not exist.</p></li> <li><p>Use <code>echo | xargs tar</code> ?</p> <p>Because <code>echo</code> will mangle file names, specifically ones with spaces in the name. </p></li> </ul> <p>When performing a <code>while read</code> loop you almost always want to use <code>read -r</code>, which will prevent backslashes in the input from being interpreted as escape sequences. If you had a file named <code>foo\bar</code> then <code>read</code> without <code>-r</code> would not read it correctly.</p> <p>EDIT:</p> <p>Per comments, here's a method for extracting tarballs into subdirectories.</p> <pre><code>for file in *.tar.gz ; do # strip any leading path information from the file name name_only="${file##*/}" # strip the extension .tar.gz without_extension="${name_only%.tar.gz}" # make a directory named after the tar file, minus extension mkdir -p "$without_extension" # extract the file into its own directory tar xzf "$file" -C "$without_extension" done </code></pre> <p>Above I extract into directories named after the tarball. However, any scheme for making unique directory names would be sufficient. For example, here's a version that extracts to sequentially numbered directories:</p> <pre><code>let n=0 for file in *.tar.gz ; do mkdir -p "$n"; tar xzf "$file" -C "$n" let n=n+1 done </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload