Copying all the .sh files into a tar file

Hi All ,
I need to tar all the .sh files in a server along with the path .
Please let me know is there any way this can be accomplished .
Need to grep through all the directories and sub directories in a server and tar all the .sh scripts .

This is pretty much straightforward task.

Run find and tar :

find . -name "*.sh" -type f | xargs tar -cvf sh_scripts.tar

No need for xargs, mant tar runs, just use "T - " tar option to read files to archive. An * prevents them being all prefixed with './':

find * -name "*.sh" -type f | tar -cvT - -f sh_scripts.tar

Man Page for tar (all Section 0) - The UNIX and Linux Forums

Is that a standard option or a GNU one?

Probably not for older tar's like my HPUX11.00. Solaris says -I, HP-UX 11.00 says nothing. I see tar can walk a tree, but no filter exclude/include options on old tar. Another reason to like cpio! Always took file names from stdin. Great for piping trees over the network:

find ... | cpio -o ... | bzip2 -9 | ssh somewho@somewhere 'cd target ; bunzip2 | cpio -i ...'

The compressor bzip2 multiplies the network speed and divides the ssh encryption cost. For some compress or gzip will be a better choice: less compression but faster flow.

1 Like

Another simple way.

Send find output to a file.
Then, use tar --files-from option, with the file just created.

If any question, can view the file to see what is going to be tarred.

--files-from is -T, see man page link above.

1 Like

You're right. I knew that. I just missed seeing in previous post.

Hopefully, tar does not truncate on -c else xargs requires -u or -r. The tar man page is vague, but it is easy to check. Man Page for tar (opensolaris Section 1) - The UNIX and Linux Forums

Function Letters
       The function portion of the key is specified by one  of    the  following
       letters:
 
       c
 
       Create.  Writing begins at the beginning of the tarfile, instead of
       at the end.
 
       r
 
       Replace. The named files are written at the end of the  tarfile.  A
       file  created  with    extended headers must be updated with extended
       headers (see E flag under Function Modifiers). A file created with-
       out extended headers cannot be modified with extended headers.
 
       t
 
       Table of Contents. The names of the specified files are listed each
       time they occur in the tarfile. If no file argument    is  specified,
       the    names  of  all files and any associated extended attributes in
       the tarfile are listed. With the v  function  modifier,  additional
       information for the specified files is displayed.
 
       u
 
       Update.  The  named    files are written at the end of the tarfile if
       they are not already in the tarfile, or if they have been  modified
       since last written to that tarfile. An update can be rather slow. A
       tarfile created on a 5.x system cannot be updated on a 4.x  system.
       A  file created with extended headers must be updated with extended
       headers (see E flag under Function Modifiers). A file created with-
       out extended headers cannot be modified with extended headers.
 
       x
 
       Extract  or restore. The named files are extracted from the tarfile
       and written to the directory specified in the tarfile, relative  to
       the    current  directory.  Use  the relative path names of files and
       directories to be extracted.
 
       Absolute path names contained in the tar archive are unpacked using
       the    absolute path names, that is, the leading forward slash (/) is
       not stripped off.
 
       If a named file matches a directory whose contents has been written
       to the tarfile, this directory is recursively extracted. The owner,
       modification time, and mode are restored (if possible);  otherwise,
       to restore owner, you must be the super-user. Character-special and
       block-special devices (created by mknod(1M)) can only be  extracted
       by  the  super-user.  If  no file argument is specified, the entire
       content of the tarfile is extracted. If the tarfile    contains  sev-
       eral  files    with the same name, each file is written to the appro-
       priate directory, overwriting the previous one. Filename  substitu-
       tion  wildcards  cannot  be    used for extracting files from the ar-
       chive. Rather, use a command of the form:
 
         tar xvf ... /dev/rmt/0 `tar tf ... /dev/rmt/0 | grep 'pattern' `
 
       When extracting tapes created with the r or u functions, directory mod-
       ification  times  can not be set correctly. These same functions cannot
       be used with many tape drives due to tape drive limitations such as the
       absence of backspace or append capabilities.
 
       When  using  the  r,  u, or x functions or the X function modifier, the
       named files must match exactly the corresponding files in the  tarfile.
       For  example,  to  extract ./thisfile, you must specify ./thisfile, and
       not thisfile. The t function displays how each file was archived.
 
There is always this, if the line is not too long:
 
$ tar cf xxxx.tar `find ...`

Abusing pax's substitution capability:

pax -w -s '/^.*[^h]$//' -s '/^.*[^s]h$//' -s '/^.*[^.]sh$//' -s '/^sh$//' . > sh_scripts.tar

For those who live in the real world, use find instead.

Regards,
Alister

-x xustar ?

I have never seen an implementation that does not default to ustar, but being explicit is good futureproofing.

Regards,
Alister

Man says it defaults to pax: Man Page for pax (all Section 0) - The UNIX and Linux Forums

From your link:

Perhaps you misunderstood the following:

That does not involve creating an archive. It's for internal purposes when replicating hierarchies, for example, where the pax format has fewer restrictions on pathnames than cpio and tar.

Regards,
Alister

Is pax superior to xustar, or is it pick and choose?

I had never heard of xustar until you linked that man page. Initially, I thought you had typo'd ustar.

Regards,
Alister

64 bit stuff