Excellent! Practicing with . and .. is a fundamental skill for navigating Linux efficiently.
Let's do some exercises to get a good feel for them. Your current step is perfect for this as it talks about these very concepts.
First, let's make sure you're in the right starting place:
- Check your current location.
You should seepwd/home/labex/project. If not, runcd /home/labex/projectto get there.
Now, let's practice:
Exercise 1: Moving up and down
-
Move up one level using
..cd ..(This should take you to
/home/labex) -
Verify your location
pwd(Confirm you are in
/home/labex) -
Now, move back into the
projectdirectory from where you are now, using a relative path.cd project(This should take you back to
/home/labex/project) -
Verify your location again
pwd(Confirm you are in
/home/labex/project)
Exercise 2: Referencing parent directory content
Let's create a file in your home directory, and then try to view it from your project directory.
-
Go to your home directory.
cd ~ -
Create a file called
my_note.txtin your home directory.echo "This is a note from my home directory." > my_note.txt -
Go back to your project directory.
cd project -
Now, from inside the
projectdirectory, try to list the contents of your home directory using..to refer to it.ls ..You should see
my_note.txtlisted there. -
Still from the
projectdirectory, try to view the content ofmy_note.txtusing..in the path.cat ../my_note.txtYou should see the text "This is a note from my home directory."
Exercise 3: Referencing the current directory with .
While cd . doesn't do anything (it just moves you to the directory you're already in), . is often used when executing files or as part of a path.
-
Make sure you are in
~/project.pwd(Should be
/home/labex/project) -
Create a simple shell script in your current directory (
.):echo '#!/bin/bash' > hello.sh echo 'echo "Hello from the script!"' >> hello.sh chmod +x hello.sh- The first two lines create a simple script.
chmod +x hello.shmakes the script executable.
-
Now, run the script using
.to indicate the current directory../hello.sh(You should see "Hello from the script!")
This demonstrates that . is useful for indicating "this directory" when the command needs a path.
How did these exercises go for you? Let me know if you encountered any issues or have more questions!