File & Folder Operations

20 minLesson 3 of 16

Learning Objectives

  • Create files with touch and directories with mkdir
  • Copy, move, and rename files and directories
  • Delete files and directories safely
  • Understand absolute vs relative paths in operations

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.txt

Creating 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 scripts

The -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.txt
⚠️

rm -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

CommandPurposeExample
touchCreate empty filetouch file.txt
mkdirCreate directorymkdir -p path/to/dir
cpCopy file/directorycp -r src/ dest/
mvMove or renamemv old.txt new.txt
rmDelete file/directoryrm -r directory/

Summary

  • touch creates files, mkdir creates directories
  • cp copies (use -r for directories)
  • mv moves or renames
  • rm deletes permanently (use -r for directories, -f to force)
  • Always verify with ls before 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.