Tuesday, November 27, 2012

compressing in Linux : the 'tar' command

I find  the 'tar' command very useful for my work especially when I work with lots of files and maintain lot of versions of code. The man page of tar is huge and I am sure there are many more flavours to it. However, I always find it confusing...there are certain customizations of 'tar' that come handy to me often and here they are...

$tar -cvzf filesDir.tgz  filesDir/
Compress all files in directory filesDir to filesDir.tgz

Suppose, you edit only a few files of a huge directory with lots of sub-folders, then you would want to save only those few files.

In such case you should maintain a fileList which contains all filenames with relative path to the parent folder.

$cat fileList.txt
file1
file2
subdir1/subdir2/file3
subdir3/file4
subdir4/


$tar -cvzf filesDir.tgz -T fileList.txt
This will compress  the files mentioned in fileList.txt. Note that subdir4/ is an entire folder. In this case, we are compressing all files contained in subdir4/

Now, comes the uncomressing part.

Go to the required path.
$ mkdir filesDir
$cd filesDir
filesDir]$ tar -xvzf filesDir.tgz


In case when you want to merge this data into another version of the same library/code, you can go into that directory and simply untar as above. The corresponding files in respective paths will get replaced by the files in filesDir.tgz, and new files and folders will be added.


Before untarring, we may want to check the difference to make sure that we are doing the right thing.


Here are the commands:
$diff -r dir1/ dir2/
This will display all the differences in two directories and their contents recursively (-r).


If you want to just see the files which correspond to changes, not the contents, then
$diff --brief -r dir1/ dir2/

No comments:

Post a Comment