I learned DOS first. Back in the day, I knew it pretty well. Occassionally, the need still arises to throw together a batch file to automate some little thing. The mainstay of DOS commands is DIR - the directory command - which lists out files and folders.

Now, I'm using Linux Mint. In a terminal window ls is the equivalent of the DOS DIR command. I sometimes get aggravated that I don't remember the basic command line switches needed for ls. So, here's goes an article to help me learn ls a little better written from the perspective of someone who knew DOS first.

man ls - read the man page. Want to know more? Try info ls for a really scary about of information about the ls command.

ls: list

Dir means directory. Ls means list. It is hard to memorize a command of a couple of random letters mixed in with all the other linux commands. Ls means list. It lists the files and folder in a folder.

Ls by default prints out a list of files and folders. The output is arranged in columns that are sorted alphabetically.

Dir by default prints out a list of files and folders, but the list shows the date, time, file size, and file name.

  • ls is equivalent dir /w.
  • ls -l is equivalent to dir.

Output Format

The are actually a number of output formats available besides using the -l switch.

  • ls -x list files sorted horizontally
  • ls -m list files with commas in between
  • ls -l the verbose listing we've already mentioned
  • ls -1 (that's a number one) list the files in a single column
  • lc -C list files vertically (column sorted)

Linux is famous for chaining commands together. Many of the special output formats are designed with that purpose in mind. This allows you to send an output listing to the next command in a chain.

Basic Sorting

You can use all of the sort options without using the -l long listing. To me, that is visually confusing. The sorting options are more meaningful if the -l is included.

NOTE: Switches can be glued together. ls -lS is the same as ls -l -S

NOTE: Capitalization matters. ls -s is NOT the same as ls -S

  • -S = size - largest first
  • -t = time - modification date/time, newest first
  • -X = extension - alphabetical
  • -U = No sort - order on disk

Column Listings

So, how do you read this listing? There's quite a bit of useful information displayed about the file or folder.

ls -l sample.txt
-rw-rw-r--   1 keith users       462 Feb  9 08:38  sample.txt
----------   - ----- -----       --- ------------  ----------
    |        |   |     |          |       |          |
    |        |   |     |          |       |          +----- file name
    |        |   |     |          |       |
    |        |   |     |          |       +----- Modified date/time
    |        |   |     |          |
    |        |   |     |          +----- Size (in bytes)
    |        |   |     |
    |        |   |     +----- Group
    |        |   |
    |        |   +----- User (owner)
    |        |
    |        +----- Number of links
    |
    +----- Permissions

The Permissions column needs some additional explanation.

The first character represents the file type (there are more, but here are the common ones).

  • '-' is a regular file
  • 'd' is a directory (folder)
  • 'l' is a symbolic link

The remaining 9 characters are in groups of three and represent file permissions.

  • r is Read
  • w is Write
  • x is Execute
  • '-' is permission is not granted

The 3 groups of characters are:

  • user permissions (characters 2,3,4)
  • group permissions (characters 5,6,7)
  • other permissions (characters 8,9,10)

Extra Column Controls

ls -F addends a type indicator after the file name. The type indicators are:

  • * - executable file
  • / - a folder
  • = - a socket
  • > - a door
  • @ - symbolic link
  • | - a named pipe

OK, I admit it, I don't know what all of those mean. But, ls -F can be useful when you are NOT using the -l switch where the file type is the first character.

Comparison Chart

When you look at these, it's almost as if the DOS guys said, "OK, we need the equivalent of the linux commands, but they must be different"

NOTE: Including -l to get the "wide" style listing.

ls DOS dir Action
default /w list just the file names
-l default list files plus extra info (e.g. date, size)
Sorting
default /on sort by file name
-lr /o-n sort by file name, reverse alpha
-lS /o-s sort by size, largest first
-lSr /os sort by size, smallest first
-lt /o-d sort by date/time, newest first
-ltr /od sort by date/time, oldest first
-lX /oe sort by extension
-lXr /o-e sort by extension, reverse alpha
--group-directories-first /og group folders first
Other
-a show the "hidden" files that begin with a .
-lg drop the owner column from the wide listing
-lG drop the group column
-o same as previous, no group column
-R recursive - dives down into subfolders
-h human readable file sizes instead of bytes