6

I have a lot of filenames that listed in a .txt file called to_be_archived_files.txt, and its content is as:

~/Documents/dir1/a.html
~/Documents/dir1/b.html
~/Documents/dir1/c/1.html

How do I add all of them into a archive.tar.gz by tar?

I want something like:

tar czf archive.tar.gz -FROM-FILELIST=to_be_archived_files.zip

How can I achieve that?

10

Use the -T option:

 -T, --files-from FILE
       get names to extract or create from FILE

Note that it doesn't work with ~ as alias for home directory, you need to specify the folder explicitly.

  • awesome. i never tought it was so easy – AGamePlayer Aug 27 '15 at 9:38
7

You'd need to replace that ~ with your home directory first. Assuming your home directory doesn't contain : (they generally don't as otherwise they couldn't be referenced in /etc/passwd):

(cd / &&
  sed "s:^~:$HOME:" | pax -w | gzip) < file.list > file.tar.gz

Or if your tar is the one from GNU or bsdtar:

(cd / &&
  sed "s:^~:$HOME:" | tar -T - -zcf -) < file.list > file.tar.gz

Some other tar have -I instead of -T for that. star uses a list=- argument for that (star cvf - list=-).

Note that in those solutions above, except for star, if any of the files in that list are of type directory, then the whole content of those directories is included.

To archive the directory only and not its content, with pax add the -d option, with GNU tar or bsdtar the --no-recursion one (also -n with bsdtar).

With star, to include the content of the directories, add the -dodesc option.

  • Hint: You need -dodesc because at the time of introduction if list= (around 1985) list= was used for find | star. Since 10 years, there is a built-in find in star that makes things much easier. – schily Aug 27 '15 at 14:43

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.