Bird
Raised Fist0
Linux CLIscripting~5 mins

ls options (-l, -a, -h, -R) in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
The ls command shows files and folders in a directory. Using options like -l, -a, -h, and -R helps you see more details, hidden files, human-readable sizes, and contents of subfolders.
When you want to see detailed information about files like size, permissions, and modification date.
When you need to find hidden files that start with a dot in a folder.
When you want file sizes shown in easy-to-read units like KB or MB instead of bytes.
When you want to list all files in a folder and all its subfolders recursively.
Commands
This command lists files with detailed info like permissions, owner, size, and modification date.
Terminal
ls -l
Expected OutputExpected
total 12 -rw-r--r-- 1 user user 123 Apr 10 10:00 file1.txt -rwxr-xr-x 1 user user 4096 Apr 9 09:30 script.sh drwxr-xr-x 2 user user 4096 Apr 8 08:20 folder
→
-l - Show detailed file information
This command shows all files including hidden ones that start with a dot.
Terminal
ls -a
Expected OutputExpected
. .. .hiddenfile file1.txt script.sh folder
→
-a - Show hidden files
This command lists files with details and shows sizes in human-readable format like KB or MB.
Terminal
ls -lh
Expected OutputExpected
total 12K -rw-r--r-- 1 user user 123 Apr 10 10:00 file1.txt -rwxr-xr-x 1 user user 4.0K Apr 9 09:30 script.sh drwxr-xr-x 2 user user 4.0K Apr 8 08:20 folder
→
-l - Show detailed file information
→
-h - Show sizes in human-readable format
This command lists all files in the current folder and all subfolders recursively.
Terminal
ls -R
Expected OutputExpected
.: file1.txt script.sh folder ./folder: inside.txt
→
-R - List directories recursively
Key Concept

If you remember nothing else, remember: combining ls options lets you see exactly what files exist, their details, hidden files, and folder contents inside folders.

Common Mistakes
Using ls without -a and missing hidden files.
Hidden files starting with a dot won’t show up, so you might miss important config files.
Always add -a if you want to see all files including hidden ones.
Using ls -l without -h and seeing large numbers in bytes.
File sizes are hard to read because they show raw bytes.
Add -h to ls -l to see sizes in KB, MB, etc.
Expecting ls -R to show hidden files inside subfolders without -a.
ls -R alone won’t show hidden files in subfolders.
Combine -a with -R as ls -aR to see all files recursively.
Summary
ls -l shows detailed info about files like size and permissions.
ls -a shows hidden files that start with a dot.
ls -h makes file sizes easy to read by using KB, MB, etc.
ls -R lists all files inside folders and their subfolders.

Practice

(1/5)
1. What does the ls -a command do in Linux?
easy
A. Shows all files including hidden ones
B. Lists files with detailed information
C. Displays file sizes in human-readable format
D. Lists files recursively in subdirectories

Solution

  1. Step 1: Understand the -a option

    The -a option in ls shows all files, including hidden files that start with a dot.
  2. Step 2: Compare with other options

    Other options like -l show details, -h shows sizes human-readable, and -R lists recursively.
  3. Final Answer:

    Shows all files including hidden ones -> Option A
  4. Quick Check:

    -a means show hidden files [OK]
Hint: Remember: 'a' for all files including hidden [OK]
Common Mistakes:
  • Confusing -a with -l for detailed info
  • Thinking -a shows sizes in human format
  • Assuming -a lists files recursively
2. Which of the following is the correct syntax to list files with detailed info and human-readable sizes?
easy
A. ls -la
B. ls -lR
C. ls -h -a
D. ls -lh

Solution

  1. Step 1: Combine options for detailed and human-readable

    The -l option shows detailed info, and -h makes sizes human-readable. Together, ls -lh is correct.
  2. Step 2: Check option order and other choices

    A (ls -la) shows detailed info including hidden files but misses human-readable sizes. C (ls -h -a) misses detailed info. D (ls -lR) lists recursively, not human-readable sizes.
  3. Final Answer:

    ls -lh -> Option D
  4. Quick Check:

    -l + -h = detailed + human sizes [OK]
Hint: Use -l and -h together for details and friendly sizes [OK]
Common Mistakes:
  • Mixing up option order and meaning
  • Using -h without -l for details
  • Confusing -R with -h
3. What will be the output of ls -lR in a directory with two subfolders, each containing files?
medium
A. Lists files in current directory only
B. Shows file sizes in human-readable format
C. Lists files with details in current and all subdirectories
D. Lists hidden files only

Solution

  1. Step 1: Understand -lR options

    -l lists files with details, and -R lists recursively through all subdirectories.
  2. Step 2: Predict output behavior

    The command will show detailed info for files in the current directory and all files in subfolders recursively.
  3. Final Answer:

    Lists files with details in current and all subdirectories -> Option C
  4. Quick Check:

    -lR = detailed + recursive listing [OK]
Hint: Remember -R means recursive, -l means detailed [OK]
Common Mistakes:
  • Thinking -R lists hidden files only
  • Assuming -lR shows human-readable sizes
  • Believing it lists only current directory
4. You ran ls hR but got an error: "ls: cannot access 'hR': No such file or directory". What is the likely cause?
medium
A. Options must be combined without spaces
B. You typed options together without a dash
C. Options order is incorrect
D. You used uppercase letters instead of lowercase

Solution

  1. Step 1: Analyze the error message

    The error "ls: cannot access 'hR': No such file or directory" suggests the shell interpreted 'hR' as a file or directory name.
  2. Step 2: Identify correct option syntax

    Options must be preceded by a dash and can be combined like -hR. Typing ls hR without dash causes the shell to treat 'hR' as a filename.
  3. Final Answer:

    You typed options together without a dash -> Option B
  4. Quick Check:

    Options need dash prefix [OK]
Hint: Always prefix options with a dash (-) [OK]
Common Mistakes:
  • Using uppercase instead of lowercase options
  • Putting spaces between dash and options
  • Assuming option order matters
5. You want to list all files including hidden ones, with detailed info and human-readable sizes, recursively through all subdirectories. Which command should you use?
hard
A. ls -alhR
B. ls -lha
C. ls -aRl
D. ls -l -a -h

Solution

  1. Step 1: Identify needed options

    You need -a for hidden files, -l for detailed info, -h for human-readable sizes, and -R for recursive listing.
  2. Step 2: Combine options correctly

    All options can be combined as -alhR. ls -alhR: ls -alhR includes all needed options.
  3. Step 3: Check other options

    B (ls -lha) misses recursive -R. C (ls -aRl) includes all options but order does not matter in Linux, so it is also correct. D (ls -l -a -h) misses recursive -R.
  4. Final Answer:

    ls -alhR -> Option A
  5. Quick Check:

    All needed options combined = ls -alhR [OK]
Hint: Combine all needed options with one dash for full listing [OK]
Common Mistakes:
  • Forgetting -R for recursion
  • Not combining options properly
  • Using options without dash
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