Bird
Raised Fist0
Linux CLIscripting~20 mins

Home directory (~) and shortcuts in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tilde Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this command?
Consider the command echo ~ executed by user alice. What will it output?
Linux CLI
echo ~
A/home/alice
B~
C/root
D/home
Attempts:
2 left
💡 Hint
The tilde (~) expands to the current user's home directory.
💻 Command Output
intermediate
2:00remaining
What does this command output?
What is the output of echo ~root when run by a normal user?
Linux CLI
echo ~root
A/root
B~root
C/home/root
DError: user not found
Attempts:
2 left
💡 Hint
The shell expands ~username to that user's home directory if it exists.
📝 Syntax
advanced
2:00remaining
Which command correctly changes directory to the home directory of user 'bob'?
You want to change directory to user bob's home using a shortcut. Which command is correct?
Acd ~ /bob
Bcd ~/bob
Ccd ~bob
Dcd ~bob/
Attempts:
2 left
💡 Hint
The tilde followed immediately by username expands to that user's home directory.
🧠 Conceptual
advanced
2:00remaining
What does the shortcut ~+ represent in the shell?
In bash and similar shells, what does the shortcut ~+ expand to?
AThe root directory
BThe previous working directory
CThe current working directory
DThe home directory
Attempts:
2 left
💡 Hint
Think about what the shell variable PWD holds.
💻 Command Output
expert
2:00remaining
What is the output of this command sequence?
Given the commands run in order:
cd /tmp
echo ~-
What will be printed?
Linux CLI
cd /tmp
echo ~-
A/
B/home/alice
C/tmp
D~-
Attempts:
2 left
💡 Hint
The shortcut ~- expands to the previous working directory.

Practice

(1/5)
1. What does the ~ symbol represent in Linux command line?
easy
A. The previous directory
B. The root directory
C. The current user's home directory
D. The system's temporary folder

Solution

  1. Step 1: Understand the meaning of ~

    The tilde symbol ~ is a shortcut that always points to the current user's home directory, like /home/username.
  2. Step 2: Compare with other directory symbols

    / is root, - is previous directory, and /tmp is temporary folder, so they don't match ~.
  3. Final Answer:

    The current user's home directory -> Option C
  4. Quick Check:

    ~ = home directory [OK]
Hint: Remember: ~ always means your home folder [OK]
Common Mistakes:
  • Confusing ~ with root directory /
  • Thinking ~ means previous directory
  • Assuming ~ is a system folder
2. Which of the following commands correctly lists files in the user's home directory using the shortcut?
easy
A. ls ~
B. ls ./home
C. ls /home
D. ls /root

Solution

  1. Step 1: Identify the shortcut for home directory

    ~ is the shortcut for the current user's home directory, so ls ~ lists files there.
  2. Step 2: Check other options

    /home is the parent folder for all users, ./home is a relative path that may not exist, and /root is root user's home, not current user.
  3. Final Answer:

    ls ~ -> Option A
  4. Quick Check:

    ls ~ lists home files [OK]
Hint: Use ls ~ to list your home files quickly [OK]
Common Mistakes:
  • Using absolute path /home instead of ~
  • Confusing relative path ./home with home directory
  • Trying to list /root which is for root user only
3. What is the output of the command echo ~/Documents if the user's home directory is /home/alice?
medium
A. /home/alice/Documents
B. /Documents
C. ~/Documents
D. /root/Documents

Solution

  1. Step 1: Understand how shell expands ~

    The shell replaces ~ with the full path of the user's home directory, here /home/alice.
  2. Step 2: Combine with the rest of the path

    Appending /Documents to /home/alice gives /home/alice/Documents.
  3. Final Answer:

    /home/alice/Documents -> Option A
  4. Quick Check:

    echo ~/Documents = /home/alice/Documents [OK]
Hint: Shell expands ~ to full home path before command runs [OK]
Common Mistakes:
  • Thinking ~ stays as literal tilde
  • Confusing root's home /root with user's home
  • Ignoring path concatenation after ~
4. A user runs cd ~username but gets an error. What is the likely cause?
medium
A. The home directory is not mounted
B. The tilde shortcut only works for current user
C. The command syntax is incorrect
D. The username does not exist on the system

Solution

  1. Step 1: Understand ~username usage

    ~username expands to the home directory of the specified user if that user exists.
  2. Step 2: Identify error cause

    If the username does not exist, the shell cannot expand ~username, causing an error.
  3. Final Answer:

    The username does not exist on the system -> Option D
  4. Quick Check:

    ~username needs valid user [OK]
Hint: Check if username exists before using ~username [OK]
Common Mistakes:
  • Assuming ~ only works for current user
  • Thinking syntax is wrong when it is correct
  • Ignoring user existence in error
5. You want to create a script that copies a file named notes.txt from your home directory to a backup folder inside your Documents. Which command correctly uses the ~ shortcut?
hard
A. cp notes.txt ~/Documents/backup/
B. cp ~/notes.txt ~/Documents/backup/
C. cp ~/notes.txt /Documents/backup/
D. cp /notes.txt ~/Documents/backup/

Solution

  1. Step 1: Identify source file path

    The file notes.txt is in the home directory, so ~/notes.txt correctly points to it.
  2. Step 2: Identify destination folder path

    The backup folder is inside Documents in home, so ~/Documents/backup/ is the correct destination path.
  3. Step 3: Check other options

    /notes.txt is root directory file (likely wrong), /Documents/backup/ is absolute path outside home, and notes.txt without path means current directory, not home.
  4. Final Answer:

    cp ~/notes.txt ~/Documents/backup/ -> Option B
  5. Quick Check:

    Use ~ for home paths in source and destination [OK]
Hint: Use ~ before filenames to refer to your home folder [OK]
Common Mistakes:
  • Using absolute paths outside home by mistake
  • Omitting ~ and assuming current directory is home
  • Confusing source and destination paths
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard