Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux ls command

Reference for the Linux ls command: list files and directories, long format, hidden files, sorting, human-readable sizes, colors and common mistakes.

ls lists the names of files and directories. Without arguments it shows the contents of the current directory, sorted alphabetically by the rules of your locale, with hidden files (names that start with a dot) left out. Given a file name it prints that name; given a directory it prints what is inside.

Most of the power of ls comes from a handful of options: -l for details, -a for hidden entries, -h for readable sizes, and the sort flags -t, -S and -r. Use ls -d when you want information about a directory itself instead of its contents.

Terminal
ls [OPTION]... [FILE]...

Try it

Bash Long format, hidden files and sorting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Directories themselves and globs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
ls [-aAbcdfFghHiklLmnNopqrRsStuUvxX1] [--color[=WHEN]] [FILE]...
Option Description
-l Long format: type and permissions, link count, owner, group, size, modification time, name.
-a Include entries that start with ., including . and ...
-A Like -a but without . and ...
-h With -l or -s, print sizes as 1.5K, 3.2M (powers of 1024).
-d Show directories themselves, not their contents.
-R List subdirectories recursively.
-t Sort by modification time, newest first.
-S Sort by size, largest first.
-X Sort by file extension.
-v Natural sort of numbers in names (file2 before file10).
-r Reverse the sort order.
-1 One name per line (the default when output is not a terminal).
-F Append an indicator: / directory, * executable, @ symlink, `
-p Append only / to directories.
-i Show inode numbers.
-s Show allocated blocks for each file.
-L Show information for the target of a symlink, not the link.
--color=auto Color output when writing to a terminal.
--time-style=STYLE Format of times with -l, for example long-iso or +%Y-%m-%d.
--group-directories-first List directories before files.

Returns: exit status 0 on success, 1 for minor problems (such as an inaccessible subdirectory while listing recursively), 2 for serious trouble (such as a missing command-line argument).

Usage notes

  • Hidden files are only a naming convention. ls -a shows them; ls * does not match them because a leading dot is not matched by * unless you set dotglob.
  • Sorting follows the locale. With LC_COLLATE=en_US.UTF-8 the order ignores case and punctuation, so B may come after a. Set LC_ALL=C for plain byte order, which is also what scripts should do when order matters.
  • Do not parse ls output in scripts. File names can contain spaces, newlines and other surprises. Use a glob (for f in *.txt) or find -print0 instead. Use ls for people, not programs.
  • When the output is a pipe ls prints one name per line automatically, as if you had given -1.
  • ls -l size is in bytes (or rounded units with -h). For directories the size is the size of the directory entry, not the total of the files inside; use du for that.
  • Times: -l shows modification time by default. -u switches to access time and -c to inode change time.
  • Aliases: many distributions alias ls to ls --color=auto. Run type ls to see whether yours does. A backslash (\ls) bypasses the alias.
  • Long listings of a symlink show name -> target. Add -L to see the target's details instead.
  • Counts: ls | wc -l counts visible entries (and is wrong for names with newlines); ls -A | wc -l includes hidden ones.