pavement

Ls

From FreeBSDwiki
Jump to: navigation, search

Equivalent to Windows' dir. ls is commonly used with flags or switches to alter its behaviour and/or output. Flags can be given as separate switches (ls -a -l /etc/) but can be combined to make things faster (ls -la). ls takes pretty much the whole alphabet as a flag, but you'll likely never need or want to use more than say 5 or 6 different flags.

Most common flags:

-a -- lists all files, including hidden files
-l -- gives long listing, including permissions, owner, group and size
-F -- shows a slash (/) immediately after each directory, 
      an asterisk (*) after  executable files, 
      an at sign (@) after symbolic links, 
      an equals sign (=) after sockets, 
      a percent sign (%) after whiteouts, 
      and a pipe (|) after a FIFO.
-R -- list subdirectories encountered recursively
-h -- When used with the -l option, use unit suffixes for sizes so as to make them human-readable. 
      No, Virgina, you do not need to count everything in bytes.
-c -- sort files by the last time file state was changed/modified
-u -- sort files by the last time the file was accessed
-G -- colorize output (different colors for different types of files)

Using ls with other programs

ls can (and is) used in conjunction with grep by using a pipe to send ls output -- which can easily fill your screen with enough entries to make you cross-eyed -- to more easily find what you're looking for. To give an example, let's say you're looking for a file that begins with name in your /etc directory. Doing an ls /etc gives you far too many entries and ls -l the same thing but scrolling past you even faster. You could do a

dave@samizdata% ls | more

and look for all files that begin with name. But why waste your time going over each entry?

dave@samizdata% ls -la | grep name

will give you a long list of all files that contain the string

Changing the default behavior of ls

If you want to change the way ls behaves by default, you'll want to make an alias for ls including the arguments you want (for example, ls -Gah to provide colorized output, including dotfiles, with human-readable filesize info) in your shell configuration file.

Personal tools