Linux on call · Chapter 12 of 30
Archives and compression
By the end of this chapter you can pack a folder into one compressed file, look inside it without unpacking, open a zip someone sent you, and shrink a big file you must keep. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Files and text
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Kabir wants a backup of the app before tonight's deploy: just the code. Not node_modules (npm install brings it back) and not the logs. Meera has sent the new menu as incoming/menu.zip, and Ravi asks whether September's log can be squeezed: nobody reads its 133893 bytes, but it has to be kept.
You know cp -r, but a copy of app is still a folder of many files, and it takes everything: du -sh app/* shows node_modules (76K) and logs (60K) are most of the app. Bunty's last backup took everything too, including the backup before it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Back up the app in backups/app.tar.gz, without node_modules and logs
Back up the app's code in one compressed file, and leave out what you do not need.
1. Run du -sh app/* to see what is in the app folder.
2. Use tar to make backups/app.tar.gz from the app folder. Compress it with gzip (the z option).
3. Leave out node_modules and logs with --exclude. Put each --exclude before the folder name app.
4. If the archive is wrong, delete it and make it again.
To pass: backups/app.tar.gz is a gzip-compressed tar archive. It has the app's code (server.js, package.json, src/menu.js, public/index.html). It has nothing from node_modules or logs, and no .log file.
- Task 2
Look inside the backup without unpacking it
Check your backup: list what is inside it, without unpacking it.
1. Make the backup first (task 1).
2. Use tar with its list option, t, on backups/app.tar.gz.
3. Read the list. Make sure nothing from node_modules or logs is in it.
To pass: you ran tar with t (or --list) on backups/app.tar.gz in the chapter folder, and it worked. A command with a typing mistake or an error does not count, so type it again.
- Task 3
Unpack Meera's menu.zip into a folder called menu
Unpack Meera's zip file into a new folder called menu.
1. Run unzip -l incoming/menu.zip. It lists the files inside, without unpacking.
2. Unpack it into the folder menu, in the chapter folder. unzip -d names the folder.
3. Run ls menu to see the files.
To pass: menu/menu.txt and menu/prices.csv are there, straight inside menu (not one folder deeper). incoming/menu.zip is still in its place.
- Task 4
Compress September's log with gzip
Make September's log smaller, but keep all of it.
1. Compress old-logs/september.log with gzip. gzip replaces the file with september.log.gz.
2. Do not use -k. The original file should be gone.
3. Count the lines without unpacking: zcat, then wc -l.
To pass: old-logs has september.log.gz, a gzip file with all 5000 lines, and old-logs/september.log is gone.
What you will learn
- tar: many files and folders packed into one archive, gzipped on the way, with some folders left out.
- tar -t: list what is inside an archive without unpacking it.
- unzip: look inside a zip and unpack it into a folder you choose.
- gzip: compress one file in place, and read it without unpacking.
More in Files and text
Build "Archives and compression" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.