Linux Lab: File & Directory Management

Learn to navigate the filesystem, create and edit files, manage directories, and handle common errors.

Lab Objectives

  • Navigate the Linux filesystem using cd, ls, and pwd.
  • Create, rename, copy, and move files.
  • Create and remove directories.
  • Append content to existing files using redirection.
  • Delete files and directories, and understand common errors.

Prerequisites

  • A Linux system with terminal access (Kali, Ubuntu, or any distribution).
  • No special permissions required — all commands run as a normal user.

Part 1: Navigating the Filesystem

Step 1: Find out where you are:
pwd

Prints your current working directory, e.g. /home/kali.

Step 2: List what is in the current directory:
ls
ls -la

-l shows details (permissions, size, date). -a includes hidden files (names starting with .).

Step 3: Move to a different directory:
cd /tmp
pwd

You should now be in /tmp.

Step 4: Go back to your home directory:
cd ~
pwd

~ is a shortcut for your home directory. You can also just type cd with no arguments.

Step 5: Move up one directory level:
cd ..
pwd

.. means "the parent directory". If you were in /home/kali, you are now in /home.

Step 6: Return home and create a lab directory:
cd ~
mkdir filelab
cd filelab
pwd

You should be in /home/kali/filelab (or equivalent).

Part 2: Creating and Writing to Files

Step 1: Create an empty file with touch:
touch empty-file.txt
ls -la empty-file.txt

The file exists but has 0 bytes.

Step 2: Create a file with content using echo and redirection:
echo "Hello, this is my first file." > hello.txt
cat hello.txt

> redirects output to a file, creating it if it does not exist or overwriting it if it does.

Step 3: Append to an existing file:
echo "This is a second line." >> hello.txt
echo "And a third line." >> hello.txt
cat hello.txt

>> appends to the end of the file instead of overwriting it. You should see all three lines.

Step 4: See what happens if you use > instead of >>:
echo "Oops, I used single redirect." > hello.txt
cat hello.txt

The previous three lines are gone. > overwrites the file completely.

Step 5: Create a multi-line file with a heredoc:
cat > notes.txt << 'EOF'
Meeting notes from today.
Action item 1: Update firewall rules.
Action item 2: Review access logs.
EOF
cat notes.txt

Heredocs let you write multiple lines in a single command.

Part 3: Copying, Moving, and Renaming Files

Step 1: Copy a file:
cp notes.txt notes-backup.txt
ls -la

You now have two files with identical content.

Step 2: Rename a file (using mv):
mv empty-file.txt renamed-file.txt
ls

mv is used for both moving and renaming. empty-file.txt is gone; renamed-file.txt took its place.

Step 3: Create a subdirectory and move a file into it:
mkdir archive
mv notes-backup.txt archive/
ls archive/

The file has been moved into the archive directory.

Step 4: Copy a file into a subdirectory:
cp hello.txt archive/hello-copy.txt
ls archive/

The original file remains and a copy exists in archive/.

Part 4: Creating Directories

Step 1: Create a single directory:
mkdir reports
ls -la
Step 2: Create nested directories in one command:
mkdir -p projects/2025/january
ls -R projects/

-p creates parent directories as needed. Without it, you would get an error because projects/ and projects/2025/ do not exist yet.

Step 3: See the error without -p:
mkdir data/logs/errors
Step 4: Fix it with -p:
mkdir -p data/logs/errors
ls -R data/

Part 5: Deleting Files and Directories

Step 1: Delete a single file:
rm renamed-file.txt
ls

The file is removed. There is no recycle bin in Linux — it is gone permanently.

Step 2: Try to delete a directory with rm:
rm archive
Step 3: Try rmdir on a directory that contains files:
rmdir archive
Step 4: Delete an empty directory with rmdir:
rmdir reports
ls

reports/ was empty, so rmdir removes it successfully.

Step 5: Delete a directory and all its contents with rm -r:
rm -r archive
ls

-r (recursive) deletes the directory and everything inside it.

Step 6: Clean up the entire lab directory:
cd ~
rm -r filelab
ls filelab

Deliverables

  • Screenshot of pwd and ls -la after creating the lab directory.
  • Screenshot of cat hello.txt showing appended lines.
  • Screenshot showing the result of > overwriting the file.
  • Screenshot of ls -R showing the nested directory structure.
  • Screenshots of each error: mkdir without -p, rm on a directory, and rmdir on a non-empty directory.

Commands Used

  • pwd — Print working directory.
  • ls — List directory contents.
  • cd — Change directory.
  • mkdir — Create a directory.
  • touch — Create an empty file.
  • echo — Print text (use with redirection).
  • cat — Display file contents.
  • cp — Copy files or directories.
  • mv — Move or rename files.
  • rm — Remove files.
  • rmdir — Remove empty directories.

Redirection Operators

  • > — Redirect output (overwrites).
  • >> — Redirect output (appends).
  • < — Redirect input from file.

Common Errors

  • No such file or directory — Path does not exist.
  • Is a directory — Use rm -r instead of rm.
  • Directory not empty — Use rm -r instead of rmdir.
  • Permission denied — Need sudo or ownership.