Bird
Raised Fist0
Linux CLIscripting~10 mins

ls (list files and directories) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ls (list files and directories)
User types 'ls'
↓
Shell runs 'ls' command
↓
System reads current directory
↓
System lists files and folders
↓
Output displayed on screen
↓
User sees list of files and directories
The 'ls' command lists all files and directories in the current folder and shows them on the screen.
Execution Sample
Linux CLI
ls
This command lists all files and directories in the current folder.
Execution Table
StepActionSystem StateOutput
1User types 'ls' and presses EnterWaiting for command
2Shell receives 'ls' commandPreparing to execute 'ls'
3System reads current directory contentsDirectory contents: ['file1.txt', 'folderA', 'script.sh']
4System formats list of files and directoriesFormatted list ready
5System outputs list to screenOutput displayedfile1.txt folderA script.sh
6Command endsReady for next command
💡 All files and directories in current folder listed and displayed
Variable Tracker
VariableStartAfter Step 3After Step 4Final
commandnone'ls''ls'none
directory_contentsempty['file1.txt', 'folderA', 'script.sh']['file1.txt', 'folderA', 'script.sh']empty
outputemptyempty'file1.txt folderA script.sh''file1.txt folderA script.sh'
Key Moments - 3 Insights
Why does 'ls' only show files and folders in the current directory?
Because 'ls' by default lists contents of the directory you are currently in, as shown in step 3 of the execution_table where the system reads the current directory.
What happens if the directory is empty?
The output will be empty because the system finds no files or folders to list, so step 5 would output nothing.
Why is the output shown all on one line?
By default, 'ls' formats the output in columns or a single line for easy reading, as seen in step 4 where the system formats the list before displaying.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the system state after step 3?
ADirectory contents: ['file1.txt', 'folderA', 'script.sh']
BWaiting for command
COutput displayed
DReady for next command
💡 Hint
Check the 'System State' column in row with Step 3 in execution_table
At which step does the system output the list to the screen?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the 'Output displayed' action in the execution_table
If the directory was empty, how would the output in step 5 change?
AIt would show 'No files found'
BIt would show an error message
CIt would show nothing (empty output)
DIt would list hidden files only
💡 Hint
Refer to key_moments about empty directory output
Concept Snapshot
ls command:
- Lists files and directories in current folder
- Usage: ls
- Shows names separated by spaces
- Does not show hidden files by default
- Output appears on screen immediately
Full Transcript
The 'ls' command is used to list all files and directories in the folder you are currently in. When you type 'ls' and press Enter, the shell runs the command. The system reads the contents of the current directory, gathers the names of files and folders, formats them, and then displays them on the screen. If the directory is empty, nothing is shown. This command helps you see what is inside your current folder quickly.

Practice

(1/5)
1. What does the ls command do in Linux?
easy
A. Creates a new directory
B. Deletes files from the current folder
C. Lists files and directories in the current folder
D. Moves files to another folder

Solution

  1. Step 1: Understand the purpose of ls

    The ls command is used to show what files and folders are inside a directory.
  2. Step 2: Identify the correct description

    Among the options, only listing files and directories matches what ls does.
  3. Final Answer:

    Lists files and directories in the current folder -> Option C
  4. Quick Check:

    ls = list files [OK]
Hint: Remember: ls = list files and folders [OK]
Common Mistakes:
  • Confusing ls with commands like rm or mkdir
  • Thinking ls deletes or moves files
  • Assuming ls creates directories
2. Which of these is the correct syntax to list all files including hidden ones in a directory?
easy
A. ls -l
B. ls -r
C. ls -h
D. ls -a

Solution

  1. Step 1: Recall option meanings for ls

    -a shows all files including hidden (those starting with a dot).
  2. Step 2: Match option to requirement

    Only ls -a lists hidden files; others do different things like long format or reverse order.
  3. Final Answer:

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

    -a means all files [OK]
Hint: Use -a to see hidden files starting with . [OK]
Common Mistakes:
  • Using -l instead of -a to see hidden files
  • Confusing -r (reverse) with -a
  • Thinking -h shows hidden files
3. What will be the output of the command ls -l /tmp if the /tmp directory contains two files named file1.txt and file2.log?
medium
A. file1.txt file2.log
B. Detailed list showing permissions, owner, size, and date for file1.txt and file2.log
C. Only hidden files in /tmp
D. An error saying /tmp does not exist

Solution

  1. Step 1: Understand -l option with ls

    The -l option shows a detailed list including permissions, owner, size, and modification date.
  2. Step 2: Apply to /tmp directory contents

    Since /tmp has two files, the command will show detailed info lines for both files.
  3. Final Answer:

    Detailed list showing permissions, owner, size, and date for file1.txt and file2.log -> Option B
  4. Quick Check:

    ls -l = detailed list [OK]
Hint: -l means long detailed listing [OK]
Common Mistakes:
  • Expecting just filenames without details
  • Thinking it lists only hidden files
  • Assuming it shows an error if directory exists
4. You run ls -la /home/user but get an error: ls: cannot access '/home/user': No such file or directory. What is the most likely cause?
medium
A. /home/user directory does not exist
B. You forgot to add the -a option
C. You need to run the command as root
D. The ls command is misspelled

Solution

  1. Step 1: Analyze the error message

    The error says the directory '/home/user' cannot be accessed because it does not exist.
  2. Step 2: Check other options

    Options -a and running as root do not cause this error; misspelling ls would cause a different error.
  3. Final Answer:

    /home/user directory does not exist -> Option A
  4. Quick Check:

    Directory missing = cannot access error [OK]
Hint: Check if directory exists before blaming options [OK]
Common Mistakes:
  • Assuming missing -a causes error
  • Thinking root is always needed
  • Confusing command misspelling with directory missing
5. You want to list all files in the directory /var/log sorted by modification time, newest first, including hidden files. Which command should you use?
hard
A. ls -lat /var/log
B. ls -ltr /var/log
C. ls -al /var/log
D. ls -lh /var/log

Solution

  1. Step 1: Identify options needed

    -a shows hidden files, -t sorts by modification time, newest first, and -l gives detailed listing.
  2. Step 2: Match options to command

    Only ls -lat /var/log combines all needed options correctly.
  3. Final Answer:

    ls -lat /var/log -> Option A
  4. Quick Check:

    -lat = list all, sort by time [OK]
Hint: Use -lat to list all files sorted by time [OK]
Common Mistakes:
  • Using -ltr which sorts oldest first
  • Forgetting -a to show hidden files
  • Using -lh which shows size but not sorting
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