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.
ls [OPTION]... [FILE]...
Try it
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.
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
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 -ashows them;ls *does not match them because a leading dot is not matched by*unless you setdotglob. - Sorting follows the locale. With
LC_COLLATE=en_US.UTF-8the order ignores case and punctuation, soBmay come aftera. SetLC_ALL=Cfor plain byte order, which is also what scripts should do when order matters. - Do not parse
lsoutput in scripts. File names can contain spaces, newlines and other surprises. Use a glob (for f in *.txt) orfind -print0instead. Uselsfor people, not programs. - When the output is a pipe
lsprints one name per line automatically, as if you had given-1. ls -lsize 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; usedufor that.- Times:
-lshows modification time by default.-uswitches to access time and-cto inode change time. - Aliases: many distributions alias
lstols --color=auto. Runtype lsto see whether yours does. A backslash (\ls) bypasses the alias. - Long listings of a symlink show
name -> target. Add-Lto see the target's details instead. - Counts:
ls | wc -lcounts visible entries (and is wrong for names with newlines);ls -A | wc -lincludes hidden ones.
Related
cd,pwd,mkdir,touch- Lessons: Navigating the filesystem, Wildcards and globbing, File permissions