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.
Part I — Permissions and Links
Write terminal commands to do the following:
- Create a file called
restricted.txt. - Using octal notation, set
restricted.txt’s permissions so the owner can read and write to it. - Using symbolic notation, change
restricted.txtso the owner, group, and everyone else can all read, write, and execute it. - Create a folder called
secret_files. Inside it, create a file calledfirst_secret.txtand a folder calledclassified. Insideclassified, create a file calledsuper_secret.txt. - Using octal notation, set permissions on
secret_filesso only the owner and group can read, write, and execute everything inside it. - Create a hard link to
restricted.txtcalledhard_link. - Create a symbolic link to the
classifiedfolder calledclassified_link.
Part II — Piping and Redirection
Create a text file called vegetables.txt with the following text:
Lettuce
Amaranth
Beet
Celery
Kale
Dill
Cabbage
Broccoli
Lettuce
Amaranth
Beet
Spinach
Chard
Broccoli
Cabbage
Dill
Write terminal commands to do the following:
- Sort
vegetables.txt. - Count the number of lines in
vegetables.txt. - Without using
touch, create a file calledvegetables_sorted.txtcontaining every unique vegetable fromvegetables.txt, sorted alphabetically. - Without using
touch, create a file calledlast_three.txtcontaining the last three lines ofvegetables.txt. - Using
wcandgreptogether, 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.
- Locked file test. Create
keys.txtwith some text inside, then remove every permission from it (chmod 000). Trycat keys.txtand read the error, then restore enough permission to read it again. - Editing through a hard link. Create
notes.txt, make a hard link callednotes_backup.txt, then append a new line tonotes.txtwith>>. Checkcaton both files — what do you notice, and why does it make sense given what a hard link actually is? - A link you can write through. Create a folder called
configs, make a symbolic link to it calledconfigs_link, then create a file inside the link (touch configs_link/settings.txt). Confirm the file shows up inconfigstoo. - 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. - Build and run a script. Create a file called
report.shcontaining three lines —whoami,pwd, andls— usingechoand redirects. Make it executable and run it. - 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.