//View Tip #355
Similar Tips
» Using tar with other compression formats
» bzip2 ssh copy and machine power off
» Incremental backup with tar
» network copy with ssh and tar
» Finding Newer Files [OR How To Create A Patch File]


Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
for file in `ls *.tar.gz`;do tar -xvf $file;done

for file in `ls *.tar.bz2`;do tar -xvjf $file;done

View Comments »


Add your comment

Comments are currently disabled
There is a small problem with this one .. The author uses “ls *” instead of *. Because of the way bash works, this command wil translate names with spaces into 2 different files. Ergo “This File.tgz” gets handled as “This” and “File.tgz” And this is obviously Not what we need )

You should use this one instead..
<pre>for file in *.tar.gz; do tar -xvf $file; done</pre>

Also - on a less related note.. You can replace “*.tar.gz” part to match any files and the “tar -xvf” part to match any command you want (For example echo if you want to see what files the thing is actually going to affect..) The $file part obviously gets substituted with the filename.
Posted 2008-09-17 19:43:40
good point Gert however the same happens when the variable gets expanded, so “This File.tar.gz” will be correctly processed by for but will get an error on the tar command; to avoid that the variables must be quoted:

 for file in *.tar.gz; do tar -xvf "$file"; done

this makes the expansion to appear as a single unit to bash so it avoids word splitting.
Posted 2009-02-11 17:48:24

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit