Bird
Raised Fist0
Linux CLIscripting~5 mins

ls (list files and directories) in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to see what files and folders are in a place on your computer. The ls command shows you a list of these files and folders so you know what is there.
When you want to check what files are in your current folder before opening or editing them
When you need to find out if a file was saved in a specific directory
When you want to see hidden files that start with a dot in your folder
When you want to see detailed information like file size and modification date
When you want to list files sorted by time or size to find recent or large files
Commands
This command lists all visible files and folders in the current directory.
Terminal
ls
Expected OutputExpected
Documents Downloads example.txt Pictures Videos
This command lists all files including hidden ones that start with a dot.
Terminal
ls -a
Expected OutputExpected
. .. .bashrc Documents Downloads example.txt Pictures Videos
→
-a - Show all files including hidden ones
This command lists files with detailed information like permissions, owner, size, and modification date.
Terminal
ls -l
Expected OutputExpected
-rw-r--r-- 1 user user 1234 Apr 25 10:00 example.txt drwxr-xr-x 2 user user 4096 Apr 20 09:00 Documents drwxr-xr-x 3 user user 4096 Apr 22 15:30 Downloads
→
-l - Show detailed file information
This command lists files in long format sorted by modification time, newest first.
Terminal
ls -lt
Expected OutputExpected
-rw-r--r-- 1 user user 1234 Apr 25 10:00 example.txt drwxr-xr-x 3 user user 4096 Apr 22 15:30 Downloads drwxr-xr-x 2 user user 4096 Apr 20 09:00 Documents
→
-t - Sort files by modification time
→
-l - Show detailed file information
Key Concept

If you remember nothing else, remember: ls shows you what files and folders are in a directory, and flags like -a and -l help you see hidden files and details.

Common Mistakes
Running ls without knowing the current directory
You might look at the wrong folder and not find the files you expect
Use pwd to check your current directory before running ls
Expecting ls to show hidden files without -a flag
Hidden files starting with a dot won’t appear without -a
Add -a flag to see hidden files: ls -a
Using ls -l but not understanding the output columns
You might misinterpret file permissions, sizes, or dates
Learn the meaning of each column in ls -l output for correct understanding
Summary
ls lists files and folders in the current directory.
Use ls -a to include hidden files in the list.
Use ls -l to see detailed information about each file.

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