DaleSchool

Working with Files and Directories

Beginner25min

Learning Objectives

  • Create directories and files with mkdir and touch
  • Copy and move files with cp and mv
  • Use rm safely
  • Process multiple files at once with wildcards (*)

Working Code

Example 1: Creating directories and files

mkdir practice
cd practice
touch hello.txt
touch world.txt
ls

Output:

hello.txt  world.txt

mkdir creates a directory, touch creates an empty file.

Example 2: Copying and renaming files

# Copy a file
cp hello.txt hello-backup.txt
ls

Output:

hello-backup.txt  hello.txt  world.txt
# Rename a file (mv handles both moving and renaming)
mv world.txt universe.txt
ls

Output:

hello-backup.txt  hello.txt  universe.txt

Example 3: Moving files into a directory

mkdir docs
mv hello.txt docs/
mv universe.txt docs/
ls
ls docs/

Output:

docs  hello-backup.txt

hello.txt  universe.txt

Try It Yourself

Creating nested directories at once

# -p option: creates intermediate directories too
mkdir -p projects/my-app/src
ls projects/
ls projects/my-app/

Output:

my-app

src

mkdir -p creates all levels of the path at once. No error even if intermediate directories already exist.

Copying an entire directory

mkdir -p backup
cp -r docs/ backup/docs
ls backup/

When copying a directory, the -r (recursive) option is required.

Deleting files

rm hello-backup.txt
ls
# Delete a directory
rm -r backup/
ls

"Why?" — What Each Command Does

| Command | Meaning | Key Options | | ------- | ------------------------------ | ------------------------------------------------- | | mkdir | Make Directory | -p: create intermediate paths | | touch | Create file / update timestamp | -- | | cp | Copy | -r: directories, -i: confirm before overwrite | | mv | Move (also renames) | -i: confirm before overwrite | | rm | Remove | -r: directories, -f: force delete |

mv is both move and rename

# Rename (within the same directory)
mv old-name.txt new-name.txt

# Move to another directory
mv file.txt ~/Documents/

# Move and rename at the same time
mv file.txt ~/Documents/renamed.txt

Common cp patterns

# Copy a file
cp source.txt destination.txt

# Copy a directory
cp -r source-dir/ destination-dir/

# Copy multiple files into a directory
cp file1.txt file2.txt target-dir/

Common Mistakes

Mistake 1: rm is not a trash can

rm permanently deletes files immediately. They don't go to the trash.

rm important.txt   # cannot be recovered!

Safe habits:

# Check targets with ls before deleting
ls *.log

# Use -i to confirm before deleting
rm -i important.txt
# remove important.txt? y

Mistake 2: The danger of rm -rf

# Never do this
rm -rf /
rm -rf ~/*
rm -rf .    # deletes the entire current directory

-f forces deletion without confirmation, -r deletes recursively. Combined, they're extremely dangerous.

Mistake 3: cp destination mistakes

# Copying to an existing file overwrites it silently
cp new.txt existing-important.txt

# Safer: use -i to confirm before overwriting
cp -i new.txt existing-important.txt
# overwrite existing-important.txt? (y/n [n])

Mistake 4: Forgetting -r when copying directories

# Error
cp my-folder/ backup/
# cp: my-folder is a directory (not copied)

# Correct
cp -r my-folder/ backup/

Deep Dive

Wildcards (_) for batch operations

_ matches any string of characters:

# List all .txt files
ls *.txt

# Delete all files starting with "log"
rm log*

# Copy all .txt files to backup directory
cp *.txt backup/

# ? matches a single character
ls file?.txt  # matches file1.txt, file2.txt, but not file10.txt

Be especially careful with wildcards when deleting. Always run ls *.txt before rm *.txt to confirm.

Symbolic links (ln -s)

A symbolic link is an alias (shortcut) to a file or directory:

# Create a symbolic link
ln -s ~/Documents/project/config.txt config

# Check (note the l at the beginning)
ls -la config
# lrwxr-xr-x  1 dale staff  35 Jan 15 config -> /Users/dale/Documents/project/config.txt

# Access the file through the link
cat config   # shows the contents of the original config.txt

Common use cases:

  • Shortening long paths
  • Sharing the same file across multiple locations
  • Managing versioned files (config -> config-v2.txt)
Safe deletion on macOS (trash)

Install the trash command on macOS to send files to the Trash:

brew install trash
trash important.txt   # moved to Trash (recoverable)

Use trash instead of rm for important files to prevent accidental data loss.

  1. Create nested directories with mkdir -p workspace/project/src workspace/project/docs.
  2. Create README.md, src/main.py, and docs/guide.txt inside workspace/project/.
  3. Make a full backup with cp -r workspace/project workspace/project-backup.
  4. Rename a file with mv workspace/project/docs/guide.txt workspace/project/docs/user-guide.txt.
  5. Verify the result with ls -la workspace/project/docs/.

Q1. Which command correctly copies a directory with all its contents recursively?

  • A) cp -a source/ dest/
  • B) cp -r source/ dest/
  • C) cp -f source/ dest/
  • D) cp -d source/ dest/