File Operations#

Concepts#

Creating Files and Directories#

touch — Create an Empty File (or Update Timestamp)#

touch newfile.txt           # creates newfile.txt (empty)
touch file1.txt file2.txt   # creates multiple files at once
touch existing.txt          # if file exists, updates its modification timestamp

touch does not overwrite an existing file’s content — it only updates the timestamp.

mkdir — Make Directories#

mkdir myfolder              # create a directory
mkdir dir1 dir2 dir3        # create multiple directories
mkdir -p a/b/c              # create nested directories (creates a, a/b, and a/b/c)

The -p flag is important: without it, mkdir a/b/c fails if a/ or a/b/ do not already exist. With -p, it creates the entire chain.

Copying Files and Directories#

cp — Copy#

cp source.txt dest.txt              # copy a file
cp source.txt /tmp/                 # copy to a different directory (keeps the name)
cp source.txt /tmp/newname.txt      # copy to a different directory with a new name
cp file1.txt file2.txt destdir/     # copy multiple files into a directory
cp -r sourcedir/ destdir/           # copy a directory recursively (-r is required)
cp -i source.txt dest.txt           # interactive: ask before overwriting
cp -v source.txt dest.txt           # verbose: show what's being copied

Important: cp without -r cannot copy directories. You will get an error if you try.

Moving and Renaming#

mv — Move (and Rename)#

In Linux, moving and renaming are the same operation:

mv oldname.txt newname.txt          # rename a file
mv file.txt /tmp/                   # move to another directory
mv file.txt /tmp/newname.txt        # move and rename at the same time
mv dir1/ dir2/                      # rename a directory (no -r needed)
mv file1.txt file2.txt destdir/     # move multiple files into a directory
mv -i source.txt dest.txt           # interactive: ask before overwriting
mv -v source.txt /tmp/              # verbose: show what's being moved

Unlike cp, mv does not need -r for directories — it just changes where the directory is referenced.

Deleting Files and Directories#

rm — Remove#

rm file.txt                 # delete a file (no confirmation, no trash)
rm file1.txt file2.txt      # delete multiple files
rm -i file.txt              # interactive: ask before deleting
rm -r directory/            # delete a directory and all its contents (recursive)
rm -rf directory/           # force recursive delete (no confirmation, no errors for missing files)

Warning: rm is permanent. There is no trash can, no undo, no recycle bin. Once a file is deleted with rm, it is gone. Be especially careful with:

  • rm -rf / — would delete the entire system (modern systems block this, but still)
  • rm -rf * — deletes everything in the current directory
  • rm -rf ~ — deletes your entire home directory

Always double-check your command before pressing Enter, especially when using rm -rf.

rmdir — Remove Empty Directories#

rmdir emptydir/             # only works if the directory is empty
rmdir -p a/b/c              # remove c, then b, then a (only if each is empty)

rmdir is safe: it refuses to delete a directory that contains anything. Use rm -r when you need to delete a directory with content.

Wildcards (Globbing)#

Wildcards let you match multiple files at once:

Wildcard Matches Example
* Any number of characters (including none) *.txt — all .txt files
? Exactly one character file?.txtfile1.txt, fileA.txt, not file10.txt
[abc] One character from the set file[123].txtfile1.txt, file2.txt, file3.txt
[a-z] One character from the range file[a-z].txtfilea.txt through filez.txt
[!abc] One character NOT in the set file[!0-9].txt — not a digit

Examples:

ls *.txt                    # list all .txt files
cp *.jpg /tmp/              # copy all .jpg files to /tmp
rm *.log                    # delete all .log files
mv report_202?.pdf archive/ # move report_2020.pdf through report_2029.pdf
ls file[1-3].txt            # match file1.txt, file2.txt, file3.txt

Important: The shell expands wildcards before the command sees them. So rm *.txt is actually expanded to rm file1.txt file2.txt file3.txt (etc.) by the shell, and then rm sees the individual filenames.

Reading File Contents (Quick Preview)#

You will learn text processing in depth in Module 06. For now, these basic tools let you see what is inside a file:

cat file.txt                # print the entire file
less file.txt               # page through the file (Space=next page, b=back, q=quit)
head file.txt               # show the first 10 lines
head -n 5 file.txt          # show the first 5 lines
tail file.txt               # show the last 10 lines
tail -n 5 file.txt          # show the last 5 lines
wc file.txt                 # count lines, words, and characters
wc -l file.txt              # count only lines

file — Determine File Type#

Linux does not rely on file extensions to identify file types. The file command examines the actual content:

file photo.jpg              # JPEG image data
file script.sh              # Bash script, ASCII text executable
file /bin/ls                # ELF 64-bit LSB pie executable
file mystery                # tells you what it is regardless of extension

Lab#

Exercise 1: Create a Practice Area#

# Create a lab directory in your home
mkdir -p ~/lab/module01
cd ~/lab/module01

# Confirm
pwd

Exercise 2: Create Files and Directories#

# Create some empty files
touch file1.txt file2.txt file3.txt

# Create a nested directory structure
mkdir -p projects/web projects/scripts

# Verify
ls -la
ls -R projects/
tree .    # if tree is installed

Exercise 3: Copy Files#

# Copy a single file
cp file1.txt file1_backup.txt

# Copy a file into a directory
cp file2.txt projects/

# Copy with a new name
cp file3.txt projects/web/index.txt

# Copy a directory (requires -r)
cp -r projects/ projects_backup/

# Verify
ls -la
ls -R projects/
ls -R projects_backup/

Exercise 4: Move and Rename#

# Rename a file
mv file1_backup.txt file1_copy.txt

# Verify it was renamed (original name is gone)
ls file1_backup.txt    # should say "No such file"
ls file1_copy.txt      # exists

# Move a file into a directory
mv file1_copy.txt projects/scripts/

# Verify
ls projects/scripts/

# Rename a directory
mv projects_backup/ archive/

# Verify
ls

Exercise 5: Practice Wildcards#

# Create some test files
touch report_2023.pdf report_2024.pdf report_2025.pdf
touch photo1.jpg photo2.jpg photo3.jpg
touch notes.txt todo.txt readme.md

# List only .pdf files
ls *.pdf

# List only .jpg files
ls *.jpg

# List files starting with "report"
ls report*

# List all .txt and .md files
ls *.txt *.md

# Copy all PDFs to projects/
cp *.pdf projects/

# Verify
ls projects/

Exercise 6: Delete Files Safely#

# Delete a single file
rm notes.txt

# Try deleting with confirmation
rm -i todo.txt
# Type 'y' to confirm, 'n' to cancel

# Try to delete a non-empty directory without -r
rmdir projects/
# Error: directory not empty

# Delete the archive directory and everything inside it
rm -r archive/

# Verify
ls

Exercise 7: Inspect File Contents#

# Put some content in a file
echo "Hello, this is line 1" > testfile.txt
echo "This is line 2" >> testfile.txt
echo "This is line 3" >> testfile.txt
# (> creates/overwrites, >> appends — you'll learn this in Module 06)

# View the entire file
cat testfile.txt

# View with less (press q to quit)
less testfile.txt

# First 2 lines
head -n 2 testfile.txt

# Last 1 line
tail -n 1 testfile.txt

# Count lines
wc -l testfile.txt

# Check file type
file testfile.txt
file /bin/bash

Exercise 8: Clean Up#

# Go up and remove the entire lab area
cd ~
rm -r lab/module01

# Verify it's gone
ls lab/
# Should be empty or show an error if lab/ was only used for module01

Review#

1. What does `mkdir -p a/b/c` do that `mkdir a/b/c` cannot?

mkdir -p creates the entire directory chain: first a, then a/b, then a/b/c. Without -p, mkdir fails if any intermediate directories do not already exist.

2. What happens if you use `cp` without `-r` on a directory?

It fails with an error: “omitting directory.” The -r (recursive) flag is required to copy a directory and its contents.

3. How is renaming a file done in Linux?

Using mv (move). For example, mv oldname.txt newname.txt renames the file. There is no separate “rename” command for single files — moving and renaming are the same operation.

4. Why is `rm` dangerous compared to deleting files on Windows or macOS?

rm permanently deletes files. There is no trash can, recycle bin, or undo. Once a file is removed with rm, it is gone. This is especially dangerous with rm -rf on the wrong directory.

5. What does the wildcard `*.txt` match?

All files in the current directory whose names end with .txt. The * matches any number of characters (including none), so it matches notes.txt, a.txt, .txt, etc.

6. What is the difference between `>` and `>>` when writing to a file?

> creates the file (or overwrites it if it exists) with the new content. >> appends to the file without erasing existing content.

7. What command tells you the actual type of a file, regardless of its extension?

file filename. It examines the file’s content (not its extension) to determine what kind of file it is.

8. What is the difference between `rm` and `rmdir`?

rmdir only removes empty directories — it is safe and will refuse to delete a directory with contents. rm -r removes directories along with everything inside them, which is powerful but dangerous.


Previous: Navigating the Filesystem | Next: Filesystem Hierarchy