Skip to content

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 seq command to generate numbers from 1 to 100,000, and redirect the output to a file named large_file.txt.
  • Check the size of the large_file.txt using the ls or du command.
    [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.txt using the cat command.
  • Observe the challenges of viewing such a large file.
  • Use the head command to display the first 10 lines of large_file.txt.
  • Use the tail command to display the last 10 lines of large_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.txt using the gzip command, creating a file named large_file.txt.gz.
  • Verify the size reduction of the compressed file compared to the original.
  • Decompress large_file.txt.gz to its original state using the gunzip command.
    [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.txt again.
  • Use the zcat command to view the contents of large_file.txt.gz without decompressing it.
  • Extract the contents of large_file.txt.gz using the zcat command into a new file named extracted.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.