CodingNic

Permissions, Redirection and Piping

Permissions, Redirection, and Piping Exercise

Permissions, Redirection and Piping 20 min read

Permissions, Redirection, and Piping Exercise

Objectives

This chapter introduces no new commands — it’s a chance to put Module 2 together: permissions, links, redirection, and piping, all in one pass.

Write terminal commands to do the following:

  1. Create a file called restricted.txt.
  2. Using octal notation, set restricted.txt’s permissions so the owner can read and write to it.
  3. Using symbolic notation, change restricted.txt so the owner, group, and everyone else can all read, write, and execute it.
  4. Create a folder called secret_files. Inside it, create a file called first_secret.txt and a folder called classified. Inside classified, create a file called super_secret.txt.
  5. Using octal notation, set permissions on secret_files so only the owner and group can read, write, and execute everything inside it.
  6. Create a hard link to restricted.txt called hard_link.
  7. Create a symbolic link to the classified folder called classified_link.

Part II — Piping and Redirection

Create a text file called vegetables.txt with the following text:

text
Lettuce
Amaranth
Beet
Celery
Kale
Dill
Cabbage
Broccoli
Lettuce
Amaranth
Beet
Spinach
Chard
Broccoli
Cabbage
Dill

Write terminal commands to do the following:

  1. Sort vegetables.txt.
  2. Count the number of lines in vegetables.txt.
  3. Without using touch, create a file called vegetables_sorted.txt containing every unique vegetable from vegetables.txt, sorted alphabetically.
  4. Without using touch, create a file called last_three.txt containing the last three lines of vegetables.txt.
  5. Using wc and grep together, count how many lines contain the word “Broccoli”.

Part III — Challenges

These lean harder on combining permissions, links, redirection, and piping in one line rather than one at a time.

  1. Locked file test. Create keys.txt with some text inside, then remove every permission from it (chmod 000). Try cat keys.txt and read the error, then restore enough permission to read it again.
  2. Editing through a hard link. Create notes.txt, make a hard link called notes_backup.txt, then append a new line to notes.txt with >>. Check cat on both files — what do you notice, and why does it make sense given what a hard link actually is?
  3. A link you can write through. Create a folder called configs, make a symbolic link to it called configs_link, then create a file inside the link (touch configs_link/settings.txt). Confirm the file shows up in configs too.
  4. One-line pipeline. Create a file with a handful of repeated numbers, one per line. In a single line, sort it, remove duplicates, count how many unique numbers remain, and save that count to a file called unique_count.txt.
  5. Build and run a script. Create a file called report.sh containing three lines — whoami, pwd, and ls — using echo and redirects. Make it executable and run it.
  6. Clean sweep. Remove everything you created in this section.

Recap

You can now read and change permissions, manage ownership, create links, redirect output into and out of files, and chain commands together with pipes. That’s the toolkit Module 2 set out to build.

Next module: environment variables, the PATH, and finding and managing running processes.