From Zero to AI

Lesson 2.3: Working with Files

Duration: 45 minutes

Learning Objectives

After completing this lesson, you will be able to:

  • Create files using touch and text redirection
  • View file contents with cat, head, and tail
  • Copy files and directories with cp
  • Move and rename files with mv
  • Delete files and directories with rm
  • Understand the dangers of certain commands

Introduction

In the previous lesson, you learned to navigate and create directories. Now it's time to work with files themselves. You'll learn to create, view, copy, move, and delete files — all from the command line.

A word of caution: unlike the graphical interface, the terminal doesn't ask "Are you sure?" before deleting things, and there's no Trash/Recycle Bin. Deleted means deleted. We'll cover how to be careful.


Main Content

Creating Files

The touch Command

touch creates an empty file or updates the timestamp of an existing file.

$ touch myfile.txt

$ ls
myfile.txt

$ touch index.ts styles.css README.md

$ ls
index.ts  myfile.txt  README.md  styles.css

If the file already exists, touch just updates its "last modified" time without changing the content.

Creating Files with Content

Use echo and redirection (>) to create files with content:

# Create file with content
$ echo "Hello, World!" > greeting.txt

$ cat greeting.txt
Hello, World!

Redirection operators:

Operator What It Does
> Write to file (overwrites existing content)
>> Append to file (adds to end)
$ echo "Line 1" > file.txt
$ cat file.txt
Line 1

$ echo "Line 2" >> file.txt
$ cat file.txt
Line 1
Line 2

$ echo "Only this line" > file.txt    # Overwrites!
$ cat file.txt
Only this line

Warning: > will overwrite without asking. Use >> to append.

Viewing File Contents

The cat Command

cat (concatenate) displays file contents:

$ cat myfile.txt
This is the content of my file.
It can have multiple lines.

For multiple files, cat combines them:

$ cat file1.txt file2.txt
Content of file1
Content of file2

The head and tail Commands

For long files, you might want to see just the beginning or end:

# Show first 10 lines
$ head longfile.txt

# Show first 5 lines
$ head -n 5 longfile.txt

# Show last 10 lines
$ tail longfile.txt

# Show last 20 lines
$ tail -n 20 longfile.txt

tail is particularly useful for watching log files:

# Watch a file for new lines (live updates)
$ tail -f logfile.txt

Press Ctrl + C to stop watching.

The less Command

For reading longer files, less lets you scroll:

$ less longfile.txt

Navigation in less:

Key Action
Space or f Page down
b Page up
/ Line up/down
/text Search for "text"
n Next search result
q Quit

Copying Files and Directories

The cp Command

cp copies files:

# Copy file to new name
$ cp original.txt copy.txt

# Copy file to different directory
$ cp myfile.txt Documents/

# Copy file to different directory with new name
$ cp myfile.txt Documents/newname.txt

Copying Directories

To copy a directory and all its contents, use -r (recursive):

$ cp -r myfolder myfolder-backup

$ ls
myfolder  myfolder-backup

Without -r, you'll get an error:

$ cp myfolder newfolder
cp: myfolder is a directory (not copied).

Useful cp Options

Option Meaning
-r Recursive (copy directories)
-i Interactive (ask before overwriting)
-v Verbose (show what's being copied)
$ cp -riv old-project new-project
'old-project/index.ts' -> 'new-project/index.ts'
'old-project/package.json' -> 'new-project/package.json'

Moving and Renaming Files

The mv Command

mv moves files. It's also used to rename files (moving to the same location with a different name).

Renaming:

$ ls
oldname.txt

$ mv oldname.txt newname.txt

$ ls
newname.txt

Moving:

$ mv myfile.txt Documents/

$ ls Documents/
myfile.txt

Moving and renaming:

$ mv myfile.txt Documents/renamed.txt

Moving multiple files:

$ mv file1.txt file2.txt file3.txt Documents/

Moving Directories

Unlike cp, you don't need -r to move directories:

$ mv myfolder Documents/

Deleting Files and Directories

The rm Command

rm removes files. There is no undo. There is no Trash. Be careful.

$ rm unwanted.txt

Deleting Multiple Files

$ rm file1.txt file2.txt file3.txt

Deleting Directories

For empty directories, you can use rmdir:

$ rmdir empty-folder

For directories with contents, use rm -r:

$ rm -r folder-with-stuff

The Dangerous Command

# DON'T RUN THIS!
$ rm -rf /

# This would attempt to delete EVERYTHING on your computer
# The -f flag means "force" - don't ask for confirmation

Safe practices:

  1. Double-check your path before pressing Enter
  2. Use -i to get confirmation prompts: rm -ri folder
  3. Use ls first to verify what you'll delete: ls folder/*
  4. Avoid rm -rf unless absolutely necessary

Useful rm Options

Option Meaning
-r Recursive (delete directories)
-i Interactive (ask before each deletion)
-f Force (no confirmation, no errors for missing files)
-v Verbose (show what's being deleted)
# Safe way to delete a folder
$ rm -riv old-project
remove old-project/index.ts? y
remove old-project/package.json? y
removed 'old-project/index.ts'
removed 'old-project/package.json'
removed directory 'old-project'

Wildcards (Pattern Matching)

Wildcards let you work with multiple files at once:

Wildcard Meaning Example
* Any characters (zero or more) *.txt = all .txt files
? Any single character file?.txt = file1.txt, fileA.txt
[abc] Any character in brackets file[123].txt = file1.txt, file2.txt, file3.txt

Examples:

# List all TypeScript files
$ ls *.ts
index.ts  utils.ts  config.ts

# Copy all images to a folder
$ cp *.jpg *.png images/

# Delete all .log files
$ rm *.log

# List files starting with "test"
$ ls test*
test1.ts  test2.ts  test-utils.ts

Caution with wildcards and rm:

# ALWAYS preview first!
$ ls *.tmp
file1.tmp  file2.tmp  cache.tmp

# Then delete
$ rm *.tmp

Practice Exercise

Task

Complete these file operation challenges:

Setup: Create a practice environment

cd ~
mkdir -p file-practice
cd file-practice

Challenge 1: Creating Files

  1. Create three empty files: notes.txt, todo.txt, ideas.txt
  2. Add the text "My first note" to notes.txt
  3. Append "My second note" to notes.txt (don't overwrite!)
  4. View the contents of notes.txt

Challenge 2: Organizing

  1. Create directories: documents and archive
  2. Copy notes.txt to the documents folder
  3. Move todo.txt and ideas.txt to the documents folder
  4. List the contents of documents to verify

Challenge 3: Renaming and Cleanup

  1. Rename documents/ideas.txt to documents/brainstorm.txt
  2. Create a backup of the documents folder called documents-backup
  3. Delete the original notes.txt in the main file-practice folder
  4. List all .txt files in documents using a wildcard

Cleanup (when done):

cd ~
rm -ri file-practice

Solutions

Challenge 1 Solution
touch notes.txt todo.txt ideas.txt
echo "My first note" > notes.txt
echo "My second note" >> notes.txt
cat notes.txt
Challenge 2 Solution
mkdir documents archive
cp notes.txt documents/
mv todo.txt ideas.txt documents/
ls documents
Challenge 3 Solution
mv documents/ideas.txt documents/brainstorm.txt
cp -r documents documents-backup
rm notes.txt
ls documents/*.txt

Key Takeaways

  • touch creates empty files; echo "text" > file creates files with content
  • > overwrites, >> appends — know the difference!
  • cat displays file contents; use less for long files
  • cp copies (use -r for directories)
  • mv moves or renames files and directories
  • rm deletes permanently — there is no undo
  • Always use -i with rm when in doubt: rm -ri folder
  • Wildcards (*, ?) help you work with multiple files at once
  • Preview before you delete: run ls pattern before rm pattern

Resources

Resource Type Difficulty
Working with Files - LinuxCommand.org Tutorial Beginner
Rm Command Examples - Linuxize Tutorial Beginner
GNU Coreutils Manual Documentation Intermediate