HOW TO COMBINE FILES IN A SINGLE FILE AND HOW TO COMPRESS FILE IN LINUX

In this article we are going to learn how to combine files in a single file in linux.

NOTE: i’m going to show you on text(.txt) file but you can take any file (mp4,mp3,.pdf, etc)

Why we try to combine files ? When we want our all same type of file to be found in single file.We don’t have to search for it.

Follow these steps :

  • Create files . You don’t have to create if you already have one . But i suggest you to first try this method on sample file .
[root@localhost S]# touch a.txt
[root@localhost S]# touch b.txt
[root@localhost S]# touch c.txt
[root@localhost S]# touch d.txt
  • Give these files a size.
[root@localhost S]# dd if=/dev/zero of=a.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.929067 seconds, 113 MB/s

[root@localhost S]# dd if=/dev/zero of=b.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 1.11129 seconds, 94.4 MB/s

[root@localhost S]# dd if=/dev/zero of=c.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.63839 seconds, 164 MB/s

[root@localhost S]# dd if=/dev/zero of=d.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.551127 seconds, 190 MB/s
  • Then check whether its been created or not .
[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 100M Dec 18 02:36 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:36 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:37 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:37 d.txt

 

TAR(.tar) this command helps us to combine files into single file . But its disadvantage is, it doesn’t compress file .

  • So let’s combine files that we have created, using tar command.
[root@localhost S]# tar -cvf alltxt.tar *.txt
a.txt
b.txt
c.txt
d.txt
  • alltxt.tar is file name, you can give whatever you like. for example- my.tar , usa.tar  .Just make sure after giving name of the file ,don’t forget to put .tar . (*.txt) used to select all text type of files . If you want your all your pdf file , type  *.pdf . So after entering that tar command on your files you will see alltxt.tar has been created .but you will notice it’s not compressing . 
[root@localhost S]# ls -lh
total 801M
-rw-r--r-- 1 root root 401M Dec 18 02:39 alltxt.tar
-rw-r--r-- 1 root root 100M Dec 18 02:36 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:36 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:37 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:37 d.txt
  • Then Remove the  files.We don’t want that files to take extra space in our system .
[root@localhost S]# rm -rf *.txt
  • We removed all files because that’s already in our alltxt.tar file. 
[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 401M Dec 18 02:39 alltxt.tar
  • We can also check what content alltxt.tar has .
[root@localhost S]# tar -tvf alltxt.tar
-rw-r--r-- root/root 104857600 2016-12-18 02:36:44 a.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:36:51 b.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:37:00 c.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:37:08 d.txt
  • Now if you want to use that file , we have to decompress it.
[root@localhost S]# tar -xvf alltxt.tar
a.txt
b.txt
c.txt
d.txt
  • Now you can see your file has been decompressed and files are redy to use .
[root@localhost S]# ls -lh
total 801M
-rw-r--r-- 1 root root 401M Dec 18 02:39 alltxt.tar
-rw-r--r-- 1 root root 100M Dec 18 02:36 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:36 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:37 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:37 d.txt

So that’s how we combine files into single file . But you will notice, tar command doesn’t compress file size .Don’t worry now we will learn how to compress file .

Zip(.zip)is a command that not only combine files but also compresses its size .

  • So first lets create file .
[root@localhost S]# touch a.txt
[root@localhost S]# touch b.txt
[root@localhost S]# touch c.txt
[root@localhost S]# touch d.txt
  • Give file a size . i’m giving file a size because we want to see how much zip command compress its size.
[root@localhost S]# dd if=/dev/zero of=a.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.378592 seconds, 277 MB/s

[root@localhost S]# dd if=/dev/zero of=b.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.408914 seconds, 256 MB/s

[root@localhost S]# dd if=/dev/zero of=c.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.65474 seconds, 160 MB/s

[root@localhost S]# dd if=/dev/zero of=d.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.329441 seconds, 318 MB/s
  • You can check your file will be created with the size you have provided .
[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 100M Dec 18 02:44 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt
  • Now i’m going to compress all txt files into w.zip file .You can give any name to that file for example- usa.zip , pc.zip etc just make sure after file name .zip should be there.
[root@localhost S]# zip w.zip *.txt
  adding: a.txt (deflated 100%)
  adding: b.txt (deflated 100%)
  adding: c.txt (deflated 100%)
  adding: d.txt (deflated 100%)

You can see w.zip has been created and it size is 399KB but it contains 400MB files. 

[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 100M Dec 18 02:44 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt
-rw-r--r-- 1 root root 399K Dec 18 02:47 w.zip
  • So i’m gonna delete txt file because it  has already been compressed into zip file.I’m deleting a file because i don’t want same file to take extra space in our system.
[root@localhost S]# rm -rf *.txt
  • Now if you want use that file, we can decompress it .
[root@localhost S]# unzip w.zip
Archive:  w.zip
  inflating: a.txt
  inflating: b.txt
  inflating: c.txt
  inflating: d.txt

 

GZIP/GUNZIP(.gz)is a command that also compresses but only to a particular file .

  • I’m going to try gzip command on a.txt .
[root@localhost S]# gzip a.txt
  • Now you can see a.txt is converted into a.txt.gz and 100MB file is compressed into 100KB.
[root@localhost S]# ls -lh
total 301M
-rw-r--r-- 1 root root 100K Dec 18 02:44 a.txt.gz
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt
  • Now we if want use that file just decompress it.
[root@localhost S]# gunzip a.txt.gz
  • You can see a.text is back on its size and file type.
[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 100M Dec 18 02:44 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt

BZIP2/BUNZIP2(.bz2) is a command to compress a particular file .Its bad thing is it takes time to compress but its compression procedure is stronger than gzip,zip .

  • Now i’m going to use that command on a.txt (size 100MB)
[root@localhost S]# bzip2 a.txt
  • You can see a.txt is converted into a.txt.bz2 and its size is 113 bytes 
[root@localhost S]# ls -lh
total 301M
-rw-r--r-- 1 root root  113 Dec 18 02:44 a.txt.bz2
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt
  • Now if you want to use that file we have to decompress it. After decompressing your file is ready to use.
[root@localhost S]# bunzip2 a.txt.bz2

But you all guys must be thinking gzip and bzip is powerful command but works only on a particular file . So now where combination comes into play.

TAR+GZIP(tar.gz / .tgz)  – In this gzip will take tar command help to perform compressing action on group of files. 

  • I’m gonna perform this command on all txt files . (*.txt ) is used to take all take files. all.tgz will be file containing all 4 files . But you can give any name to your file (usa.tgz , mine.tgz) just make sure in last you have to put .tgz 
[root@localhost S]# tar cvfz all.tgz *.txt
a.txt
b.txt
c.txt
d.txt
  • You can check mine 400MB files has been compressed into 398KB with name all.tgz
[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 398K Dec 18 02:55 all.tgz
-rw-r--r-- 1 root root 100M Dec 18 02:44 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt
  • Now you can delete the files that has already been compressed into all.tgz to save disk space.
[root@localhost S]# rm -rf *.txt
  • You can also check how many files does all.tgz have
[root@localhost S]# tar tvzf all.tgz
-rw-r--r-- root/root 104857600 2016-12-18 02:44:47 a.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:44:53 b.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:44:58 c.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:45:04 d.txt
  • If you want to use that file you can decompress it.
[root@localhost S]# tar xvzf all.tgz
a.txt
b.txt
c.txt
d.txt

 

TAR+BZIP2(tar.bz2/.bz2)in this bzip2 takes helps from tar command  to compress group of files.

  • So let’s start , i’m going to take all.tbZ2 but you give any name to your file but make sure after file name to put .tbz2  . And *.txt to take all text files.
[root@localhost S]# tar cvfj all.tbz2 *.txt
a.txt
b.txt
c.txt
d.txt
  • You can see my 400MB files have been compressed into 666bytes with name all.tbz2
-rw-r--r-- 1 root root  666 Dec 18 03:00 all.tbz2
-rw-r--r-- 1 root root 100M Dec 18 02:44 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt
  • Now you can delete the file that has been compressed into all.tbz2 to save disk space.
[root@localhost S]# rm -rf *txt
  • You can also check the content of all.tbz2
[root@localhost S]# tar tvjf all.tbz2
-rw-r--r-- root/root 104857600 2016-12-18 02:44:47 a.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:44:53 b.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:44:58 c.txt
-rw-r--r-- root/root 104857600 2016-12-18 02:45:04 d.txt
  • If you want to use the file that all.tbz2 contains we have to decompress it.
[root@localhost S]# tar xvjf all.tbz2
a.txt
b.txt
c.txt
d.txt
  • You can check all files are back in its original size .
[root@localhost S]# ls -lh
total 401M
-rw-r--r-- 1 root root 100M Dec 18 02:44 a.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 b.txt
-rw-r--r-- 1 root root 100M Dec 18 02:44 c.txt
-rw-r--r-- 1 root root 100M Dec 18 02:45 d.txt

So this is how you can compress file and combine files into single file.

HOW TO BREAK FILE INTO MANY FILES TO SECURE YOUR DATA

HOW TO INCREASE OR EXTEND SIZE OF LVM (LOGICAL VOLUME) IN LINUX

HOW TO OPEN AN .ISO FILE IN LINUX

4 thoughts on “HOW TO COMBINE FILES IN A SINGLE FILE AND HOW TO COMPRESS FILE IN LINUX

  1. Heya i’m for the primary time here. I found this board and I in finding
    It truly useful & it helped me out a lot. I hope to give something again and help others
    such as you helped me.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s