How can I use tar command to group files without compression?

61

11

I have a very large folder of 120,000+ files. and I need to move them to another location on my same machine (same partition).

I'd like to use the tar command to group them up as a single unit and then mv them to the end location.

What command do I need to run to tar them all together and not compress anything (I need the fastest result).

sergserg

Posted 2013-01-08T15:38:20.380

Reputation: 794

@vonbrand one might want to rsync over the network one single continuous file. much faster than rsyncing 120k smaller files. it's the difference between constant full speed to highs and lows – mwm – 2018-11-29T12:19:07.030

1Why use tar, and not just mv(1) the files? Or use cp(1) if you don't want to lose the originals? – vonbrand – 2013-01-11T17:58:59.267

Answers

86

tar does not compress by default, just don't add a compression option:

tar cvf myfolder.tar myfolder

I am including Hennes' comment in my answer since it adds useful information:

TAR (tape archive) is originally a unix program used to create archives on tape. Since all devices are treated as files under unix it is easy not to write to a tape but to a file instead. This is usually done with the -f flag. The command tar cvf myfolder.tar myfolder means tar, create, verbose file filename_to_create what_to_tar. There is no compression in this anywhere. Tar archives (as files) where often compressed using the compress program and gained the extention .Z (e.g. file.tar.Z). Later on this got included in gtar with the z flag

terdon

Posted 2013-01-08T15:38:20.380

Reputation: 45 216

Note that if the fastest results are desired you should omit the "v" flag to silence output. In my case this reduced command time from 16 seconds to 6 seconds when running it on an AWS t2.medium instance. – David Backeus – 2019-09-04T07:26:04.627

Will that recursively add all subfolder and files in the tar? – sergserg – 2013-01-08T15:51:03.160

2@Serg yes, it will. – terdon – 2013-01-08T15:54:02.137

1Thanks. I usually try to answer in both short and long form. As in "yes/no it will [not] work" for the short one, and an explanation as to why as the longer answer. Having the background as to why often helps in the future. – Hennes – 2013-01-08T16:01:12.157

@Serg as it's not always the case that a manual or instructions are so good that they make things clear just straight away reading them, sometimes the best way is to go ahead and try the command anyway, then you'll see what it does, and once you know what it does or have a good idea then the manual can confirm that for you! it's easy to test tar on a small example set of files.with examples of how to use it, without having to make head or tail of the manual. – barlop – 2013-01-20T17:59:09.953

7

The @terdon answer is right.

But I made a small mistake doing tar cvf myfolder.tar.gz myfolder. I wanted same ending names for the files in the same path tar.gz even when it's not compressed.

So, if you put something like .gz, .z at the end of the filename you're trying to tar, the tar app will understand you want to use some compression, and it will apply accordingly to what you put (gz = gzip).

So if you want to use a extension like that tar.gz, make sure to use the flag --no-auto-compress

--no-auto-compress    do not use archive suffix to determine the compression 
program

Ratata Tata

Posted 2013-01-08T15:38:20.380

Reputation: 171