CodingNic

Terminal Fundamentals

Working with Files and Folders

Terminal Fundamentals 20 min read

Working with Files and Folders

Objectives

By the end of this chapter, you should be able to:

  • Create files and folders using mkdir and touch
  • Move files and folders using mv
  • Copy files and folders using cp
  • Remove files and folders using rm and rmdir
  • Explain what a flag is
  • Explain what whoami, cat, echo, less, and open do

๐Ÿ’ก 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:

bash
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:

bash
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):

bash
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:

bash
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:

bash
cp test.txt test_copy.txt

Copying a whole directory needs an extra piece:

bash
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.

bash
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.

  1. Create a file called name.txt.
  2. Rename it to rename.txt using mv. What does this tell you about what mv actually does?
  3. Copy rename.txt to copy.txt using cp.
  4. Delete copy.txt.
  5. Create a folder called questions, then cd into it.
  6. Create first.txt and second.txt inside it.
  7. Move back up a directory and copy questions to questions_copy.
  8. What is the -r in cp -r called, and what does it do?
  9. Delete both questions and questions_copy.

Recap

  • mkdir and touch create folders and files; mv and cp move and copy them; rm and rmdir delete them.
  • A flag is an option attached to a command, like the -r in cp -r.
  • cat and less show you what’s inside a file; open hands 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.