Practice Exercise: Working with Large and Compressed Files
Objective
Gain practical experience in working with large text files and compressed files in a Linux environment.
Task 1: Generating a Large Text File
- Open a terminal window.
- Use the
seqcommand to generate numbers from 1 to 100,000, and redirect the output to a file namedlarge_file.txt. - Check the size of the
large_file.txtusing thelsorducommand.[intern@intern-a1t-inf-lnx1 ~]$ seq 1 100000 > large_file.txt [intern@intern-a1t-inf-lnx1 ~]$ du -h large_file.txt 576K large_file.txt [intern@intern-a1t-inf-lnx1 ~]$ ls -lh large_file.txt -rw-rw-r-- 1 intern intern 576K Sep 19 15:35 large_file.txt
Task 2: Viewing Large Files
- Attempt to view the entire
large_file.txtusing thecatcommand. - Observe the challenges of viewing such a large file.
- Use the
headcommand to display the first 10 lines oflarge_file.txt. - Use the
tailcommand to display the last 10 lines oflarge_file.txt.[intern@intern-a1t-inf-lnx1 ~]$ cat large_file.txt ..... 99997 99998 99999 100000 [intern@intern-a1t-inf-lnx1 ~]$ cat large_file.txt | head 1 2 3 4 5 6 7 8 9 10 [intern@intern-a1t-inf-lnx1 ~]$ cat large_file.txt | tail 99991 99992 99993 99994 99995 99996 99997 99998 99999 100000
Task 3: Compressing and Decompressing Files
- Compress the
large_file.txtusing thegzipcommand, creating a file namedlarge_file.txt.gz. - Verify the size reduction of the compressed file compared to the original.
- Decompress
large_file.txt.gzto its original state using thegunzipcommand.[intern@intern-a1t-inf-lnx1 ~]$ gzip large_file.txt [intern@intern-a1t-inf-lnx1 ~]$ ls -lh large_file.txt.gz -rw-rw-r-- 1 intern intern 211K Sep 19 15:35 large_file.txt.gz [intern@intern-a1t-inf-lnx1 ~]$ gunzip large_file.txt.gz [intern@intern-a1t-inf-lnx1 ~]$ ls -lh large_file.txt -rw-rw-r-- 1 intern intern 576K Sep 19 15:35 large_file.txt
Task 4: Working with Compressed Files
- Compress the
large_file.txtagain. - Use the
zcatcommand to view the contents oflarge_file.txt.gzwithout decompressing it. - Extract the contents of
large_file.txt.gzusing thezcatcommand into a new file namedextracted.txt.[intern@intern-a1t-inf-lnx1 ~]$ gzip large_file.txt [intern@intern-a1t-inf-lnx1 ~]$ zcat large_file.txt.gz | tail 99991 99992 99993 99994 99995 99996 99997 99998 99999 100000 [intern@intern-a1t-inf-lnx1 ~]$ zcat large_file.txt.gz > extracted.txt [intern@intern-a1t-inf-lnx1 ~]$ ls -lh extracted.txt -rw-rw-r-- 1 intern intern 576K Sep 19 15:49 extracted.txt
Conclusion
In this lab exercise, you've gained hands-on experience in working with large text files and compressed files in a Linux environment. You've learned how to generate large text files, view the contents of large files efficiently using head and tail, compress and decompress files using gzip and gunzip, and work with compressed files using zcat. These skills are valuable for handling large datasets and efficiently managing disk space.