Back to Blog

Zsh Shell Tutorial #3: File Operations - touch, mkdir, cp, mv, rm & Wildcards (macOS)

Sandy LaneSandy Lane

Video: Zsh Shell Tutorial #3: File Operations - touch, mkdir, cp, mv, rm & Wildcards (macOS) by Taught by Celeste AI - AI Coding Coach

Watch full page →

Zsh Shell Tutorial #3: File Operations - touch, mkdir, cp, mv, rm & Wildcards (macOS)

Master essential file operations in Zsh to efficiently create, copy, move, and delete files and directories on macOS. This guide covers commands like touch, mkdir, cp, mv, and rm, plus how to use wildcards for powerful pattern matching.

Code

# Create an empty file or update its timestamp
touch myfile.txt

# Create a single directory
mkdir myfolder

# Create nested directories (parents created as needed)
mkdir -p project/myapp/source

# Copy a file to another location or filename
cp myfile.txt myfile_backup.txt

# Copy a directory recursively (including all contents)
cp -r project myproject_backup

# Move or rename a file or directory
mv myfile.txt documents/myfile_renamed.txt

# Delete a file (permanently!)
rm unwantedfile.txt

# Delete a directory and all its contents recursively (be very careful!)
rm -r oldfolder

# Use wildcards to match patterns:
# * matches any number of characters
ls *.txt          # lists all .txt files
# ? matches exactly one character
ls file?.txt      # matches file1.txt, fileA.txt, etc.
# [ ] matches any one character inside brackets
ls file[123].txt  # matches file1.txt, file2.txt, or file3.txt

Key Points

  • touch creates empty files or updates existing file timestamps without modifying content.
  • mkdir -p creates nested directories in one command, creating parent folders as needed.
  • cp -r copies directories recursively, preserving all files and subfolders.
  • mv moves or renames files and directories without extra flags.
  • rm permanently deletes files or directories; use with caution as there is no undo.
  • Wildcards like *, ?, and [ ] enable flexible filename pattern matching across commands.