Creating Files
The touch command creates an empty file (or updates the timestamp of an existing one):
# Create a single file
touch my_file.txt
# Create multiple files at once
touch file1.txt file2.txt file3.txt
# Create files with a pattern
touch report_{1..5}.txt
# Creates: report_1.txt, report_2.txt, ... report_5.txtCreating Directories
Use mkdir (make directory) to create folders:
# Create a single directory
mkdir projects
# Create nested directories with -p (parent)
mkdir -p devops/docker/configs
# Create multiple directories
mkdir logs backups scriptsThe -p flag creates parent directories as needed — no error if they already exist.
Creating Files in Other Locations
You can create files anywhere using absolute or relative paths:
# Create a file inside a directory
touch projects/notes.txt
# Create using absolute path
touch /home/ubuntu/projects/todo.txt
# Verify with recursive listing
ls -R projects/Copying Files and Directories
The cp command copies files and directories:
# Copy a file
cp file1.txt file1_backup.txt
# Copy a file to another directory
cp file1.txt projects/
# Copy with a new name
cp file1.txt projects/renamed_file.txt
# Copy a directory (requires -r for recursive)
cp -r projects/ projects_backup/Moving and Renaming
The mv command moves OR renames files:
# Move a file to a directory
mv file2.txt projects/
# Rename a file (same directory, different name)
mv file3.txt important_notes.txt
# Move and rename simultaneously
mv file1.txt projects/main_config.txt
# Move a directory
mv projects_backup/ /tmp/Key insight: If the destination is in the same directory, mv acts as a rename. If it's a different directory, it moves the file.
Deleting Files and Directories
The rm command removes files permanently (no trash bin!):
# Delete a single file
rm file1.txt
# Delete multiple files
rm file1.txt file2.txt file3.txt
# Delete a directory and its contents (-r = recursive)
rm -r projects_backup/
# Force delete without confirmation (-f)
rm -rf old_directory/
# Delete with confirmation prompt (-i)
rm -i important_file.txtrm -rf is extremely dangerous. There is no undo. Always double-check your path before running this command, especially as root.
Practical Workflow
Here's a typical workflow combining these commands:
# Set up a project structure
mkdir -p webapp/{src,tests,docs,configs}
touch webapp/src/app.py
touch webapp/tests/test_app.py
touch webapp/docs/README.md
touch webapp/configs/settings.yaml
# Verify the structure
ls -R webapp/
# Make a backup before changes
cp -r webapp/ webapp_backup/
# Reorganize
mv webapp/configs/settings.yaml webapp/src/
rm -r webapp/configs/
# Clean up backup when done
rm -rf webapp_backup/Command Summary
| Command | Purpose | Example |
|---|---|---|
touch | Create empty file | touch file.txt |
mkdir | Create directory | mkdir -p path/to/dir |
cp | Copy file/directory | cp -r src/ dest/ |
mv | Move or rename | mv old.txt new.txt |
rm | Delete file/directory | rm -r directory/ |
Summary
touchcreates files,mkdircreates directoriescpcopies (use-rfor directories)mvmoves or renamesrmdeletes permanently (use-rfor directories,-fto force)- Always verify with
lsbefore and after operations
Next Steps
Now that you can manipulate files, let's learn how to edit their contents and redirect command output in the next lesson.