Bird
Raised Fist0
Linux CLIscripting~10 mins

Home directory (~) and shortcuts in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Home directory (~) and shortcuts
Start at any directory
↓
Use ~ to represent home
↓
Commands use ~ as shortcut
↓
Shell expands ~ to /home/username
↓
Command runs in home directory path
The shell replaces ~ with your home directory path before running commands, letting you quickly access your home folder.
Execution Sample
Linux CLI
cd ~
ls ~
echo ~
This code changes to your home directory, lists its contents, and prints the home directory path.
Execution Table
StepCommandShell ExpansionActionOutput
1cd ~cd /home/userChange directory to home
2ls ~ls /home/userList files in home directoryDocuments Downloads Music Pictures
3echo ~echo /home/userPrint home directory path/home/user
4exitStop executionEnd of commands
💡 Commands end after echo ~; shell expands ~ to full home directory path each time.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
PWD (current directory)/any/dir/home/user/home/user/home/user/home/user
~ (home shortcut)N/A/home/user/home/user/home/user/home/user
Key Moments - 3 Insights
Why does the shell replace ~ with /home/user before running commands?
Because ~ is a shortcut for your home directory, the shell expands it to the full path so commands know where to go, as shown in execution_table rows 1-3.
What happens if you type 'cd ~' from any directory?
You move to your home directory regardless of your current location, as seen in execution_table step 1 where PWD changes to /home/user.
Does ~ work inside commands like ls and echo?
Yes, the shell expands ~ in any command argument before running it, shown in steps 2 and 3 where ls and echo use /home/user.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current directory after step 1?
A/any/dir
B/root
C/home/user
D/tmp
💡 Hint
Check the 'PWD (current directory)' value after step 1 in variable_tracker.
At which step does the shell print the full home directory path?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in execution_table for the echo command.
If you replace ~ with /home/user manually in commands, how does the execution table change?
ACommands will fail because ~ is required
BNo change in output, only shell expansion step is skipped
COutput will be different because /home/user is not recognized
DShell will add ~ again automatically
💡 Hint
Refer to execution_table rows 1-3 where shell expands ~ to /home/user before running commands.
Concept Snapshot
Use ~ as a shortcut for your home directory.
Shell replaces ~ with full path like /home/user before running commands.
Works in cd, ls, echo, and many commands.
Makes navigation and file access faster and easier.
You can always type ~ instead of full home path.
Full Transcript
This lesson shows how the tilde (~) symbol works as a shortcut for your home directory in Linux shell commands. When you type commands like 'cd ~', 'ls ~', or 'echo ~', the shell automatically replaces ~ with your full home directory path, such as /home/user. This lets you quickly move to or list files in your home folder without typing the full path. The execution table traces three commands: changing directory to home, listing home contents, and printing the home path. The variable tracker shows how the current directory changes to /home/user after 'cd ~'. Key moments clarify why the shell expands ~ and how it works in different commands. The quiz tests understanding of directory changes and shell expansions. Remember, ~ is a handy shortcut to save typing and speed up navigation in the terminal.

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