2.7 Practice Exercises - Basic Linux Commands
1. Exploring and Navigating
-
Find out which directory you are in:
Show Answer
pwd
-
List everything in your current directory:
Show Answer
ls -l
-
Move into your home directory, then into the Documents folder (create one first if it doesn’t exist):
Show Answer
mkdir -p ~/Documents
cd ~/Documents
2. Working with Directories
-
Inside Documents, create a new folder called workshop:
Show Answer
mkdir workshop
-
Go into workshop and make three subfolders: notes, data, and scripts:
Show Answer
cd workshop
mkdir notes data scripts -
Verify the structure:
Show Answer
ls -R
3. Creating and Viewing Files
-
Inside notes/, create a file day1.txt and add some text with nano:
Show Answer
nano notes/day1.txt
-
Display the file’s content:
Show Answer
cat notes/day1.txt
-
Show just the first and last few lines of the file:
Show Answer
head notes/day1.txt
tail notes/day1.txt
4. Copying, Moving, and Renaming
-
Copy day1.txt into the data/ folder:
Show Answer
cp notes/day1.txt data/
-
Rename the copied file to backup.txt:
Show Answer
mv data/day1.txt data/backup.txt
-
Move the original day1.txt into the scripts/ folder:
Show Answer
mv notes/day1.txt scripts/
5. Deleting Safely
-
Create an empty file called temp.txt in workshop/:
Show Answer
touch temp.txt
-
Delete it:
Show Answer
rm temp.txt
-
Try removing a whole directory (scripts/):
Show Answer
rm -r scripts
⚠️ Always double-check the path before using rm -r
!
6. Searching and Filtering
-
Make a text file with several lines:
Show Answer
echo -e "apple\nbanana\ncherry\napricot\nblueberry" > fruits.txt
-
Find all lines containing "ap":
Show Answer
grep "ap" fruits.txt
-
Count how many lines are in the file:
Show Answer
wc -l fruits.txt
7. Redirecting and Piping
-
Save a directory listing into a file:
Show Answer
ls -l > listing.txt
-
Append the output of date into the same file:
Show Answer
date >> listing.txt
-
Chain commands: list files and immediately search for "txt":
Show Answer
ls | grep txt
8. (For WSL Users) Creating Symbolic Links
Create a symbolic link from your Windows Downloads folder to the WSL downloads directory:
-
Create the link Downloads/Win-Downloads in WSL:
Show Answer
ln -s /mnt/c/Users/YourWindowsUsername/Downloads ~/Downloads/Win-Downloads
9. Bonus Challenge: Mini Project
- Create a folder
project
. - Inside it, create a
src
folder and aREADME.txt
. - Write a short note in
README.txt
withnano
. - Copy
README.txt
intosrc/
and rename itnotes.txt
. - Use
grep
to search for a word insidenotes.txt
. - Redirect the search results into a new file
results.txt
. - Finally, remove the entire
project
folder.
Show Answer
mkdir project
cd project
mkdir src
nano README.txt
cp README.txt src/notes.txt
grep "word" src/notes.txt > results.txt
cd ..
rm -r project