Let's Rewrite Linux 'ls' command: Command, file types and flags
This article is part of a series.
Note: For the whole series, Zsh is used, for which, output can be different compared other shell profiles.
Introduction
The ls
command is one of the most commonly used commands in Linux/UNIX. This command is used to list the contents of a directory. Without any flags, we have the following output:
What are these colorful file names? Well, let’s find out!
File Types
There are 8 main types of files in Linux:
- Regular files: Any file with no special “powers” are considered regular file. These files include text, image, video etc. files. Color code: None
- Executable files: Files that can be executed/run. Color code: Green
- Directory files: Practically folders. They store the location of other files. Color code: Blue
- Block or Character special files: Hardware devices (hard, CD/DVD drives) are stored in Linux as special files. Color code: Yellow + Black background
- Link Files: Files pointing to other files. Basically, shortcuts. Color code: Cyan/Sky Blue (Red + Black Background if link is broken)
- Socket files: Communication endpoint for applications. Two applications can share data via socket files with each other. Color code: Magenta
- Named pipe files: Also used for communication between applications. Difference is that socket communication is bidirectional, unlike pipe files. Also, pipes can be created between related processes (for ex. parent and child processes), however, there is no limitation for sockets. Color code: Light Yellow + Black Background
- Archive files: Basically compressed/archived files. Color code: Red
There are more of course, but we will not dive into that. For more info, you can check this answer.
Flags
Now that we know which file types we have, let’s dig into command ls
more. According to the manual, there are many flags for the command. The most used ones and the ones that we will rewrite are:
- -a: Show hidden entries (a.k.a. files starting with
.
) - -A: Do not show current and previous directories (
.
and..
) - -C: List entries in tabular format (by columns)
- -c: with
-lt
: sort by, and show, ctime (time of last modification of file status information); with-l
: show ctime and sort by name; otherwise: sort by ctime. - -d: List directories themselves, not their contents
- -f: List all files in their order in directory, without any colors
- -F: Append indicator (kind of like extension) to the end of filename, depending their type (one of */=>@|)
- -g: Same as
-l
, but do not show owner. - -G: In list view, do not show group
- -h: In list view, show file size in human-readable form (2G, 12M, etc.)
- -i: Print inode (index number) of each file
- -l: Show entries in long list format
- -m: Show list as comma-separated values
- -n: Same as
-l
, but show owner and group ID - -p: Put ‘/’ at the end of directory names
- -Q: All file names will be inside quotes
- -r: Reverse order for sorting
- -R: List all subdirectories recursively.
- -S: Sort file by size, largest first
- -t: Sort by time, newest first
- -u: with
-lt
: sort by, and show, access time with-l
: show access time and sort by name otherwise: sort by access time. - -U: Do not sort, list entries in directory order
- -X: Sort alphabetically by file extensions
- -1: List one file per line
What is the plan?
We will rewrite ls
command and its above-mentioned flags step-by-step, in C. We will deep-dive into Linux filesystem, permissions, users/groups etc. Moreover, we will use Test Driven Development and modular programming to ensure we write the most optimal code. To be honest, I am thrilled with this project, and I hope you are too!
This article is part of a series.
comments powered by Disqus