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:
pwdPrints your current working directory, e.g. /home/kali.
Step 2: List what is in the current directory:
lsls -la-l shows details (permissions, size, date). -a includes hidden files (names starting with .).
Step 3: Move to a different directory:
cd /tmppwdYou 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 filelabcd filelabpwdYou 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.txtls -la empty-file.txtThe 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.txtcat 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.txtecho "And a third line." >> hello.txtcat 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.txtcat hello.txtThe 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.
EOFcat notes.txtHeredocs 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.txtls -laYou now have two files with identical content.
Step 2: Rename a file (using mv):
mv empty-file.txt renamed-file.txtlsmv 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 archivemv 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.txtls archive/The original file remains and a copy exists in archive/.
Part 4: Creating Directories
Step 1: Create a single directory:
mkdir reportsls -laStep 2: Create nested directories in one command:
mkdir -p projects/2025/januaryls -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/errorsmkdir: cannot create directory 'data/logs/errors': No such file or directoryThis fails because data/ does not exist. Use mkdir -p to create the entire path.
Step 4: Fix it with -p:
mkdir -p data/logs/errorsls -R data/Part 5: Deleting Files and Directories
Step 1: Delete a single file:
rm renamed-file.txtlsThe file is removed. There is no recycle bin in Linux — it is gone permanently.
Step 2: Try to delete a directory with rm:
rm archiverm: cannot remove 'archive': Is a directoryrm without flags cannot delete directories. You need either rmdir or rm -r.
Step 3: Try rmdir on a directory that contains files:
rmdir archivermdir: failed to remove 'archive': Directory not emptyrmdir only removes empty directories.
Step 4: Delete an empty directory with rmdir:
rmdir reportslsreports/ was empty, so rmdir removes it successfully.
Step 5: Delete a directory and all its contents with rm -r:
rm -r archivels-r (recursive) deletes the directory and everything inside it.
Step 6: Clean up the entire lab directory:
cd ~rm -r filelabls filelabls: cannot access 'filelab': No such file or directoryThis confirms the directory has been completely removed.
Deliverables
- Screenshot of
pwdandls -laafter creating the lab directory. - Screenshot of
cat hello.txtshowing appended lines. - Screenshot showing the result of
>overwriting the file. - Screenshot of
ls -Rshowing the nested directory structure. - Screenshots of each error:
mkdirwithout-p,rmon a directory, andrmdiron 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 -rinstead ofrm. - Directory not empty — Use
rm -rinstead ofrmdir. - Permission denied — Need
sudoor ownership.