Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.

Listing Files in Linux

Linux provides powerful commands for listing files and directories, allowing you to view and manage your filesystem efficiently. This guide covers the most commonly used commands for listing files, their options, and practical examples.

ls
  • Lists files and directories in the current directory.
  • By default, output is sorted alphabetically.

Sample Output

Desktop Documents Downloads file.txt script.sh

Common ls Options

CommandDescription
ls -lLong listing (detailed info)
ls -aIncludes hidden files (those starting with .)
ls -lhHuman-readable sizes (K, M, G)
ls -ltSort by modification time
ls -RRecursively list subdirectories
ls -lSSort by file size
ls --color=autoEnables colored output (default in most distros)

Example

ls -lh

Sample Output:

-rw-r--r--. 1 user user 12K Jul 22 09:00 file.txt -rwxr-xr-x. 1 user user 2.1K Jul 22 08:59 script.sh drwxr-xr-x. 2 user user 4.0K Jul 21 17:23 Documents

Explanation of Fields in ls -l

-rwxr-xr-x. 1 user user 2.1K Jul 22 08:59 script.sh │ │ │ │ │ │ └── File name │ │ │ │ │ └── Last modified date/time │ │ │ │ └── File size │ │ │ └── Group owner │ │ └── Owner │ └── Number of hard links └── File type and permissions

Here’s a detailed explanation of file types in Linux as shown by the first character in ls -l output:

File Types in Linux (1st Character of ls -l Output)

SymbolFile TypeDescriptionExample
-Regular fileA standard file that contains text, data, code, etc.-rw-r--r-- 1 user user 123 file.txt
dDirectoryA folder that contains files or other directoriesdrwxr-xr-x 2 user user 4096 folder/
lSymbolic linkA shortcut or reference to another filelrwxrwxrwx 1 user user 11 link -> file.txt
bBlock deviceHardware device that handles data in blocks (e.g., hard drives)brw-rw---- 1 root disk 8, 1 /dev/sda1
cCharacter deviceDevice that handles data as a stream of bytes (e.g., keyboard, serial ports)crw-rw---- 1 root tty 4, 64 /dev/ttyS0
pNamed pipe (FIFO)A method for inter-process communicationprw-r--r-- 1 user user 0 pipe
sSocketFile used for inter-process communication via a network or local socketsrwxr-xr-x 1 user user 0 socket

How to View File Types

You can use:

ls -l

Or, to see file types clearly:

ls -lF

The -F option appends special symbols to filenames:

  • / → directory
  • * → executable
  • @ → symlink
  • | → FIFO
  • = → socket

Example

$ ls -l total 0 -rw-r--r-- 1 user user 0 Jul 22 10:00 file.txt drwxr-xr-x 2 user user 4096 Jul 22 10:00 mydir lrwxrwxrwx 1 user user 10 Jul 22 10:01 link -> file.txt prw-r--r-- 1 user user 0 Jul 22 10:01 mypipe srwxr-xr-x 1 user user 0 Jul 22 10:01 mysocket

Understanding ls Output Colors

If ls shows colors (enabled by default on most Linux systems), here’s what they mean:

ColorFile Type
BlueDirectory
GreenExecutable
CyanSymbolic Link
RedArchive or Broken Symlink
YellowBlock or Character Device
MagentaImage or Media File
Grey/DullHidden or Unreadable File
WhiteRegular File

You can force colored output using:

ls --color=always

Or disable it:

ls --color=never

touch – Creating Files

Basic Usage

touch filename
  • Creates a new empty file.
  • If the file already exists, it updates the timestamp (modification time).

Example

touch hello.txt

Creates an empty file called hello.txt.

Verify with ls -l:

ls -l hello.txt
-rw-r--r--. 1 user user 0 Jul 22 10:30 hello.txt
  • File is empty (0 bytes).
  • Permissions: rw- for owner, r-- for group and others.

Creating Multiple Files

touch file1.txt file2.txt file3.txt

Updating Timestamps Only

touch existing_file.txt

This does not change the content — only the modification time.

Summary Table

CommandPurposeExampleSample Output / BehaviorExplanation
lsLists files/directories in current directorylsDesktop Documents DownloadsBasic alphabetical listing
ls -lLong listing format with detailed infols -l-rw-r--r-- 1 user user 2.1K Jul 22 08:59 file.txtIncludes permissions, links, owner, group, size, date, and name
ls -aShows hidden filesls -a.bashrc .config DocumentsLists files starting with .
ls -lhHuman-readable sizesls -lh12K, 2.1K instead of bytesFile sizes in KB, MB, etc.
ls -ltSorts by modification timels -ltNewest files appear firstUseful for recent changes
ls -lSSorts by file sizels -lSLargest files appear firstHelpful for disk usage tracking
ls -RRecursively lists directoriesls -RIncludes contents of all subdirectoriesUseful for full tree views
ls --color=autoEnables colored outputls --color=autoFiles are color-coded by typeBlue: dir, Green: executable, Cyan: symlink, etc.
ls -lFAppends symbol to indicate file typels -lF/ for dirs, * for executables, @ for symlinks, `` for FIFOHelps visually distinguish file types
touchCreates a new file or updates timestamptouch hello.txtCreates empty file, or updates timestamp if it existsFile shown as: -rw-r--r-- 1 user user 0 Jul 22 10:30 hello.txt
touch file1.txt file2.txtCreates multiple empty filestouch file1 file2Files created instantlyNo content inside, size is 0
Last updated on