Showing posts with label tar. Show all posts
Showing posts with label tar. Show all posts

Compressed tar archive

There are some cases when you want to create compressed tar archive but you do not have enough disk space to keep original files and tar archive or you would like to speedup the process or you do not have GNU tar (which support x,z flags for compress the tar). In such case you can use one small trick which will save you some disk space and on the same time will speedup the process because or parallel run of the commands.

Instead of run:
tar czf file.tar.gz files_to_be_archived
you can run it on this way:
tar cf - files_to_be_archived|gzip -c >file.tar.gz

This will avoid creation of temporary intermediate file and run tar and gzip in parallel. Usage of dash (-) is good practice because in UNIX OS absence of name of file archive will send the output of  command to real tape device.
You can find the original discussion in Unix StackExchange  

Tar - the game continue

Sometime, some sysadmins forget one little detail about UNIX tar and make archives with leading slash. This can be helpful when you try to restore fast some files or directories, but in most of the cases is very bad habit. In many cases it's just need to extract somewhere (but not on the original place) file and compare it with the current file. So you have archive with leading slash and want to extract something. Let do it on a little complicated, but safe way:

mkdir -p /tmp/lib
for i in `ldd /usr/bin/tar|awk  '{print $3}'`;
do
cp $i /tmp/lib
done
cp /usr/bin/tar /tmp
mkdir -p /tmp/usr/lib
cp /usr/lib/ld.so.1 /tmp/usr/lib
chroot /tmp ./tar xvf archive.tar


In the man page, I get the idea, (SUN Solaris chroot) was mentioned full list of so libraries, but I am lazy and hate to write. And want to avoid mistakes. BTW in the documentation is not mentioned ld.so library and without this library tar do not want to run :-)

P.S. The script was tested only on Solaris 10, but (depend of format of ldd) will work on other UNIXes and Linux
P.P.S. You can use pax or GNU tar but as they are not a standard in UNIX world I try to make my solution universal

Playing with tar

Every sysadmin know that tar program do not accept wildcards for extract files so if you want to extract only one or few files you should play with construction like this:
 tar xvf archive.tar `tar tvf archive.tar|grep string` 
But on complex requests will be need to do it several times and if archive reside on tape (do not forget from where come the name of utility tar) this can take long time. Instead is much better to deal with files:
tar tvf archive.tar>/tmp/somefilename 
make some filtering on the listing
tar xvf archive.tar `cat /tmp/somefilename` 
P.S. I use cat to make command work independently (almost) of version of tar, UNIX flavour or Linux

Play with compressed tar files

Time to time every system administrator or user should extract file or entire archive from compressed tar, but quite often in filesystems there is not enough free space. In this case it's possible to extract file(s) on the fly without first uncompress and then untar:

gzip -dc archive.tar.gz|tar xf -

The above line can be modified when you should deal with broken archives. If you try to decompress such archive gzip just stop when find error in compression. But you want to save what's possible. So let's make it on this way:

cat archive.tar.gz|gzip -dc|tar xf -

I know it is possible to use GNU tar to extract files from compressed archive, but in UNIX world you can't be sure this software is installed :-)

Few remarks:
  1. It's wise to use 'f -' key because in some Unixes without explicit 'f' tar will use tape device or whatever is defiined in TAPE variable
  2. In above examples beside less diskpace usage you benefit from parallel execution (from pipe)

Should I trust AI

 Should I trust AI? So far no, sorry.  I tested for the moment (May, 2025) most advanced model for programming and ask very simple question:...