Creating Files and Directories
Before you can work with files, you need to know how to create them. Linux provides simple commands for creating both empty files and new directories.
Creating Files with touch
The touch command creates a new, empty file. If the file already exists,
it updates the file's modification timestamp without changing its contents.
# Create a single file
touch notes.txt
# Create multiple files at once
touch file1.txt file2.txt file3.txt
# Verify the files were created
ls -l
Creating Directories with mkdir
The mkdir (make directory) command creates new folders. Use the -p
flag to create nested directories in one step.
# Create a single directory
mkdir projects
# Create nested directories (parent + child)
mkdir -p projects/website/css
# Without -p, this would fail if "projects" didn't exist
Always use mkdir -p when creating nested directories. Without it,
mkdir will fail if any parent directory in the path does not already
exist. With -p, it creates the entire chain silently.
Copying Files and Directories
The cp (copy) command duplicates files and directories. The original
remains untouched, and a new copy is created at the destination.
Copying Files
# Copy a file to a new name in the same directory
cp notes.txt notes-backup.txt
# Copy a file into another directory
cp notes.txt ~/Documents/
# Copy a file into another directory with a new name
cp notes.txt ~/Documents/my-notes.txt
Copying Directories
To copy an entire directory and everything inside it, you must use the -r
(recursive) flag. Without it, cp will refuse to copy a directory.
# Copy a directory and all its contents
cp -r projects projects-backup
# Copy into a specific location
cp -r projects ~/Documents/projects-copy
Use cp -a instead of cp -r when you want to preserve
permissions, timestamps, and ownership. The -a flag is short for
"archive" and is the best choice for making exact copies.
Moving and Renaming
The mv (move) command does double duty: it moves files to a new location
and renames files. Unlike cp, the original is removed -- the file is
relocated, not duplicated.
Moving Files
# Move a file to another directory
mv notes.txt ~/Documents/
# Move multiple files into a directory
mv file1.txt file2.txt file3.txt ~/Documents/
Renaming Files
Renaming is just moving a file to a new name in the same directory. There is no
separate "rename" command in basic Linux -- mv handles it.
# Rename a file
mv old-name.txt new-name.txt
# Rename a directory
mv old-folder new-folder
If the destination file already exists, mv will silently overwrite
it. Use mv -i (interactive mode) to get a confirmation prompt before
overwriting any existing file.
Deleting Files and Directories
The rm (remove) command deletes files, and rmdir removes
empty directories. These operations are permanent on Linux -- there is no trash can
in the terminal.
Deleting Files
# Delete a single file
rm notes.txt
# Delete with confirmation prompt
rm -i notes.txt
# rm: remove regular file 'notes.txt'? y
# Delete multiple files
rm file1.txt file2.txt file3.txt
Deleting Directories
# Remove an empty directory
rmdir empty-folder
# Remove a directory and everything inside it
rm -r projects
# Remove with confirmation for each file
rm -ri projects
The command rm -rf deletes everything recursively without asking
for confirmation. A typo like rm -rf / or rm -rf ~ can
destroy your entire system or home directory in seconds. Always double-check
the path before running any rm command, and prefer rm -ri
when deleting directories until you are confident.
Using Wildcards
Wildcards (also called globs) let you match multiple files at once, making bulk operations much faster. The shell expands the wildcard into a list of matching file names before the command runs.
*.txt matches all text files.
file?.txt matches file1.txt but not file10.txt.
# Copy all .txt files to Documents
cp *.txt ~/Documents/
# Delete all .log files
rm *.log
# Move all files starting with "report"
mv report* ~/Documents/
# List all .jpg and .png files
ls *.jpg *.png
Before running rm *.something, first run ls *.something
to see exactly which files match. This lets you verify the wildcard catches the
right files before you commit to deleting them.
Summary
In this tutorial, you learned how to manage files and directories from the terminal:
- touch -- Create empty files or update timestamps
- mkdir / mkdir -p -- Create directories and nested directory trees
- cp / cp -r -- Copy files and directories
- mv -- Move or rename files and directories
- rm / rm -r -- Delete files and directories (permanently)
- Wildcards -- Match multiple files with *, ?, and []
You now have the fundamental skills to create, organize, and clean up files
from the command line. Next, you will learn how to read and inspect file contents
using commands like cat, less, and tail.