انتقل إلى المحتوى
أمين أبوزيد
منشورBEG-006-L08

عرض عملي: إعداد مجلد مشروع باستخدام الطرفية فقط

  • الدرس 8 من 8
  • 11 دقيقة قراءة
  • مبتدئ
في هذا الدرس 11 موضوعات

هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.

Lesson Overview

This lesson is a guided walk-through. You will use almost everything from the module in one short session: move around with paths, create folders and files, write text into a file, list files, save a listing, search it, set a variable, and finish with a planned mistake on a dummy folder, so you see how a careless delete goes wrong and how one habit prevents it.

It follows the same story as the module's lab, which asks you to set up a project using only the terminal and to hand in your command history and the final folder tree. Treat this demo as the dress rehearsal: do it once alongside the lesson, then do the lab on your own with different names.

The picture is still the literal assistant. You will give it orders in a sensible sequence: first where to stand, then what to build, then what to check, always reading before you press Enter.

You need Lessons 1 to 7. Use a practice folder, never a folder with real work in it. Commands appear in Bash and PowerShell; folder layouts and some output differ between systems, so your results may not match the sample output exactly.

Learning Objectives

By the end of this lesson you will be able to:

  • Plan a small folder structure and build it from the terminal in order.
  • Create files, write text into them with redirection, and check the result.
  • List files, sort them by size, save the listing, and search it for a word.
  • Set a session variable and use it in a command.
  • Capture your command history and show the final folder tree.
  • Explain how checking your location before a delete prevents a mistake.
  • Repeat the whole routine in either shell.

Written Explanation

The plan

A project folder for a small Java practice app might look like this. We use demo as the root, with two subfolders:

text
demo
├── src
│   └── Hello.txt
├── docs
│   └── notes.txt
└── listing.txt

The files are plain text for now, so nothing else needs to be installed. The point is the terminal skills, not the contents. You will use .txt files; real Java source files come in later modules.

The plan has six phases: stand, build, fill, inspect, search, clean up.

Phase 1: Stand

Open a terminal. Check where you are, then move to a folder that is safe for practice, such as a sandbox folder in your home folder.

Bash (Linux or macOS):

text
$ pwd
/home/ana
$ mkdir -p sandbox
$ cd sandbox
$ pwd
/home/ana/sandbox

PowerShell (Windows):

text
PS> pwd
Path
----
C:\Users\Ana
PS> mkdir sandbox
PS> cd sandbox
PS> pwd

Two checks happened: you saw where you started, and you confirmed where you ended. If sandbox already exists, mkdir complains and you just cd into it.

Phase 2: Build

Create the project and its two subfolders:

text
$ mkdir -p demo/src demo/docs
$ cd demo
$ pwd
/home/ana/sandbox/demo

PowerShell: mkdir demo/src and mkdir demo/docs (two commands, or one mkdir src, docs after cd demo), then cd demo. Then create two empty files:

text
$ touch src/Hello.txt docs/notes.txt
PS> New-Item src/Hello.txt -ItemType File
PS> New-Item docs/notes.txt -ItemType File

The two paths in the Bash command are relative to demo, which is why pwd first matters.

Phase 3: Fill

Write text into the files with redirection (Lesson 4). > starts a file fresh and >> adds:

text
$ echo "Hello from the terminal" > src/Hello.txt
$ echo "Project notes" > docs/notes.txt
$ echo "Created without a mouse" >> docs/notes.txt
$ cat docs/notes.txt
Project notes
Created without a mouse

PowerShell uses the same echo, >, and >>, and Get-Content docs/notes.txt (or cat) to read the file back.

Now set a variable for the session and use it. A variable can hold the project name:

text
$ export PROJECT=demo
$ echo "Project is $PROJECT" >> docs/notes.txt
PS> $env:PROJECT = "demo"
PS> echo "Project is $env:PROJECT" >> docs/notes.txt

Double quotes let the shell insert the variable's value. The notes file now has three lines. This variable disappears when you close the terminal, as Lesson 5 explained; the line it added to the file stays.

Phase 4: Inspect

List the project recursively, so you see the whole tree:

text
$ ls -R
PS> Get-ChildItem -Recurse -Name

ls -R lists subfolders too (the capital R means "recursive" here). Get-ChildItem -Recurse -Name prints the relative path of every item. Some systems also have a tree command that draws the layout; it may not be installed on yours. Check before you rely on it.

Now list the files in docs and src sorted by size, and save the result:

text
$ ls -lS src docs > listing.txt
PS> Get-ChildItem src, docs | Sort-Object Length -Descending > listing.txt

In Bash, ls -lS sorts by size with the biggest first. In PowerShell, the objects are sorted by their Length property. Look at the saved file:

text
$ cat listing.txt

Your sizes will differ from anyone else's, which is fine.

Phase 5: Search

Use a filter to find a word in the saved listing:

text
$ grep notes listing.txt
PS> Select-String notes listing.txt

You should see the line for notes.txt. If nothing appears, check your spelling and the capitalisation: grep is case-sensitive unless you add -i. Then try the pipe form straight from the folder, with no file in between: ls src docs | grep Hello, or Get-ChildItem src, docs -Name | Select-String Hello.

Phase 6: Save your evidence

The lab asks for two things: a command history file and the final folder tree. Capture both:

text
$ history > ../history.txt
$ ls -R > ../tree.txt
PS> Get-History > ../history.txt
PS> Get-ChildItem -Recurse -Name > ../tree.txt

They are saved in sandbox, one level above demo, so they are not part of the project they describe. A note: history in Bash prints the commands of the current session; on some systems a few of the latest commands may not yet show up in a file until the session ends. If your history looks short, run the command and look at the file before you hand it in. Get-History shows the commands of the current PowerShell session. The details can vary between systems, so check the output.

The planned mistake

Now the part you must not skip. We use two dummy folders, so nothing real is at risk. Do this only in sandbox.

text
$ cd ..
$ mkdir scratch precious
$ touch scratch/a.tmp scratch/b.tmp precious/a.tmp precious/keep.txt

(PowerShell: New-Item scratch/a.tmp -ItemType File and so on.)

You intend to clean the temporary files out of scratch. But you are standing in precious, because a previous task left you there:

text
$ cd precious
$ rm *.tmp
$ ls
keep.txt

The command ran perfectly. It deleted precious/a.tmp, not the files in scratch. No error, no warning, no recycle bin. The wildcard *.tmp meant "every .tmp file in the current folder", and the current folder was the wrong one.

Now replay it the safe way. Recreate the file, then do the three checks from Lesson 3:

text
$ touch a.tmp
$ pwd
/home/ana/sandbox/precious
$ ls
a.tmp  keep.txt

The pwd shows precious. You wanted scratch. The mistake is caught before it happens. Move, then delete:

text
$ cd ../scratch
$ pwd
$ rm *.tmp
$ ls

On PowerShell, a built-in preview adds a further layer of safety: Remove-Item *.tmp -WhatIf prints what would be deleted without deleting it. Microsoft's documentation describes the -WhatIf parameter for this purpose.

Clean up

When you are finished, remove the practice material deliberately:

text
$ cd ..
$ pwd
$ ls
$ rm -r scratch precious demo

(PowerShell: Remove-Item scratch, precious, demo -Recurse.) Keep history.txt and tree.txt if you want to show your work, and delete sandbox only when you are done with them.

Examples

Example 1: The minimal routine (simple)

The shortest useful version of the demo, in Bash:

text
$ mkdir -p tiny/src
$ cd tiny
$ echo "hello" > src/a.txt
$ ls -R

This builds a folder with a subfolder and one file, then lists it. If it prints src and a.txt, your basics work.

Example 2: Lab-style evidence trail (practical)

For the lab you want your commands visible. After building a project, you save the history and the tree, and read them back:

text
$ history > ../history.txt
$ ls -R > ../tree.txt
$ cat ../tree.txt

A reviewer can now see both what you typed and what you ended with, without watching you work. That is how professionals share a reproducible setup: send the commands, not a description of clicks.

Example 3: Catching a wrong-folder delete (realistic)

You have scratch and precious folders. You type cd to move somewhere, an interruption happens, and you come back and type rm *.tmp. Before pressing Enter you run pwd, which prints .../precious. You stop, run cd ../scratch, confirm with pwd, and only then delete. One second of checking saved a file that the shell would never have returned.

Backend Insight

Everything in this demo is what setting up a real service looks like on a server: create directories for the application, its configuration, and its logs; write or copy files into them; set variables for the service; list and search logs; and save evidence of what you did. On a server, the terminal is the only tool, and a wrong-folder delete can take a service down.

The history file has a professional name: an audit trail. When something breaks on a server, the first question is "what commands were run?" Teams also save a setup as a script, which is a text file of commands that can be run again, with the same result, on a fresh machine. What you did here by hand is exactly what such a script automates.

Common Mistakes

Mistake 1: Skipping pwd before deleting

The planned mistake showed it. Before any rm or Remove-Item, run pwd and ls.

Mistake 2: Mixing Bash and PowerShell syntax

touch in Windows PowerShell, New-Item in Bash, or export in PowerShell. Check the prompt before pasting a line.

Mistake 3: Spaces in paths without quotes

mkdir my project creates two folders. Use mkdir "my project", or better, my-project.

Mistake 4: Redirecting into the file you are reading

grep word listing.txt > listing.txt empties the file. Always write results to a different file.

Exercises

Try each exercise yourself before opening the solutions. Work in a practice folder.

Level A: Foundation

A1. Which command shows your current folder, and why do you run it before rm?

أظهر الحل A1

A1. pwd (or Get-Location). The delete acts on the current folder's contents, so you must know where you are first.

A2. What is the difference between > and >> when you write into notes.txt?

أظهر الحل A2

A2. > replaces the file's contents, >> appends and keeps what was there.

A3. Name one Bash command and one PowerShell command that list a folder recursively.

أظهر الحل A3

A3. Bash: ls -R. PowerShell: Get-ChildItem -Recurse.

Level B: Application

B1. Build this tree with a single mkdir command in Bash (or two in PowerShell): shop/src, shop/docs, shop/config. Then show the layout.

أظهر الحل B1

B1. Bash: mkdir -p shop/src shop/docs shop/config, then ls -R shop. PowerShell: mkdir shop/src, shop/docs, shop/config may work as one command in your version; if it does not, run three mkdir commands. Then Get-ChildItem shop -Recurse -Name.

B2. In shop/config, create settings.txt with the lines mode=test and port=8080 using redirection. Show the commands and the result of reading the file.

أظهر الحل B2

B2. echo "mode=test" > shop/config/settings.txt, then echo "port=8080" >> shop/config/settings.txt, then cat shop/config/settings.txt (PowerShell: Get-Content). The result is the two lines.

B3. List shop's files so that the largest comes first, save that listing to shop-listing.txt, and show only the lines that contain settings.

أظهر الحل B3

B3. Bash: ls -lS shop/src shop/docs shop/config > shop-listing.txt then grep settings shop-listing.txt. PowerShell: Get-ChildItem shop -Recurse -File | Sort-Object Length -Descending > shop-listing.txt then Select-String settings shop-listing.txt.

Level C: Challenge

C1. Write the full command sequence for the lab's acceptance: build a project folder, create at least three files, list them sorted by size, save the listing, search the listing for a word, and save your history. Say which of your commands are the same in both shells.

أظهر الحل C1

C1. Example, Bash: mkdir -p proj/src proj/docs, echo "a" > proj/src/a.txt, echo "bb" > proj/src/b.txt, echo "ccc" > proj/docs/c.txt, ls -lS proj/src proj/docs > listing.txt, grep c listing.txt, history > history.txt. The echo commands, >, and >> work the same in both shells. The listing, sorting, search, and history commands have the PowerShell equivalents in the cheat sheet.

C2. You are in an unknown folder and about to run rm -r build. List the checks you would make first, in order, and say what you would do if ls shows a build folder containing a file you did not expect.

أظهر الحل C2

C2. First pwd to confirm the folder, then ls to see build and what is next to it, then read the command aloud. If build holds something unexpected, stop: look inside with ls build, ask whether the file is needed or tracked elsewhere, and delete only the part you are sure about, or move the folder to a safe place instead of deleting it.

Mini Challenge

Redo the whole demo for a different project, such as library with books and members folders, without looking at the lesson. Use your own file names, your own variable, and your own search word.

Check your work: at the end you have history.txt and tree.txt, a listing file sorted by size, and one search result that you can explain. At least one command in your history is a pwd right before a delete.

Summary & Next Lesson

Key takeaways

  • A terminal-only setup follows an order: stand, build, fill, inspect, search, clean up.
  • Relative paths, redirection, pipes, and a session variable are enough to build and examine a project without a mouse.
  • pwd and ls before a delete is the habit that prevents wrong-folder disasters. In PowerShell, -WhatIf adds a preview.
  • Saving your history and tree creates evidence that others can follow, and it is what the lab asks for.
  • The same routine, saved in a file, becomes a script.

Next lesson

You have finished the lessons of this module. The next step is the module's lab, "Terminal-only project setup", where you repeat this routine on your own and hand in a history file and a folder tree.

Self-check

  1. In what order do the six phases go?
  2. Why was rm *.tmp dangerous in the planned mistake?
  3. Which command saves your Bash history to a file?
  4. How do you list a folder recursively in PowerShell?
  5. What should you check before every delete?
أظهر الإجابات
  1. Stand, build, fill, inspect, search, clean up.
  2. The wildcard acts on the current folder, which was the wrong one, and the delete cannot be undone.
  3. history > history.txt.
  4. Get-ChildItem -Recurse.
  5. Run pwd for the current folder and ls for what is in it.

References

  • Microsoft Learn, Remove-Item. Supports: -WhatIf shows what would happen without running the command, -Recurse for folders with contents, and the prompt when a folder with contents is removed without -Recurse.
  • Microsoft Learn, about_Redirection. Supports: > replaces file contents without warning, >> appends.
  • Microsoft Learn, about_Pipelines. Supports: Sort-Object -Property Length and piping into Select-String.
  • Microsoft Learn, about_Environment_Variables. Supports: setting $Env: variables for the current session only.
  • Not verified on an official page: ls -R, ls -lS, history behavior and when it flushes to a file, Get-History, tree availability, mkdir a, b with a comma list in PowerShell, Get-ChildItem -File, and the exact output shown in sample blocks. The text hedges these.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

المختبر والتمارين الاختبار

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.