ShareYourGroupShareYourGroupCommands

Linux and Mac commands

ls: list the files in a folder

ls lists what is in a folder. It is the command you will type more than any other, and it changes nothing, so it is safe to experiment with.

The plain listing

Linux or Mac terminal
asha@pc:~/demo$ ls
log.txt
marks.csv
notes.txt
old
photos
report.txt

Just names, in alphabetical order. On most systems folders and files are shown in different colours.

The long listing

-l gives one line per item, with details.

Linux or Mac terminal
asha@pc:~/demo$ ls -l
total 4
-rw-r--r-- 1 asha asha 222 Nov  1 15:00 log.txt
-rw-r--r-- 1 asha asha  53 Nov  1 15:00 marks.csv
-rw-r--r-- 1 asha asha  51 Nov  1 15:00 notes.txt
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 old
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 photos
-rw-r--r-- 1 asha asha  94 Nov  1 15:00 report.txt

Reading one line from the left:

PartMeaning
-rw-r--r--the type and the permissions. A leading d means a folder, - an ordinary file
1the number of links, which you can ignore
asha ashathe owner and the group
51the size in bytes
Nov 1 15:00when it was last changed
notes.txtthe name

The permission letters are explained on the page about chmod.

Hidden files

Names that begin with a dot are hidden from a plain ls. Settings files are usually named this way. -a shows all.

Linux or Mac terminal
asha@pc:~/demo$ touch .settings
asha@pc:~/demo$ ls
log.txt
marks.csv
notes.txt
old
photos
report.txt
asha@pc:~/demo$ ls -a
.
..
.settings
log.txt
marks.csv
notes.txt
old
photos
report.txt

The entries . and .. mean this folder and the one above it.

Sizes people can read

-h, with -l, shows sizes as K, M and G instead of plain bytes.

Linux or Mac terminal
asha@pc:~/demo$ ls -lh
total 4.0K
-rw-r--r-- 1 asha asha 222 Nov  1 15:00 log.txt
-rw-r--r-- 1 asha asha  53 Nov  1 15:00 marks.csv
-rw-r--r-- 1 asha asha  51 Nov  1 15:00 notes.txt
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 old
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 photos
-rw-r--r-- 1 asha asha  94 Nov  1 15:00 report.txt

Newest first

-t sorts by time, newest first. -r reverses any order. Together, -ltr puts the most recently changed file at the bottom, next to the prompt, which is where you want it in a long folder.

Linux or Mac terminal
asha@pc:~/demo$ ls -lt
total 4
-rw-r--r-- 1 asha asha 222 Nov  1 15:00 log.txt
-rw-r--r-- 1 asha asha  53 Nov  1 15:00 marks.csv
-rw-r--r-- 1 asha asha  51 Nov  1 15:00 notes.txt
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 old
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 photos
-rw-r--r-- 1 asha asha  94 Nov  1 15:00 report.txt

Sort by size with -S, largest first.

Linux or Mac terminal
asha@pc:~/demo$ ls -lS
total 4
-rw-r--r-- 1 asha asha 222 Nov  1 15:00 log.txt
-rw-r--r-- 1 asha asha  94 Nov  1 15:00 report.txt
-rw-r--r-- 1 asha asha  53 Nov  1 15:00 marks.csv
-rw-r--r-- 1 asha asha  51 Nov  1 15:00 notes.txt
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 old
drwxr-xr-x 1 asha asha   0 Nov  1 15:00 photos

Another folder, and patterns

Linux or Mac terminal
asha@pc:~/demo$ ls photos
beach.jpg
hills.jpg
asha@pc:~/demo$ ls *.txt
log.txt
notes.txt
report.txt
asha@pc:~/demo$ ls -l photos/*.jpg
-rw-r--r-- 1 asha asha 39 Nov  1 15:00 photos/beach.jpg
-rw-r--r-- 1 asha asha 39 Nov  1 15:00 photos/hills.jpg

A star matches any run of characters, and a question mark exactly one. The shell expands the pattern before ls runs, so ls *.txt really receives the list of matching names.

Everything below

-R lists this folder and every folder inside it.

Linux or Mac terminal
asha@pc:~/demo$ ls -R
.:
log.txt
marks.csv
notes.txt
old
photos
report.txt

./old:
draft.txt

./photos:
beach.jpg
hills.jpg

Folders marked

-F adds a slash after folder names, which helps when colours are not available. -d lists a folder itself instead of its contents.

Linux or Mac terminal
asha@pc:~/demo$ ls -F
log.txt
marks.csv
notes.txt
old/
photos/
report.txt
asha@pc:~/demo$ ls -ld photos
drwxr-xr-x 1 asha asha 0 Nov  1 15:00 photos

Combining options

Single-letter options can be joined after one dash. ls -lah is the favourite of many people: long, all, human-readable.

OptionEffect
-llong listing
-ainclude hidden files
-hhuman-readable sizes
-tsort by time, newest first
-Ssort by size, largest first
-rreverse the order
-Rinclude subfolders
-Fmark folders with a slash
-1one name per line

Counting files

Pipe the list into wc -l to count lines.

Linux or Mac terminal
asha@pc:~/demo$ ls | wc -l
6

When a file is not there

Linux or Mac terminal
asha@pc:~/demo$ ls nothing.txt
ls: cannot access 'nothing.txt': No such file or directory

The message means exactly what it says. Check the spelling, remember that capital letters matter on Linux, and make sure you are in the folder you think you are in with pwd.

Names with spaces

Put the name in quotation marks, or put a backslash before each space: ls "My Documents" or ls My\ Documents. Pressing Tab after the first letters completes a name correctly, spaces included.

On a Mac

The Mac's ls is a slightly different version from the one on Linux. The options on this page work on both. Colours are switched on with ls -G on a Mac and ls --color on Linux.

The Windows equivalent is dir.

Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.

This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.