# Tar for files?

**URL:** <https://discourse.julialang.org/t/tar-for-files/87422>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 18, 2022, 2:47am UTC](https://discourse.julialang.org/t/tar-for-files/87422 "2022-09-18T02:47:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [September 18, 2022, 2:47am UTC](https://discourse.julialang.org/t/tar-for-files/87422/1 "2022-09-18T02:47:34Z")

</div>

Hello everyone. I really liked the idea of the Tar.jl package. Can you use them for files instead of a complete folder? I have code that generates many files, and it would be interesting to compress these files into a single tar file and also access them later.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 18, 2022, 2:30pm UTC](https://discourse.julialang.org/t/tar-for-files/87422/2 "2022-09-18T14:30:19Z")

</div>

> [@marllos](#):
>
> it would be interesting to compress these files into a single tar file

Note that a tar file isn’t _compressed_ in the sense of a zip or other file. It merely wraps the given files into a single tarball, it doesn’t try to minimize its size.

> [@marllos](#):
>
> Can you use them for files instead of a complete folder? I have code that generates many files

`Tar.create` accepts a `predicate` argument, so you can do

```julia
Tar.create(dir_containing_files, tarball_destination) do path
  basename(path) in ["myfile1", "myfile2"]
end

```

This will look in `dir_containing_files`, but only include `myfile1` and `myfile2` in the created tarball, and exclude any other files in the directory.

You can include any logic in the `do` block, so if your generated filenames have a pattern to them, you can select based on that too.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [September 19, 2022, 4:21am UTC](https://discourse.julialang.org/t/tar-for-files/87422/3 "2022-09-19T04:21:28Z")

</div>

Very good, I will use it that way. Thanks for the answer. Happy birthday!

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [September 20, 2022, 6:42pm UTC](https://discourse.julialang.org/t/tar-for-files/87422/4 "2022-09-20T18:42:29Z")

</div>

Before asking a question I always research, but sometimes it is hard to find the answer except with a direct question here on the forum. In my code, I have several Julia scripts running on the same machine and adding files to the same tar file. To do this I use the following code:

```julia
function tar_add_files(tar_file, new_file, rmf=true)
	if !isfile(tar_file)
		run(`tar -cf $tar -T /dev/null`) # create an empty tar file
	end
	run(`tar -rf $tar $new_file`) # add new_file to an existing .tar
	if rmf rm(new_file) end # remove new_file
end

scritp1.jl => tar_add_files("results.tar", "file1.data")
scritp2.jl => tar_add_files("results.tar", "file2.data")
scritp3.jl => tar_add_files("results.tar", "file3.data")
... many scripts.jl

```

1. Can you add files to an empty tarball using Tar.jl?
2. Instead of extracting a tarball, can you read the contents of the tarball into a buffer and then read it with readdlm?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 20, 2022, 7:59pm UTC](https://discourse.julialang.org/t/tar-for-files/87422/5 "2022-09-20T19:59:16Z")

</div>

> [@marllos](#):
>
> Instead of extracting a tarball, can you read the contents of the tarball into a buffer and then read it with readdlm?

> <https://github.com/JuliaIO/Tar.jl/pull/95>

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [September 27, 2022, 3:45pm UTC](https://discourse.julialang.org/t/tar-for-files/87422/6 "2022-09-27T15:45:57Z")

</div>

The two answers are complementary, as I cannot mark both as a solution, I will mark the first one. Thank you very much.
