Working with Files and Folders
Objectives
By the end of this chapter, you should be able to:
- Create files and folders using
mkdirandtouch - Move files and folders using
mv - Copy files and folders using
cp - Remove files and folders using
rmandrmdir - Explain what a flag is
- Explain what
whoami,cat,echo,less, andopendo
๐ก Why this matters: Right now you can only look around your filesystem. This lesson turns Terminal into a tool you actually build things with โ the same handful of commands you’re about to learn are how developers scaffold every project, from a single script to a full application.
Who Am I, Again?
Quick refresher before diving in: you already met pwd last lesson for checking where you are. Two more commands answer equally simple questions.
whoami prints the username you’re currently logged in as. echo prints text back to you โ type echo hello and Terminal prints hello. Small on their own, but you’ll lean on both constantly once you start scripting.
Creating Files and Folders
To make a folder, use mkdir (short for “make directory”) followed by a name. Head to your Desktop and create one:
cd ~/Desktop
mkdir first_folder
Move into it the same way you learned last lesson โ cd first_folder if you’re already on the Desktop, or the full path from anywhere else.
To create a file, use touch, which makes an empty file:
touch first_file
Run ls to confirm it’s there. touch doesn’t put anything inside the file โ that’s next.
Seeing Inside a File
cat prints a file’s contents straight into Terminal. Try cat first_file โ nothing prints, because the file is empty. Add some text with a redirect (more on these next lesson):
echo "Hello World" > first_file
echo writes text; > sends that text into the file on the right instead of onto the screen. Run cat first_file again and you’ll see it.
For longer files, less is friendlier than cat โ it lets you scroll and search instead of dumping everything at once. Try less first_file, then press q to exit.
Opening a File the Normal Way
Sometimes you just want a file open in whatever app your system would normally use. open first_file does that (on Windows, the equivalent is start). open . opens the current folder in your file manager โ handy when you want to switch from Terminal back to clicking around.
Moving Files and Folders
mv moves โ or renames โ a file or folder. Make a test file on the Desktop and move it into first_folder:
cd ~/Desktop
touch test.txt
mv test.txt first_folder/test.txt
No confirmation message means it worked โ Terminal stays quiet on success and only speaks up on error. cd into first_folder and ls to double check.
Copying Files and Folders
cp copies instead of moving:
cp test.txt test_copy.txt
Copying a whole directory needs an extra piece:
cp -r first_folder first_folder_copy
That -r is called a flag โ an option you attach to a command to change how it behaves. Leave it off and cp refuses to copy a directory at all. To see every flag a command supports, run man cp (arrow keys to scroll, q to quit; the Windows equivalent is --help).
Deleting Files and Folders
rm deletes files. rmdir deletes empty directories. Neither asks “are you sure?” and neither can be undone โ there’s no trash bin here.
rm test.txt
rmdir first_folder
If first_folder still has anything inside it, rmdir refuses, and rm -rf first_folder is the way through โ -r for recursive, -f for force. Treat -rf with real caution: it deletes without asking, every time.
Quick Reference
| Command | Does |
|---|---|
whoami |
Prints your username |
echo |
Prints text (or writes it to a file with >) |
cat |
Prints a whole file to the screen |
less |
Scrolls through a file โ press q to exit |
open |
Opens a file or folder in its default app |
Try It
There’s no sandbox for this one โ mkdir, touch, cp, mv, and rm all change your real filesystem, so work through these in your own terminal.
- Create a file called
name.txt. - Rename it to
rename.txtusingmv. What does this tell you about whatmvactually does? - Copy
rename.txttocopy.txtusingcp. - Delete
copy.txt. - Create a folder called
questions, thencdinto it. - Create
first.txtandsecond.txtinside it. - Move back up a directory and copy
questionstoquestions_copy. - What is the
-rincp -rcalled, and what does it do? - Delete both
questionsandquestions_copy.
Recap
mkdirandtouchcreate folders and files;mvandcpmove and copy them;rmandrmdirdelete them.- A flag is an option attached to a command, like the
-rincp -r. catandlessshow you what’s inside a file;openhands it off to your system’s default app.
Next lesson, you’ll start sending command output exactly where you want it โ into files, into other commands โ with redirection and piping.