How does Ls work internally? A deep dive into files, devices and OS

Aditi SitaulaAditi Sitaula
5 min read

When you’re working with Linux, one of the most common commands you'll use is ls. Whether you're listing files in your home directory, exploring device files in /dev, or examining the contents of a system directory, ls is an essential tool. But have you ever wondered how the Linux operating system works behind the scenes when you type ls? What exactly happens when it lists devices, files, and directories?

In this blog post, we’ll take a deep dive into the mechanics of the ls command, exploring how the Linux OS manages to display files and devices and the systems it interacts with to get the job done.

1. What Happens When You Type ls?

At a high level, the command simply lists the contents of a directory. But the way it retrieves this information is much more intricate. When you type ls in the terminal and press Enter, the following sequence of events takes place:

  1. User Input: You type ls into the shell.

  2. Shell Execution: The shell (e.g., Bash) executes the ls command.

  3. System Calls: The ls command invokes system calls to interact with the filesystem and retrieve the list of files and directories.

  4. Displaying Results: The results are displayed on your terminal.

Let’s look more into this now:

At its core, the ls command interacts with the operating system through system calls. Specifically, when you invoke ls, the command uses system calls like opendir() to open the directory and readdir() to read its contents. These system calls allow the kernel to access the filesystem and retrieve the list of files and directories. Each entry listed by ls is a directory entry, and these entries may correspond to regular files, subdirectories, or even special device files.

Now, the files and directories you see listed come from the filesystem that the Linux kernel manages. Whether you're dealing with files in /home, etc these are all part of a structure the kernel keeps track of. Files have metadata that tells you their name, who owns them, and when they were last modified. And when you run ls, Linux looks up that metadata and serves it to you in a neat, readable list. But here’s where it gets interesting: when you run ls in a place like /dev, you're not just listing regular files – you're looking at device files. These special files represent hardware devices (like storage drives, network interfaces, or serial ports) that Linux manages via the kernel. It’s like a backstage pass to the hardware.

You might think /dev is just a random collection of weird filenames, but each one is a device that the kernel is managing. For example, sda could represent your first hard drive, tty might be a terminal, and eth0 could be your Ethernet card. These device files aren’t regular files you can open with a text editor; they act as communication channels between software and hardware. In fact, on Linux, you interact with hardware through these files – so you can think of them as the Linux equivalent of the "plug" you use to connect with the physical world.

But wait – what if I told you that not all files in Linux are even on our hard drive? Yes, Linux has these clever virtual filesystems, like /sys, that let you peek behind the curtain and see the inner workings of the kernel in real-time. When you run ls in these directories, you're not seeing files stored on disk but files that the kernel dynamically creates to give you information. /sys is full of stuff like device details, kernel parameters, and driver information.

Now, if you’ve been using Linux for a while, you’ve probably noticed that ls is pretty speedy. It’s not always checking the hard drive to get the list of files – that would be way too slow. Instead, Linux uses caching to speed things up. If you've recently accessed a directory, Linux stores that information in memory. So the next time you run ls, it can grab the list from memory instead of fetching it from disk. This is why ls can list large directories almost instantly – Linux is using its memory smartly to reduce the need for slow disk access. It's like having a personal assistant who remembers exactly where you last put your keys (or in this case, your files).Of course, there's also the whole issue of permissions. Linux is big on security, and when you run ls, it checks your permissions for each file and directory. If you don’t have permission to see a file, ls simply won’t show it.

And here’s the kicker: ls doesn’t just show you files that are on your computer – it can list device files, too. Run ls in /dev, and you'll see entries for things like sda. These are device files that represent hardware devices on your system, like your hard drive or your network interface. It might look like any other file, but in reality, these are special files that the Linux kernel uses to interface with the hardware. It's like the OS giving you a window into the hardware without needing you to plug in cables.

In conclusion, the ls command is way more than just a simple tool for listing files. It’s a gateway to understanding how Linux interacts with both the filesystem and the hardware. Whether you’re looking at regular files, special device files, or even virtual filesystems, ls showing you a glimpse of how the kernel manages the system. Next time you type ls, take a moment to appreciate all the behind-the-scenes magic that makes it happen – because it’s not just showing you files, it’s giving you a sneak peek into the heart of the Linux operating system.

0
Subscribe to my newsletter

Read articles from Aditi Sitaula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditi Sitaula
Aditi Sitaula