Li'
Li'

Reputation: 3183

How to tar a set of files?

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?

Upvotes: 25

Views: 70603

Answers (3)

Aromal
Aromal

Reputation: 176

You can simply use the below commands

tar cf file1.tar 1_1_1.txt 1_1_2.txt

tar cf file2.tar 2_1_1.txt 2_1_2.txt

Upvotes: 8

user1019830
user1019830

Reputation:

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>.

Upvotes: 36

iwanek
iwanek

Reputation: 368

Try this one:

find . -maxdepth 1 -name "*.txt" -exec tar -rf test.tar {} \;

If you want to include all files including subfolders, remove "-maxdepth 1"

you can use patterns like "1_1*.txt"

Upvotes: 3

Related Questions