CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / ls

ls

List directory contents

Beginneressentialfilesystemnavigation

ls shows what's in a directory. With a few options it also shows permissions, owners, sizes and times, hidden files, and lets you sort by size or age: the first thing you check on any box.

Syntaxls [OPTION]... [FILE]...

Watch it run

You've just logged in to a server and want to see what's in your home directory and an app folder.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
ls

Plain ls prints names only, in columns, sorted alphabetically. The colours come from --color=auto: blue is a directory, green an executable file.

Anatomy of a command

$
commandls

The program that lists directory entries.

How it works

ls opens the directory and reads its entries with the getdents system call. A directory is just a list of names, each pointing to an inode number. For a plain listing that is all ls needs. With -l it also calls stat (or statx) on every entry to read the inode: type and permission bits, owner and group IDs (turned into names through /etc/passwd and /etc/group), size, link count and timestamps. That is why ls -l on a directory with a million files is slow while plain ls is fast, and why a listing can show a name whose stat fails (a broken symlink or a file deleted mid-listing).

A directory only stores names and inode numbers; -l reads every inode to fill in the other columns.

rungetdentsread entriesstat eachuid -> nameprintYouls -l /applscoreutilsKernel VFSsyscalls/app directoryname -> inodeInodesmode, uid, size/etc/passwduid -> nameTerminalformatted rows
1/6
You run ls -l

The shell starts the ls program with two arguments: the option -l and the path /app.

← → to step

Options you'll actually use

-lLong format: type, permissions, links, owner, group, size, mtime, name
-aInclude hidden entries (names starting with .), plus . and ..
-ALike -a, but without . and ..
-hHuman-readable sizes with -l: 1.1K, 23M, 4.2G
-tSort by modification time, newest first
-SSort by size, largest first
-rReverse the sort order (oldest or smallest first)
-RRecurse into subdirectories
-dList a directory itself, not its contents
-iShow each entry's inode number
-1One name per line (handy in scripts and pipes)
--color=autoColour by type: blue directories, green executables, cyan links

Common mistakes

ls -l | grep '^d' to count directories, then parsing columns with cut

ls output is for humans: names with spaces or newlines break column parsing, and the format differs between systems.

find . -maxdepth 1 -type d | wc -l
ls -lS /var/log to find what's filling the disk

ls only sizes the files in that one directory; a 30 GB subdirectory shows as 4096 bytes.

du -sh /var/log/* | sort -h
ls dir/*.log when there are 200,000 log files

The shell expands the glob first and can fail with 'Argument list too long' before ls even runs.

find dir -name '*.log' | head
Reading 'total 32' as 32 files

total is the number of 1K disk blocks used by the listed files, not a count of entries.

ls -A | wc -l

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List everything in your home directory, including hidden files, with human-readable sizes.

  2. 2

    In /var/log, show the three largest files.

  3. 3

    Show the permissions of the /tmp directory itself, not its contents. What is the extra letter at the end?

  4. 4

    Find out whether /usr/bin/python3 is a real file or a link, and where it points.

Related commands

cdChange the current working directoryfindSearch the filesystem by name, size, age, owner and morestatShow a file's inode: size, permissions, owner and timestampsduFind out which files and directories use the space
Nextcd
Linux / File & Directory Management / ls

ls

List directory contents

Beginneressentialfilesystemnavigation

ls shows what's in a directory. With a few options it also shows permissions, owners, sizes and times, hidden files, and lets you sort by size or age: the first thing you check on any box.

Syntaxls [OPTION]... [FILE]...

Watch it run

You've just logged in to a server and want to see what's in your home directory and an app folder.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
ls

Plain ls prints names only, in columns, sorted alphabetically. The colours come from --color=auto: blue is a directory, green an executable file.

Anatomy of a command

$
commandls

The program that lists directory entries.

How it works

ls opens the directory and reads its entries with the getdents system call. A directory is just a list of names, each pointing to an inode number. For a plain listing that is all ls needs. With -l it also calls stat (or statx) on every entry to read the inode: type and permission bits, owner and group IDs (turned into names through /etc/passwd and /etc/group), size, link count and timestamps. That is why ls -l on a directory with a million files is slow while plain ls is fast, and why a listing can show a name whose stat fails (a broken symlink or a file deleted mid-listing).

A directory only stores names and inode numbers; -l reads every inode to fill in the other columns.

rungetdentsread entriesstat eachuid -> nameprintYouls -l /applscoreutilsKernel VFSsyscalls/app directoryname -> inodeInodesmode, uid, size/etc/passwduid -> nameTerminalformatted rows
1/6
You run ls -l

The shell starts the ls program with two arguments: the option -l and the path /app.

← → to step

Options you'll actually use

-lLong format: type, permissions, links, owner, group, size, mtime, name
-aInclude hidden entries (names starting with .), plus . and ..
-ALike -a, but without . and ..
-hHuman-readable sizes with -l: 1.1K, 23M, 4.2G
-tSort by modification time, newest first
-SSort by size, largest first
-rReverse the sort order (oldest or smallest first)
-RRecurse into subdirectories
-dList a directory itself, not its contents
-iShow each entry's inode number
-1One name per line (handy in scripts and pipes)
--color=autoColour by type: blue directories, green executables, cyan links

Common mistakes

ls -l | grep '^d' to count directories, then parsing columns with cut

ls output is for humans: names with spaces or newlines break column parsing, and the format differs between systems.

find . -maxdepth 1 -type d | wc -l
ls -lS /var/log to find what's filling the disk

ls only sizes the files in that one directory; a 30 GB subdirectory shows as 4096 bytes.

du -sh /var/log/* | sort -h
ls dir/*.log when there are 200,000 log files

The shell expands the glob first and can fail with 'Argument list too long' before ls even runs.

find dir -name '*.log' | head
Reading 'total 32' as 32 files

total is the number of 1K disk blocks used by the listed files, not a count of entries.

ls -A | wc -l

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List everything in your home directory, including hidden files, with human-readable sizes.

  2. 2

    In /var/log, show the three largest files.

  3. 3

    Show the permissions of the /tmp directory itself, not its contents. What is the extra letter at the end?

  4. 4

    Find out whether /usr/bin/python3 is a real file or a link, and where it points.

Related commands

cdChange the current working directoryfindSearch the filesystem by name, size, age, owner and morestatShow a file's inode: size, permissions, owner and timestampsduFind out which files and directories use the space
Nextcd