Skip to content

🐧 ls Command Mastery in Linux ​

Welcome to your deep dive into the ls command β€” one of the most fundamental tools for navigating Linux. This guide follows a Socratic learning style with prompts, outputs, and practical challenges.

πŸ‘£ Getting Started with ls ​

Task

What do you think ls does when run without any options? Try it:

bash
ls
Answer

Lists files and directories in the current working directory.

TIP

The default behavior does not show hidden files.

πŸ“‚ Listing with More Detail ​

bash
ls -l
Output
-rw-r--r-- 1 user group  1234 Jun  5 09:00 report.txt
drwxr-xr-x 2 user group  4096 Jun  5 09:01 projects

Task

Can you guess what each of the columns represent?

Answer

Permissions, links, owner, group, size, date, filename

πŸ™ˆ Show Hidden Files ​

bash
ls -a
Output
.  ..  .bashrc  .gitconfig  notes.txt

Pitfall

., .., and .* files are hidden by default.

πŸ“œ Combine Flags ​

bash
ls -la

TIP

Combining -l and -a gives you long listing including hidden files.

⏱ Sort by Time ​

bash
ls -lt
Output
-rw-r--r-- 1 user group  4096 Jun  6 11:11 today.txt
-rw-r--r-- 1 user group  1234 May 29 08:45 old.txt

Task

Can you guess what ls -ltr does?

Answer

Sorts by modification time in reverse order.

πŸ” Filter with Patterns ​

bash
ls *.txt

Task

Predict the output if your folder has these files: notes.txt, image.png, todo.txt, README.md

Answer
notes.txt  todo.txt

🌈 Colored Output ​

bash
ls --color=auto

TIP

Color helps differentiate between files, directories, executables, etc.

πŸ“¦ File Sizes with -lh ​

bash
ls -lh
Output
-rw-r--r-- 1 user group 1.2K Jun  6 09:15 example.txt

TIP

-h makes file sizes human-readable (K, M, G).

🎯 Practice Challenge 1 ​

Task

List all files in /var/log that are larger than 1 MB, sorted by size in descending order.

Hint

You’ll need ls, -lSh, and maybe grep or find for filtering.

🧭 Recursive Listing ​

bash
ls -R

Pitfall

This lists all files in all subdirectories β€” can be verbose!

🎲 Practice Challenge 2 ​

Task

List all .sh files in your home directory and subdirectories, showing details and hidden files.

Hint

Combine ls -laR with filtering.

βš™οΈ Advanced Use: Sort by Size & Type ​

bash
ls -lhS
bash
ls -lX

TIP

-S sorts by size, -X sorts by extension.

🎯 Practice Challenge 3 ​

Task

You want to clean up space. List the 5 largest files in /home including hidden ones, in human-readable format.

Hint

Use ls -lahS | head -n 5

πŸ§ͺ Bonus: Use with Aliases ​

bash
echo "alias ll='ls -alh'" >> ~/.bashrc && source ~/.bashrc

TIP

Now ll will give you a long listing of all files including hidden ones.

βœ… Summary ​

  • ls is versatile and works best with options.
  • Combine flags for power: -alh, -lt, -lhS
  • Don’t forget -R and --color=auto for visibility and depth.
  • Practice challenges make you internalize the real-world usage.

🧠 Final Challenge (Project Style) ​

Task

Create a script that logs the top 10 largest .log files from /var/log, sorted by size, including the date/time of the scan.

bash
#!/bin/bash
echo "Log scan on $(date)" > scan.log
ls -lhS /var/log/*.log | head -n 10 >> scan.log
cat scan.log

Happy hacking πŸ§πŸ’‘

Use the shell, Luke. πŸ”§