What ls prints by default
Run ls with no arguments and it lists the entries in the current directory, skipping names that start with a dot. When the output goes to a terminal, the names are arranged in columns, sorted alphabetically by the current locale, and often colored by file type. When the output goes to a pipe or a file, ls prints one name per line. This difference is deliberate. The program checks whether standard output is a terminal before it decides how to format the list.
The default view is for people. Its names are useful, but they do not tell you what kind of entry each name is, how large it is, or who may change it. For that you need the long format.
ls
ls -l /etc | head -n 5Reading the long listing column by column
The flag -l asks for one line per entry with these fields in order: the file type and permission bits, the number of hard links, the owner name, the group name, the size in bytes, the modification time, and the name. A symbolic link shows the target after an arrow. The first line of output is often a total in blocks, which you can ignore for now.
| Column | Meaning |
|---|---|
| First character | File type: - regular, d directory, l symbolic link, and others covered later in this unit |
| Next nine characters | Permission bits for owner, group, and others, read in threes |
| Second field | Hard link count. For a directory, it counts subdirectories plus two |
| Owner and group | Names looked up from the numeric user and group IDs stored in the inode |
| Size | Length in bytes. For a directory it is the size of the entry table, not the contents |
| Time | Modification time by default, shown in your locale |
Every column comes from the inode, except the name, which comes from the directory entry. The next units explain why that split matters.
Hidden files, all files, and directories
A name that starts with a dot is hidden by convention. The flag -a shows every entry, including . and ... The flag -A shows the hidden entries but leaves out . and .., which is usually what you want. When you pass a directory as an argument, ls lists its contents. The flag -d changes that: it shows the directory entry itself, which is necessary when you want to read the permissions of a directory rather than the files inside it.
ls -ld /workspace
ls -lA /workspaceSorting and sizes
By default the entries are sorted by name. The flag -S sorts by size, largest first, and -t sorts by modification time, newest first. Add -r to reverse any order. The flag -h prints sizes in human units such as K and M, but it changes only the presentation, not the order. These options combine: ls -lhtr shows the newest entry last, which is convenient at the bottom of a long listing.
The flag -R recurses into subdirectories. For a large tree, the find command from a later module is more precise, because it gives you tests on names, sizes, and times and it does not print the directory headers that make recursive output hard to parse.
Use ls -1 when you want one name per line in a predictable format, for example to pipe into wc -l or grep. Even so, scripts should prefer globs or find over parsing ls, because file names can contain spaces and newlines.
Reading times and sizes correctly
The time column in a long listing is the modification time, and its format changes with the age of the file. Recent files show the time of day, while older files show the date and the year. The rule is intentional, and it helps a person scan a listing. It does not help a script, which should ask for an exact value with stat instead. The option --time-style in GNU ls lets you choose a fixed format, such as full ISO times, when you need the output to be stable to read.
Sizes need the same care. The number in the size column is the length of the file in bytes, which is the logical size. It is not the amount of disk space the file occupies. A file of one byte still occupies at least one block, and a file with holes may occupy much less than its logical size. The flag -s prints the allocated size in blocks, and the difference between the two numbers is a quick way to see whether a file is sparse. Combine -s with -h when you want a readable allocation.
cd /workspace
truncate -s 10M sparse.img
ls -lh sparse.img
ls -sh sparse.imgThe first listing shows a logical size of ten megabytes, while the second shows how much space is actually allocated, which is close to zero because the file has no written data. This distinction explains why copying a sparse file without special options can make it much larger.
Why scripts should not parse ls
The output of ls -l is designed for a person, and its format is not a stable interface. Column widths change with the data, the time format depends on the locale and on how old the file is, and a name that contains a newline breaks any line-based parser. A safe script uses a glob such as *.txt, which the shell expands before the program runs, or it uses find -print0 with a null-delimited consumer. Use ls for looking at things and the other tools for doing things with them.
Practice in the lab
In the lab you will create a small directory, list it three ways, sort it by size, and save each result in /workspace. Notice how the hidden file appears only with -a or -A, and how the order changes with -S.