Pages

Tuesday, December 31, 2013

Command to tar multiple sub-folders (Linux)

Hello,

  I am  not a scripting expert but I tried this below 1 line script by adding many parts and it did my work so I  thought of sharing with all.

This script will tar multiple sub-folder base on the number giving by the user in the script.



 #head -num File_with_list.txt | tr '\n' '\0' | tar --null -uf /folder_destination/name.tar -T -


Where,

 -num is -50 or -100 or any number of folders you want to tar in single .tar file
  File_with_list.txt is a file which has a list of folders which you want to tar
    you can get a list of folders in a file by using following command
   
 ls -l > file_with_list.txt

    /folder_destination is the path where you want to save your tar file
    name.tar is the name of the tar file
    -T -   is the bit which provides the input to the tar with the folder names

so in short..

  The above command will take the input from the head command and piped with \n for new line at the end of line and then \0 for null characters,  this is then again piped to tar which is feeded by STDIN with the help of -T - bit at the end.

 If you want to archive/tar the folder in an alphabetical order. then sort the name in the file and then run the script.
to sort the name in the file use the command
:%!sort 



All the Best ........ :)
Cheers!

No comments:

Post a Comment