How to tar a set of files?

Li' picture Li' · Mar 3, 2013 · Viewed 60.9k times · Source

I am new to the tar command. I have many files in a directory: 1_1_1.txt, 1_1_2.txt, 2_1_1.txt, 2_1_2.txt..

I know there is a command:

tar cf file.tar *.txt

which will tar all the four files into file.tar. ButI need to tar 1_1_1.txt and 1_1_2.txt into one tar file and 2_1_1.txt and 2_1_2.txt into another tar file. How can I accomplish this?

Answer

user1019830 picture user1019830 · Mar 3, 2013

To simply create a tarball of these files I would just do:

tar cf ones.tar 1_*.txt
tar cf twos.tar 2_*.txt

Most likely you want to compress the tarballs, so use the z option:

tar czf ones.tar.gz 1_*.txt
tar czf twos.tar.gz 2_*.txt

View the contents of your tarballs with tar tf <tarball>.