Linux Commands GuideLinux Tutorials

Master the Linux ls Command: 16 Practical Examples and Options for Beginners

The Linux ls command is the fundamental tool for viewing directory contents on every Unix-like system. Whether you are an absolute beginner or an administrator brushing up on essentials, understanding ls options improves productivity and troubleshooting. This guide, focused on the main keyword “Linux ls command”, walks through 16 practical, real-world examples with clear outputs and concise explanations so you can quickly learn listing hidden files, sorting, recursive views, inode numbers, human-readable sizes, and more. Each example includes the exact command, sample output, and a short explanation to help you apply these options confidently in shells such as Bash, Zsh, and Dash.

1. List hidden files and directories

Display all files, including those starting with a dot (hidden files) in the current directory.

ls -a

.  ..  .bashrc  .profile  README.md  src  scripts  notes.txt

The -a flag shows all entries including . (current directory) and .. (parent directory). Hidden files begin with a dot; using -a is useful to inspect configuration files such as .bashrc or .profile.

2. List hidden files but skip . and ..

Show hidden files but suppress the special entries . and ...

ls -A

.bashrc  .profile  README.md  src  scripts  notes.txt

The -A option behaves like -a but omits the current and parent directory entries, making output cleaner when you only want actual hidden files and directories.

3. Display files in reverse alphabetical order

Reverse the default sort order to list the oldest or last entries first when combined with other sort flags.

ls -r

utils  tmp  scripts  src  README.md  notes.txt

The -r option reverses the sort order applied by ls (by default alphabetical). When paired with -t it lists oldest files first; with plain ls it simply flips alphabetical order.

4. Sort files by extension

Group files by their extension so similar types appear together.

ls -X

archive.tar  data.csv  report.pdf  image.png  logo.svg  script.sh  readme.txt

The -X flag sorts entries alphabetically by extension (the part after the last dot). This is helpful to quickly locate files of a specific type in a directory containing mixed formats.

5. Sort files by modification time (newest first)

See the most recently modified files at the top of the list.

ls -t

notes.txt  script.sh  README.md  src  scripts

The -t option sorts by modification time, newest first. Combine with -r to invert it and show the oldest first. This is useful for monitoring logs or recently edited files.

6. List subdirectories recursively

Show contents of the current directory and all nested subdirectories.

ls -R

.:
README.md  src  scripts  docs

./src:
main.c  utils.c  utils.h

./scripts:
build.sh  deploy.sh

The -R (recursive) option prints each directory's contents with headings for subdirectories. This is helpful when you need an overview of a project tree without switching folders.

7. Display inode numbers for files

Show filesystem inode numbers alongside filenames for low-level tasks and troubleshooting.

ls -i

1048577 README.md
1048592 src
1048594 scripts
1048600 notes.txt

The -i flag prints the inode number before each entry. Inodes uniquely identify files on a filesystem and are useful for identifying hard links, recovering files, or debugging filesystem issues.

8. Long listing: detailed file information

Get file permissions, owner, group, size, modification time, and name in a structured row format.

ls -l

-rw-r--r-- 1 alice staff  4096 Mar  2 10:12 README.md
drwxr-xr-x 2 alice staff  4096 Mar  2 09:58 src
-rwxr-xr-x 1 alice staff  2048 Mar  1 16:45 scripts/build.sh

The -l option produces a long listing: permissions, hard link count, owner, group, size (bytes), timestamp, and filename. Directory entries are indicated with a leading d in the permissions field.

9. Show author information with long listing

Include the file author when producing a long listing (supported on systems that track authors).

ls -l --author

-rw-r--r-- 1 alice bob  4096 Mar  2 10:12 README.md
drwxr-xr-x 2 alice bob  4096 Mar  2 09:58 src
-rwxr-xr-x 1 alice bob  2048 Mar  1 16:45 scripts/build.sh

The –author option, combined with -l, prints an author column if the system supports it. On many Linux distributions the author equals the owner, but some environments differentiate them.

10. Print C-style escapes for non-graphic characters

Reveal non-printable characters (like newlines or leading spaces) by printing C-style escape sequences.

ls -b

notes.txt  README.md  src

The -b option prints C-style escapes for non-graphic characters. For example, a newline in a filename appears as , and spaces might show up as \[space]. This helps avoid confusion when files contain special characters.

11. Show file sizes in specific units

Display file sizes using a specified block size unit such as kilobytes for easier reading.

ls -l --block-size=k

-rw-r--r-- 1 alice staff 4k Mar  2 10:12 README.md
drwxr-xr-x 2 alice staff 4k Mar  2 09:58 src

The –block-size=UNIT option forces size output into the chosen unit; here k prints kilobytes (k). Use M for megabytes, G for gigabytes, etc. Note: -l still reports the actual file size rounded to that unit.

12. Display only filenames with human-readable sizes

Show allocated size and name in a compact, human-friendly form.

ls -s -h

4.0K README.md
4.0K src
2.0K scripts/build.sh

The -s option shows the allocated size (disk usage) and the -h flag makes sizes human-readable (K, M, G). This compact view is useful when scanning disk usage quickly.

13. Exclude backup files from the listing

Suppress files that end with a tilde (~), commonly used as editor backup files.

ls -B

README.md  src  scripts

The -B option omits backup files that end with a tilde (~). This cleans up listings when editors create temporary backups like file.txt~.

14. Append file-type indicators

Show a character after each name that indicates the file type (directory, executable, symlink, etc.).

ls -F

README.md  src/  scripts/  deploy.sh*  link -> src

The -F flag appends indicators: / for directories, * for executables, @ for symbolic links, = for sockets, and | for named pipes. This is useful for quick visual identification of file types.

15. Change output format (comma-separated)

Produce a comma-separated, single-line listing useful for scripts or compact display.

ls --format=commas

README.md, src, scripts, notes.txt

The –format option accepts values like commas, single-column, vertical, and long. commas prints entries separated by commas, making it convenient to feed into parsers or include in short reports.

16. Hide a particular pattern of files

Exclude specific filename patterns from the output using glob-style patterns.

ls --hide=*.txt

README.md  src  scripts

The –hide=PATTERN option hides files matching the given shell pattern (glob). In the example, *.txt files are excluded from the listing. Use –hide when you want to ignore noisy file types during inspection.

Conclusion

Mastering the Linux ls command transforms how you interact with files and directories on the command line. The examples above cover common and practical options—from uncovering hidden files and sorting by time or extension to displaying human-readable sizes, inode numbers, and file-type indicators. Combine these flags (for example -la to see all files in long format, or -ltr to view oldest entries first) to tailor output to your workflow. Practice these commands in your home directory and on small project trees to gain confidence; knowing a few powerful ls options saves time and reduces errors when managing servers, development projects, or daily file tasks.

One Comment

  1. Great rundown of the ls command—could you add a quick example of using an alias (like alias ll='ls -lah –color=auto’) to make common options easier to remember?

Odgovori na Ethan Gonzalez Poništi odgovor

Vaša email adresa neće biti objavljivana. Neophodna polja su označena sa *