20.0 File Compression
Compressing file’s is essential for saving space and making copying or downloading easier.
Preperation
To prepare to do this assignment copy and run the following command:
mkdir /root/found/; cat onthegenealogyofmorality | grep chapter > /root/found/chapterlist.txt; find / -user nietzche | grep file* | xargs cp -t /root/found/
Tar a file
To crompress the found directory we will have to use the tar command to full command will look as follows. tar -czvf /root/results.tar.gz found/
This command does the following:
-c: Create an archive.
-z: Compress the archive with gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
Perform the command:
tar -czvf /root/results.tar.gz found/
Result:
[root@rhcsa ~]# tar -czvf /root/results.tar.gz found/
found/
found/chapterlist.txt
found/file1
found/file2
found/file3
found/file4
Let’s copy this file to the directory of your colleague nietzche
cp results.tar.gz /home/nietzche/.
To check your result perform an ls:
ls -l /home/nietzche/
Result:
[root@rhcsa ~]# ls -l /home/nietzche/
total 4
-rwx------. 1 nietzche nietzche 0 Jul 27 14:44 file1
-rw-r--r--. 1 root root 203 Aug 18 10:10 results.tar.gz
Extract
Now we will unpack the file to show the files with tar -xzvf results.tar.gz
This command does the following:
-x: Extract an archive.
-z: The archive is a gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
tar -xzvf results.tar.gz
Result:
[root@rhcsa nietzche]# tar -xzvf results.tar.gz
found/
found/chapterlist.txt
found/file1
found/file2
found/file3
found/file4
Check if all your files are here.
ls -l /home/nietzche/ ; ls -l /home/nietzche/found/
Result:
[root@rhcsa nietzche]# ls -l /home/nietzche/ ; ls -l /home/nietzche/found/
total 4
-rwx------. 1 nietzche nietzche 0 Jul 27 14:44 file1
drwxr-xr-x. 2 root root 81 Aug 18 10:00 found
-rw-r--r--. 1 root root 203 Aug 18 10:10 results.tar.gz
total 0
-rw-r--r--. 1 root root 0 Aug 18 09:59 chapterlist.txt
-rwx------. 1 root root 0 Aug 18 10:00 file1
-rwx------. 1 root root 0 Aug 18 10:00 file2
-rwx------. 1 root root 0 Aug 18 10:00 file3
-rwx------. 1 root root 0 Aug 18 10:00 file4
Extraction has worked!
bzip
Let’s try to compress the files as a bzip a seperate compression method with tar -cjvf /root/result.bzip /root/found/
This command does the following:
-c: Create an archive.
-j: Compress the archive with bzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
tar -cjvf /root/result.bzip /root/found/
Result:
[root@rhcsa ~]# tar -cjvf /root/result.bzip /root/found
tar: Removing leading `/' from member names
/root/found/
/root/found/chapterlist.txt
/root/found/file1
/root/found/file2
/root/found/file3
/root/found/file4
Now we have created a bzip compressed file, check result with:
ls -l /root/
Result:
[root@rhcsa ~]# ls -l /root/
total 16
-rw-------. 1 root root 1766 Jul 3 10:23 anaconda-ks.cfg
drwxr-xr-x. 2 root root 81 Aug 18 10:00 found
-rw-r--r--. 1 root root 1921 Jul 3 10:24 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 171 Jul 27 14:44 labcheck
-rw-r--r--. 1 root root 214 Aug 18 10:19 result.bzip
-rw-r--r--. 1 root root 203 Aug 18 10:10 results.tar.gz