Kahibaro
Discord Login Register

Listing files

Basic `ls` Usage

The standard command to list files and directories is ls.

bash
  ls
bash
  ls /etc
  ls /var/log

ls shows only filenames by default, and typically omits “hidden” files (names starting with .).

Showing Hidden Files

In Linux, filenames beginning with . are treated as hidden (for example .bashrc, .config).

To include hidden files:

bash
ls -a
ls -a /home/user

Common variations:

Long Listings with Details

To see more information (permissions, owner, size, date) use the long format:

bash
ls -l
ls -l /etc

This adds one line per entry with multiple columns (you will learn what each means in the permissions/users chapters):

Example output (for illustration):

text
-rw-r--r-- 1 user user  1234 Dec  1 10:00 notes.txt
drwxr-xr-x 2 user user  4096 Dec  2 09:30 projects

Combine with -a or -A:

bash
ls -la
ls -lA

Human-Readable Sizes

To view file sizes in KB/MB/GB instead of raw bytes:

bash
ls -lh
ls -lh /var/log

You can combine multiple options:

bash
ls -lah    # long, all (including hidden), human‑readable

Indicating File Types

To quickly see what is a directory, executable, symlink, etc.:

bash
ls -F

This adds markers:

You can also combine:

bash
ls -lF

This gives a detailed listing with type indicators.

Sorting Output

By default, ls sorts entries alphabetically. You can change the sort order.

Common sorting options:

bash
  ls -lt
bash
  ls -ltr
  ls -lr
bash
  ls -lS

These options can be combined with others (like -h and -a):

bash
ls -lhS
ls -lahr

Recursively Listing Subdirectories

To list the contents of directories and all their subdirectories:

bash
ls -R
ls -R /etc

This prints a tree-like listing of each directory and its contents.

Use with caution in very large directory trees; it can produce a lot of output.

Coloring Output

Most modern distributions configure ls to use colors automatically, so different file types appear in different colors. If not, you can request it explicitly:

bash
ls --color=auto
ls --color=always

Often this behavior is set through an alias (for example, alias ls='ls --color=auto'), which you’ll see when learning shell configuration.

Listing a Single File or Directory

You can use ls to check details for a specific path:

bash
ls -l myfile.txt
ls -ld mydirectory

Note the difference:

Useful Combinations to Practice

Try these combinations and observe the differences:

bash
  ls
  ls -l
bash
  ls -a
  ls -la
bash
  ls -lhS      # biggest files first
  ls -lt       # newest modified first
  ls -ltr      # oldest modified first
bash
  ls -R
  ls -lR

Practice running these in different directories like your home directory, /etc, and /var/log to get comfortable reading the output.

Views: 24

Comments

Please login to add a comment.

Don't have an account? Register now!