Rewriting "ls" #1: Command, file types and flags
Table of contents
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!
Subscribe to my newsletter
Read articles from Miradil Zeynalli directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Miradil Zeynalli
Miradil Zeynalli
💬 Who am I? I am Miradil Zeynalli, a graduate of ADA University, one of the leading universities in Baku, Azerbaijan. Currently studying Embedded Electronics Engineering in Lund University, Sweden. Have Embedded Software Engineer experience of 3.5 years, and one year experience as Lead Django Developer. 🔭 What else do I do? Besides embedded engineering, I am also interested in Data Science and Machine Learning. In the last year of bachelor's, I did my Senior Design Project in "Statistical Analysis and Visualization of BP DTS data", where we worked with data from oil wells of British Petroleum. Furthermore, I developed a timetable program that uses AI for assigning time slots to courses. 🤔 How do I use my time? During my leisure time, I prefer to read (I love Dan Brown’s books), watch movies, and sometimes read articles about quantum computing. As a former professional chess player (and the world champion), I actively play chess on online platforms. Feel free to challenge me! In fact, right here, right now! See below :) ⚡ Fun facts Favorite movie: The Dark Knight, Twelve Angry Men Favorite actor: Morgan Freeman Favorite author: Dan Brown Favorite book: The Da Vinci Code Favorite sports: Chess, Basketball