Intro to the CLI Lab Exercise
Episode X: A New Terminal
A long time ago, in a Unix environment far, far away… young Jedi Padawans who knew only of desktop software were seduced by the dark side of the Force to enter:
The Terminal
Part I: mkdir and touch - set the scene
The mkdir and touch commands are foundational for managing files and directories. mkdir stands for make directory. mkdir allows you to create new directories in your file system. On the other hand, touch is used for creating new files.
Both commands provide a quick and straightforward way to set up your project’s structure.
Practice:
-
At the start of this lab, you should already be in a directory called
star-wars. -
Create a new directory here called
death-star. -
Create the following files inside of it:
darth-vader.txt,princess-leia.txt, andstorm-trooper.txt. -
Make another directory in
star-warscalledgalaxy-far-far-away. -
Inside the
galaxy-far-far-awaydirectory, make a new directory namedtatooineand create the following files inside it:luke.txtandben-kenobi.txt. -
Inside the
tatooinedirectory, make a directory calledmillenium-falcon, and inside it, createhan-solo.txtandchewbacca.txtfiles.
Part II: cp - copy
The cp command is used for copying files and directories. Short for copy, this command lets you duplicate files from one location to another.
For example, cp source.txt destination.txt would copy the contents of source.txt into a new file called destination.txt. It’s a versatile tool often used in scripts and batch operations. While cp is powerful and efficient, it’s crucial to use it carefully to avoid accidental overwrites of the destination file.
💡 Check the manual: Run
man cpto see the options — hit theQkey to get out of the manual page viewer.
Practice:
- Copy
storm-trooper.txtfrom thedeath-stardirectory to thetatooinedirectory.
Part III: mv - move
The mv command is your go-to tool for moving and renaming files and directories. Short for move, this command allows you to take a file from one location and place it into another. But it’s more versatile than that; you’ll see why soon.
You could use mv myfile.txt /new/directory/ to move a file from one directory to another.
💡 Check the manual: Run
man mvto see the options —hit theQkey to get out of the manual page viewer.
Practice:
-
Move
luke.txtandben-kenobi.txtto themillenium-falcondirectory. -
Move the
millenium-falcondirectory out of thetatooinedirectory and into thegalaxy-far-far-awaydirectory. -
Move the
millenium-falcondirectory into thedeath-stardirectory. -
Move
princess-leia.txtinto themillenium-falcondirectory.
Part IV: mv - rename
You can also use mv to rename files. This is accomplished by moving the file from one name to another within the same directory.
Using the command mv oldfile.txt newfile.txt will rename oldfile.txt to newfile.txt.
Practice:
- Rename
ben-kenobi.txttoobi-wan.txt.
Part V: rm - remove
The rm command stands for remove and deletes files and directories. To delete a file, you’d type rm filename. To delete a directory and all the files inside of it, you’d use rm -r directoryname.
While the rm command is efficient for cleaning up, it comes with a caution: deleted items don’t go to a recycle bin— they’re permanently removed. So before executing this command, make sure you want to delete the target permanently.
Practice:
- Delete
obi-wan.txt.
Part VI: All together
The mkdir, touch, cp, mv, and rm commands are essential tools for file and directory management in the command line.
mkdircreates directoriestouchcreates filescpcopies files or directoriesmvmoves or renames themrmdeletes them
Let’s try these all together!
Practice:
-
In the
galaxy-far-far-awaydirectory, make a directory calledyavin-4. -
Move the
millenium-falcondirectory out of thedeath-stardirectory and into theyavin-4directory. -
Make a directory in the
yavin-4directory calledx-wing. -
Move
princess-leia.txtto theyavin-4directory andluke.txtto thex-wingdirectory. -
Move the
millenium-falcondirectory and thex-wingdirectory out of theyavin-4directory and into thegalaxy-far-far-awaydirectory. -
In the
death-stardirectory, create directories fortie-fighter-1,tie-fighter-2, andtie-fighter-3. -
Move
darth-vader.txtinto thetie-fighter-1directory. -
Make a copy of
storm-trooper.txtinto both thetie-fighter-2andtie-fighter-3directories. -
Move all of the
tie-fightersdirectories out of thedeath-stardirectory and intogalaxy-far-far-awaydirectory.
Part VII: rm -r: Remove directories and everything they contain
Remember, this command will not ask you if you really want to delete the directory and its files. It will just delete.
Once you hit Enter, its gone forever.
Always make sure you are deleting the right thing, or you could accidentally delete things you don’t intend to delete.
Practice:
- Remove the
tie-fighter-2andtie-fighter-3directories.
Part VIII: May the Force be with you
Let’s wrap up this story properly.
Practice:
-
Touch a file in the
x-wingdirectory calledthe-force.txt. -
Destroy the
death-stardirectory and anyone inside of it. -
Return the
x-wingandmillenium-falcondirectory to theyavin-4directory.
Part IX: Clean up
-
Navigate to your
~/code/ga/labsdirectory:cd ~/code/ga/labs -
Remove the
star-warsdirectory and all its contents.
Like it never happened!