Skip to content

Commit a92115c

Browse files
authored
improvements to the ls command - hacktoberfest (#385)
* docs: Add SELinux support details for the `ls` command * docs: Enhance `ls` command documentation with additional flags and examples
1 parent 2e4dd7f commit a92115c

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

ebook/en/content/001-the-ls-command.md

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ ls
1717
ls {Directory_Path}
1818
```
1919

20+
3. List all files including hidden ones in long format with human-readable sizes:
21+
22+
```
23+
ls -lah
24+
```
25+
26+
4. Sort files by modification time, newest first:
27+
28+
```
29+
ls -lt
30+
```
31+
32+
5. List files by size, largest first:
33+
34+
```
35+
ls -lhS
36+
```
37+
38+
6. Show only directories in the current path:
39+
40+
```
41+
ls -d */
42+
```
43+
44+
7. List all text files with details:
45+
46+
```
47+
ls -lh *.txt
48+
```
49+
50+
8. Recursively list all files in subdirectories:
51+
52+
```
53+
ls -R
54+
```
55+
2056
### Syntax:
2157

2258
```
@@ -44,6 +80,131 @@ In this interactive tutorial, you will learn the different ways to use the `ls`
4480
|`-d`|`--directory`|Instead of listing the files and directories inside the directory, it shows any information about the directory itself, it can be used with `-l` to show long formatted information|
4581
|`-F`|`--classify`|Appends an indicator character to the end of each listed name, as an example: `/` character is appended after each directory name listed|
4682
|`-h`|`--human-readable`|like `-l` but displays file size in human-readable unit not in bytes|
83+
|`-i`|`--inode`|Display inode number for each file|
84+
|`-R`|`--recursive`|List subdirectories recursively|
85+
|`-1`|<center>-</center>|List one file per line|
86+
|`-c`|<center>-</center>|Sort by change time (when file metadata was last changed)|
87+
|`-u`|<center>-</center>|Sort by access time (when file was last accessed)|
88+
|`-X`|<center>-</center>|Sort alphabetically by file extension|
89+
|<center>-</center>|`--color=auto`|Colorize output to distinguish file types|
90+
|`-g`|<center>-</center>|Like `-l` but without showing owner|
91+
|`-o`|<center>-</center>|Like `-l` but without showing group|
92+
|<center>-</center>|`--group-directories-first`|List directories before files|
93+
94+
95+
### File Type Indicators:
96+
97+
When using the `-F` or `--classify` flag, `ls` appends special characters to filenames to indicate their type:
98+
99+
|**Indicator**|**File Type**|**Example**|
100+
|:---|:---|:---|
101+
|`/`|Directory|`docs/`|
102+
|`*`|Executable file|`script.sh*`|
103+
|`@`|Symbolic link|`link@`|
104+
|`\|`|FIFO (named pipe)|`pipe\|`|
105+
|`=`|Socket|`socket=`|
106+
|No indicator|Regular file|`file.txt`|
107+
108+
**Example:**
109+
110+
```bash
111+
ls -F
112+
documents/ script.sh* link@ data.txt pipe| socket=
113+
```
114+
115+
116+
### SELinux Support on Red Hat-Based Systems:
117+
118+
On Red Hat-based distributions (RHEL, CentOS, Fedora, Rocky Linux, AlmaLinux) that use SELinux, the `ls` command provides additional options to display SELinux security context information:
119+
120+
|**Short Flag** |**Long Flag** |**Description** |
121+
|:---|:---|:---|
122+
|`-Z`|`--context`|Display SELinux security context for files and directories|
123+
|`-lZ`|<center>-</center>|Show long format with SELinux security context|
124+
125+
**Example Output:**
126+
127+
```bash
128+
ls -Z
129+
unconfined_u:object_r:user_home_t:s0 file.txt
130+
unconfined_u:object_r:user_home_t:s0 directory
131+
```
132+
133+
```bash
134+
ls -lZ
135+
-rw-rw-r--. 1 user user unconfined_u:object_r:user_home_t:s0 1234 Jan 15 10:30 file.txt
136+
drwxrwxr-x. 2 user user unconfined_u:object_r:user_home_t:s0 4096 Jan 15 10:25 directory
137+
```
138+
139+
The SELinux context format is: `user:role:type:level`
140+
141+
**Note:** The `-Z` option is only functional on systems with SELinux enabled. On non-SELinux systems, this option may not be available or will show no additional information.
142+
143+
144+
### Understanding Long Format Output:
145+
146+
When using `ls -l`, each line displays detailed information about a file or directory:
147+
148+
```bash
149+
-rw-r--r-- 1 user group 1234 Jan 15 10:30 file.txt
150+
```
151+
152+
**Column breakdown:**
153+
154+
1. **File Type and Permissions** (`-rw-r--r--`):
155+
- First character: File type (`-` = regular file, `d` = directory, `l` = symlink, `b` = block device, `c` = character device, `p` = pipe, `s` = socket)
156+
- Next 9 characters: Permissions in three groups (owner, group, others)
157+
- `r` = read, `w` = write, `x` = execute, `-` = no permission
158+
159+
2. **Link Count** (`1`): Number of hard links to the file
160+
161+
3. **Owner** (`user`): Username of the file owner
162+
163+
4. **Group** (`group`): Group name that owns the file
164+
165+
5. **Size** (`1234`): File size in bytes (use `-h` flag for human-readable format like `1.2K`, `3.4M`)
166+
167+
6. **Modification Time** (`Jan 15 10:30`): Date and time the file was last modified
168+
169+
7. **Filename** (`file.txt`): Name of the file or directory
170+
171+
**Example with human-readable sizes:**
172+
173+
```bash
174+
ls -lh
175+
drwxr-xr-x 2 user group 4.0K Jan 15 10:25 documents
176+
-rwxr-xr-x 1 user group 2.3M Jan 14 09:15 script.sh
177+
-rw-r--r-- 1 user group 15K Jan 16 14:30 data.csv
178+
```
179+
180+
181+
### Using Wildcards and Patterns:
182+
183+
The `ls` command supports wildcards for pattern matching:
184+
185+
|**Wildcard**|**Description**|**Example**|**Matches**|
186+
|:---|:---|:---|:---|
187+
|`*`|Matches any number of characters|`ls *.txt`|All files ending with `.txt`|
188+
|`?`|Matches exactly one character|`ls file?.txt`|`file1.txt`, `file2.txt`, but not `file10.txt`|
189+
|`[]`|Matches any character within brackets|`ls file[123].txt`|`file1.txt`, `file2.txt`, `file3.txt`|
190+
|`[!]`|Matches any character NOT in brackets|`ls file[!3].txt`|`file1.txt`, `file2.txt`, but not `file3.txt`|
191+
|`{}`|Matches any of the comma-separated patterns|`ls *.{jpg,png,gif}`|All image files with these extensions|
192+
193+
**Examples:**
194+
195+
```bash
196+
# List all PDF and DOCX files
197+
ls *.{pdf,docx}
198+
199+
# List files starting with 'test' followed by a single digit
200+
ls test?.log
201+
202+
# List all files starting with uppercase letters
203+
ls [A-Z]*
204+
205+
# List all hidden configuration files
206+
ls .config*
207+
```
47208

48209

49210
### SELinux Support on Red Hat-Based Systems:
@@ -97,3 +258,78 @@ Customizing command behavior in Linux is easy using the `alias` command. To make
97258
3. **Verification**: Open a new terminal session, and the `ls` command will display files as configured.
98259
99260
261+
### Performance Tips:
262+
263+
**1. Avoid recursive listing on large directories:**
264+
- The `-R` flag can be slow on directories with many subdirectories and files
265+
- Consider using `find` command for more control: `find /path -type f`
266+
267+
**2. Disable color output in scripts:**
268+
- Use `--color=never` when piping output or in scripts to improve performance
269+
- Example: `ls --color=never | grep pattern`
270+
271+
**3. Limit output for large directories:**
272+
- Combine with `head` to see only first few entries: `ls -1 | head -n 20`
273+
- Or use `ls -1 | wc -l` to just count files without displaying them
274+
275+
**4. Use specific paths instead of wildcards when possible:**
276+
- `ls /var/log/syslog` is faster than `ls /var/log/sys*` when you know the exact filename
277+
278+
279+
### Common Use Cases:
280+
281+
**1. Find the largest files in a directory:**
282+
283+
```bash
284+
ls -lhS | head -n 10
285+
```
286+
287+
**2. List only directories:**
288+
289+
```bash
290+
ls -d */
291+
```
292+
293+
Or with full details:
294+
295+
```bash
296+
ls -ld */
297+
```
298+
299+
**3. Count files in a directory:**
300+
301+
```bash
302+
ls -1 | wc -l
303+
```
304+
305+
**4. List files modified in the last 24 hours:**
306+
307+
```bash
308+
ls -lt | head
309+
```
310+
311+
**5. Show files sorted by extension:**
312+
313+
```bash
314+
ls -lX
315+
```
316+
317+
**6. List files with their inode numbers (useful for debugging):**
318+
319+
```bash
320+
ls -li
321+
```
322+
323+
**7. Display directory contents one per line (useful for scripting):**
324+
325+
```bash
326+
ls -1
327+
```
328+
329+
**8. Combine multiple sort options (size + reverse):**
330+
331+
```bash
332+
ls -lhSr
333+
```
334+
335+

0 commit comments

Comments
 (0)