How to untar all .tar.gz with shell-script?
for f in *.tar.gz
do
tar zxvf "$f" -C /path/tar
done
How to untar every type of tar file in a directory with bash script?
To get your script to work with minimal changes, use $afile
whenever you want the variable's value. The dollar sign makes a variable reference; otherwise you just get the literal string 'afile'
. Also get rid of the square brackets and instead echo
the variable to grep
.
for afile in `ls -1`; do
if echo "$afile" | grep '\.tar\.gz'
then
tar -xzf "$afile"
elif echo $afile | grep '\.tar\.xz'
then
tar -xJf "$afile"
elif echo "$afile" | grep '\.tar\.bz2'
then
tar -xjf "$afile"
else
echo "Something is wrong with the program"
fi
done
Since you're a bash beginner, let's look at various other ways you could write the script. I'd make a couple of improvements. For one, you shouldn't loop over ls
. You can get the same thing by looping over *
. Second, grep
is a heavyweight tool. You can do some simple string comparisons with built-in shell constructs like [[
and ==
.
for afile in *; do
if [[ "$afile" == *.tar.gz ]]; then
tar -xzf "$afile"
elif [[ "$afile" == *.tar.xz ]]; then
tar -xJf "$afile"
elif [[ "$afile" == *.tar.bz2 ]]; then
tar -xjf "$afile"
else
echo "Something is wrong with the program"
fi
done
Actually, this would be even nicer with a case
statement. Let's try that. Also let's echo the error message to stderr with >&2
. That's always a good idea.
for afile in *; do
case "$afile" in
*.tar.gz) tar -xzf "$afile";;
*.tar.xz) tar -xJf "$afile";;
*.tar.bz2) tar -xjf "$afile";;
*) echo "Something is wrong with the program" >&2
esac
done
We could even get rid of the error message if we just list the three types of files we want to loop over. Then there's no way to hit the else case.
for afile in *.tar.{gz,xz,bz2}; do
case "$afile" in
*.tar.gz) tar -xzf "$afile";;
*.tar.xz) tar -xJf "$afile";;
*.tar.bz2) tar -xjf "$afile";;
esac
done
Or a completely different way to do it: use find
to find all the files and its -exec
action to call a command for each file it finds. Here {}
is a placeholder for the files it finds.
find . -name '*.tar.gz' -exec tar -xzf {} \;
find . -name '*.tar.xz' -exec tar -xJf {} \;
find . -name '*.tar.bz2' -exec tar -xjf {} \;
How to untar all tar files in current directory using Putty
-xfv
is wrong since v
is being referred as the file instead. Also, tar
can't accept multiple files to extract at once. Perhaps -M
can be used but it's a little stubborn when I tried it. Also, it would be difficult to pass multiple arguments that were extracted from pathname expansion i.e. you have to do tar -xvM -f file1.tar -f file2.tar
.
Do this instead:
for F in alcatelS*.tar; do
tar -xvf "$F"
done
Or one-line: (EDIT: Sorry that -is- a "one"-liner but I find that not technically a real one-liner, just a condensed one so I should haven't referred to that as a one-liner. Avoid the wrong convention.)
for F in alcatelS*.tar; do tar -xvf "$F"; done
untar all .gz in directories and subdirectories
If I understand your question, you could use something like
DEST=<Destination Folder>
SRC=<Src Folder>
find $SRC -name "*.tar.gz" -or -name "*.tgz" -exec tar xzvvf -C $DEST {} \;
How to untar multiple files with an extension .tar.gz.aa, .tar.gz.ab..... in windows?
You are calling cat
(an alias for Get-Content
) to enumerate the contents of a single file and then attempting to pass the parsed file content to tar
. You were getting the OutOfMemoryException
due to this. Get-Content
is not designed to read binary files, it's designed to read ASCII and Unicode text files, and certainly not 10GB of them. Even if you had the available memory I don't know how performantly Get-Content
would handle single files that large.
Just pass the file path to tar
like this, adding any additional arguments you need such as controlling output directory, etc.:
tar xvzf "$name.tar.gz.aa"
You can extract all of the archives with a loop in one go (with some helpful output and result checking). This code is also 100% executable in PowerShell Core and should work on Linux:
Push-Location $pathToFolderWithGzips
try {
( Get-ChildItem -File *.tar.gz.a[a-n] ).FullName | ForEach-Object {
Write-Host "Extracting $_"
tar xzf $_
if( $LASTEXITCODE -ne 0 ) {
Write-Warning "tar returned $LASTEXITCODE"
}
}
} finally {
Pop-Location
}
Let's break this down:
$pathToFolderWithGzips
should be set to the full path to the directory containing your tarballs.Push-Location
works likecd
but uses the location stack. You can return to previous directories withPop-Location
. We change directories to the location we want to extract the files to.- Note: PowerShell Core supports the POSIX-like
cd -
andcd +
- Note: PowerShell Core supports the POSIX-like
- Wrap the rest in a
try
block so we can go back to the previous folder location after thetry
completes. ( Get-ChildItem -File *.tar.gz.a[a-n] ).FullName
enumerates all files in the current directory matching the globbing pattern, but making sure the last letter is one ofa
throughn
. Accessing theFullName
property gives us only the fully-qualified paths for each file which is all we need to pass down the pipeline.| ForEach-Object { ... }
will pipe all of the filenames from theFullName
values of the previous expression and iterate over each fully-qualified path.Write-Host
outputs information to the console via the information stream. This text is not programmatically accessible within the current PowerShell session.Write-Warning
is used further on for a similar effect but is visually distinct.- Use
Write-Output
instead if you do want the text to be processed within the same session later on, but usually we want to operate on objects over strings if we can.
- Use
$_
is an alias for$PSItem
, which is an automatic variable used for pipeline context. Every file path iterated over in theForEach-Object
loop will be referenced as$PSItem
. We pass the archive path totar
with this variable.$LASTEXITCODE
is set when the last executable finishes running. This works similarly to how$?
works inbash
(though don't confuse this for PowerShell's$?
).-ne
is the operator for "not equals"finally
is used after closing thetry
block toPop-Location
back to the previous directory. Thefinally
block is always executed *regardless of whether thetry
code succeeds or fails.- I'm admittedly not good with the
tar
executable so if you know how to control folder output without being in the current directory, you can omit thePush-Location
,Pop-Location
,try
, andfinally
bits and just run what is inside the currenttry
block, modifying thetar
command appropriately. You will also need to prefix*.tar.gz.a[a-n]
with$pathToFolderWithGzips
(e.g.$pathToFolderWithGzips\*.tar.gz.a[a-n]
) in this case too.
- I'm admittedly not good with the
How to uncompress a tar.gz in another directory
gzip -dc archive.tar.gz | tar -xf - -C /destination
or, with GNU tar
tar xzf archive.tar.gz -C /destination
How to extract a .tar.gz file on UNIX
You must either run the command from the directory your file exists in, or provide a relative or absolute path to the file. Let's do the latter:
cd /home/jsmith
mkdir cw
cd cw
tar zxvf /home/jsmith/Downloads/fileNameHere.tgz
Extracting specific folders in multiple tar.gz files recursively
Possible solution
After tinkering around with the above shell code I managed to extract only the csv folders by adding the csv wildcard command:
for f in *.tar.gz; do tar -xzvf "$f" "*csv*" -C ../synthea_output; done
The output now looks like this:
|-- output_1
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_10
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_11
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_12
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_2
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_3
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_4
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_5
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_6
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_7
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
|-- output_8
| `-- csv
| |-- allergies.csv
| |-- careplans.csv
| |-- conditions.csv
| |-- encounters.csv
| |-- immunizations.csv
| |-- medications.csv
| |-- observations.csv
| |-- patients.csv
| `-- procedures.csv
`-- output_9
`-- csv
|-- allergies.csv
|-- careplans.csv
|-- conditions.csv
|-- encounters.csv
|-- immunizations.csv
|-- medications.csv
|-- observations.csv
|-- patients.csv
`-- procedures.csv
Related Topics
Linux Desktop Shortcut and Icon from Install
Is There an Equivalent to Dbus on Osx
Can't Remove, Purge, Unistall Mongodb from Debian
Securing Udp - Openssl or Gnutls or ...
Kafka Console Consumer with Kerberos Authentication
How to Measure Mispredictions for a Single Branch on Linux
Hidden File .Env Not Copied Using Docker Copy
Git Clone Fails: Server Certificate Verification Failed
How to Diff Two Sections of The Same File
What Is The Maximum Allowed Limit on The Length of a Process Name
How to Accept Multiple Tcp Connections in Perl
What's The Purpose of Mmap Memory Protection Prot_None
Why Doesn't Tar Preserve File Permissions
How to Detect If a Server Is Using Spdy
Linux: Checking If a Socket/Pipe Is Broken Without Doing a Read()/Write()
How to Remount The /Proc Filesystem in a Docker as a R/W System