2

My directory structure is as follows :

/home/workspace/build/

/home/workspace/js/jai.1.js
/home/workspace/js/jai.2.js
/home/workspace/js/jai.3.js
/home/workspace/js/jai.4.js

I want to tar all js files whose prefix is jai that are in /home/workspace/js/ directory to /home/workspace/build/ directory.

Suppose name of the tar file that is created is bundle.tar.gz

So bundle.tar.gz should contain following files

jai.1.js
jai.2.js
jai.3.js
jai.4.js

I want to achieve this using tar command instead of copying files to destination and then tar it.

EDIT

I want to tar it from destination directory i.e., /home/workspace/build/

5
(cd ../js && pax -w jai.*.js) | gzip > bundle.tar.gz

Would do it (or replace pax -w with tar cf - if you have a tar command). The point is to change the current directory only for pax/tar by using a subshell.

Some tar implementations (tar is a very non-portable command which is why POSIX introduced pax) have a -C option that makes it change directory for the file collection but not for the file output. Those also generally support a z option to call gzip by themselves or implement the gzip compression internally.

However, things like:

tar czf bundle.tar.gz -C ../js jai.*.js

wouldn't work because that jai.*.js is expanded by the shell for which the current directory has not changed. With zsh, you could do:

tar czf bundle.tar.gz -C ../js ../js/jai.*.js(:t)

Where the :t modifier gets the tail (basename) of the files generated by the glob.

With pax, you can also do:

pax -'s|.*/||' -w -- ../js/jai.*.js | gzip > bundle.tar.gz

But note that the substitution to strip the leading path components would also apply to the target of symlinks (some tar implementations have similar options where you can specify whether you want those translated or not), and would also give a different outcome if any of those jail.*.js files were of type directory and contained more files.

With libarchive bsdtar, you can also do:

bsdtar zcf bundle.tar.gz -C ../js --include=. --include='jai.*.js' .

GNU tar has no --include by has a --exclude, so you could do:

tar zcf bundle.tar.gz -C ../js --exclude='*[^s.]' \
                               --exclude='*?.' \
                               --exclude='*[^j]s' \
                               --exclude='*[^.]js' .

(those would add an entry for . though)

With star:

star czf bundle.tar.gz -C ../js 'pat=jai.*.js' .

(would include jai.foo/bar.js though, and not non-matching files in jail.*.js directories)

star czf bundle.tar.gz -C ../js 'pat=jai.#[^/].js{%!/*}' .

(where #<expr> is like <expr>* in EREs and {%!/*} like (/.*)? (% is nothing and ! is OR)) to do the same as (cd ../js && tar czf - jai.*.js) > bundle.tar.gz. It would also still crawl the entire directory structure for only selecting the files at the top level.

With newer versions, you can also use -find. So for the equivalent of our (cd ...) one that doesn't crawl into unnecessary directories but still include all the files in jai.*.js directories:

star czf bundle.tar.gz C=../js  -find . \
  \( \( -name '*.js' -o -path '*/*' -o -name . \) -o ! -prune \) ! -name .

Though more likely, you'd actually want:

star czf bundle.tar.gz C=../js \
  -find . -maxdepth 1 -name 'jai.*.js' -type f

That is archive only the jai.*.js regular files and only at the top level which with zsh you could also do with:

(cd ../js && pax -w -- jai.*.js(.)) | gzip > bundle.tar.gz

((.) being a glob qualifier that restricts to regular files).

3

No regex is required here. You can use just plain shell asterisk like that:

tar cvzf /home/workspace/build/bundle.tar.gz /home/workspace/js/jai.*.js

If you don't want to retain directory structure, then easiest way is to cd /home/workspace/js/ and then tar cvzf /home/workspace/build/bundle.tar.gz jai.*.js

  • Sir I already specified that I don't want directory structure in my tar file. I just want files. Using this command it also stores the directory in tar file – Prashant Pokhriyal Sep 28 '17 at 8:17
  • Corrected answer accordingly, Sir :) – DevilaN Sep 28 '17 at 8:28
  • But I want to tar it from /build directory not from the source directory. – Prashant Pokhriyal Sep 28 '17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.