music
OSdata.com: programming text book 

OSdata.com

ls

summary

    This subchapter looks at ls, a UNIX (and Linux) command.

    ls is the UNIX (and Linux) list command.

free book on UNIX/Linux System Administration

Teach Yourself UNIX/Linux System Administration and Shell Programming

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

Google

ls

    This subchapter looks at ls, a UNIX (and Linux) command.

    ls is used to list files.

    The ls command was described in the first UNIX book, UNIX Programmer’s Manual, by Ken Thompson and Dennis Ritchie, published November 3, 1971.

list directory contents

    Use the ls command to list the contents of the current working directory (files and directories).

    $ ls
    Desktop          Movies        Send registration
    Documents        Music         Sites
    Downloads        Pictures
    Library          Public
    $

PC-DOS equivalent

    ls -la is the UNIX equivalent of the MS-DOS or PC-DOS command DIR. You can add the PC-DOS equivalent to your shell session with the alias command. To make the change permanent, add the following line to the .bashrc file in your home directory. Note that if you add this PC-DOS/MS-DOS equivalent, only add the all upper case version, because the lower case type is an important UNIX command that you will also need.

    $ alias DIR="ls -la"

    ls -la | less is the UNIX equivalent of the MS-DOS or PC-DOS command DIR /P.

list all

    Type ls with the -A option to get a list of all entries other than . (single dot) and .. (double dot).

    $ ls -A

list with indicators

    Type ls with the -F option to have special indicators for special files.

    $ ls -F
    Desktop/                Music/                  file01.txt
    Documents/              Pictures/               file02.txt
    Downloads/              Public/                 names
    Library/                Send registration@      numberfile.txt
    Movies/                 Sites/                  testdir/
    $

    A slash / will be added after every pathname that is a directory. An asterisk * will be added after every file that is executable (including scripts). An at sign @ will be displayed after each symbolic link. An equals sign = will be displayed after each socket. A percent sign % will be displayed after each whiteout. A vertical bar | will be displayed after each item than is a FIFO.

    ls -F is a builtin command in csh.

hidden files

    The -a option will give a list of all files, including hidden files.

    $ ls
    Desktop     Downloads   Movies   Pictures   scripts
    Documents   Library     Music    Public
    $ ls -a
    .               .cups       Library    scripts
    ..              .vminfo     Movies
    .DS_Store       Desktop     Music
    .Trash          Documents   Pictures
    -bash_history   Downloads   Public
    $

    The hidden or invisible files are hidden to make it less likely that they are accidentally corrupted or deleted.

list specific directory

    So far all of the examples have been on the current working directory. If you name a specific directory, you will get a listing for that directory/ You may use any of the flags you have alrady learned.

    $ ls Music
    GarageBand      iTunes
    $

multiple directories

    You can list multiple directories, each separated by a space character. This will give the listing for each directory, one at a time.

    $ ls Movies Music
    Movies:
    iMovie Projects.localized

    Music:
    GarageBand      iTunes
    $

common error

    The most common error is attempting to ls a directory or file that doesn’t exist.

separate lines

    The -1 option will cause each item on its own line. This is useful for scripts that will do further processing.

    $ ls -1
    Desktop
    Documents
    Downloads
    Library
    Movies
    Music
    Pictures
    Public
    scripts
    $

option grouping

    You can combine multiple options for a single command. For example, if you wanted to show visible and invisble files and show the file indicators, you can use both the -F and -a options together.

    $ ls -a -F
    ./              .cups/       Library/    scripts
    ../             .vminfo      Movies/
    .DS_Store       Desktop/     Music/
    .Trash/         Documents/   Pictures/
    -bash_history   Downloads/   Public/
    $

    You can group option by listing them in any order (with a few rare exceptions) and either with their own minus sign or after a single minus sign. All of the following versions of the ls command work the same way:

    $ ls -a -F
    $ ls -F -a
    $ ls -aF
    $ ls -Fa
    $

list specific file

    You can ls a specific file or files. This is primarily useful with other options that report additional information.

    $ ls filename

list specific directory

    You can ls a specific directory. It will list all of the files in the designated directory.

    $ ls directory

    You can the -d switch to have ls list just the named directory. This is pretty much useless by itself, but useful when combined with switches for specific kinds of information.

    $ ls -d directory

list files and directories

    You can ls a a combined list of specific files and directories.

    $ ls filename directory

colorized

    Use the -G option to colorize the output of ls.

    $ ls -G
    Desktop        Downloads      Movies         Pictures        file01.txt     names          scripts
    Documents      Library        Music          Public          forthfunctions numberfile.txt testdir
    $

    You can edit your .profile file to set specific colors for directories, sockets, pipes, symlinks, etc. You can also set colors for executable files. You can even set colors for specific file extensions.

    The default for Mac OS X (shown above) is royal blue for directories.

    Daniel Sandman wrote:

    I have this as an alias in my ~/.zshrc.

    alias ls="ls --color=auto -FshX"

list all subdirectory contents

    Use the -R switch to show all of the contents of all of the subdirectories. This can be a huge list. The -R stands for recursive.

    $ ls -R

long listing

    Use the -l switch for a long listing. Each file or directory is on its own line. One sample line shown:

    $ ls -l
    drwxr-xr-x    2 admin   staff   68 Aug 20 16:43 testdir
    $

    The order of information

    Mode file type (see chart below), file permissions (owner, group, other in the order of read, write, execute), possible special information.

bBlock special file
cCharacter special file
dDirectory
lSymbolic link
sSocket link
pFIFO (pipe)
-Regular file

    If the file or directory has extended attributes the permissions field is followed by the @ character.

    If the file or directory has extended security information (such as an Access Control List), the permissions field is followed by the + character.

    links The number of links.

    user The user name. If the user name is unknown, the numeric ID is shown. Adding the -n switch will force the numeric ID instead.

    group The group name. If the group name is unknown, the numeric ID is shown. Adding the -n switch will force the numeric ID instead.

    size The number of bytes. If the file is a character special or block special file, the major and minor device numbers replace the size field.

    date and time The abbreviated month, day-of-month, hour and minute the file was last modified. If the modification time is more than six (6) months past or future, then the year replaces the hour and minute fields.

    pathname The name of the file or directory. If the file is a symbolic link, the pathname of the linked-to file is preceded by ->.

    The first line (before the individual file entries) is the number of blocks used by the files in the directory.

list in reverse order

    Use the -r switch to show the results in reverse order.

    $ ls -r

list by time of modification

    Use the -t switch to show the results by time of modification (most recent first).

    $ ls -t

list by time of last access

    Use the -u switch to show the results by time of last access (most recent first).

    $ ls -u

list by time of file creation

    Use the -U switch to show the results by time of file creation (most recent first).

    $ ls -U

show time information

    Use the -T switch to show full time information: month, day, hour, minute, second, year. This switch needs to be comibined with the -l switch. Sample line below:

    $ ls -Tl
    drwxr-xr-x    2 admin   staff   68 Aug 20 16:43:15 2013 testdir
    $

show extended information

    Use the -@ switch to show extended attribute keys and sizes. This switch needs to be comibined with the -l switch.

    $ ls -@l

show Access Control List

    Use the -e switch to show the Access Control List (ACL) associated with a file when there is an ACL. This switch needs to be comibined with the -l switch.

    $ ls -el

show number of blocks

    Use the -s switch to show the size in blocks for each file. The UNIX default block size is 512 bytes, but thsi can very different on modern versions of UNIX or Linux.

    $ ls -s

sort by size

    Use the -S switch to sort the listing by size. This switch needs to be comibined with the -l switch.

    $ ls -Sl

advanced information

scripting gotchas

    This is an advanced item, but it is placed in this chapter so that you can easily find where to look it up when you need it.

    Do not use the output of ls in a BASH for loop.

    $ for i in $(ls *.mp3); do # Wrong!
    $    command $i # Wrong!
    $ done # Wrong!
    
    $ for i in $(ls *.mp3) # Wrong!
    $ for i in `ls *.mp3` # Wrong!

    If any file name has spaces in it, then it will be word split. So, the MP3 for Alex Clair’s “Too Close” (number four song on his “The Lateness of the Hour” album) might be 04 Too Close. In that case, your command would run on the files “04”, “Too”, and “Close”, rather than “04 Too Close”. Additional errors likely to follow. Listen to the song “Too Close” for free.

    Double quote won’t work either because that will cause the entire output of the ls command to be treated as a single item. Your loop will run once on all of the file names conactenated together.

    $ for i in "$(ls *.mp3)"; # Wrong!

    The solution is to run your loop without even using ls, relying on BASH’s filename expansion. Don’t forget to check for the possibility that there are no qualifying files in the current directory.

    $ for i in *.mp3; do
    $    [[ -f "$i" ]] || continue
    $    command "$i"
    $ done

    Thanks to Daniel Massimillano for a correction.

other

    On November 8, 2010, Ramesh Natarajan named this the number twelve (12) most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).

    In June 2009, Ken Milberg named this command as one of the Top 50 universal UNIX commands at this web page Top 50 Universal INIX commands. Note that this web page requires agreeing to be spammed before you can read it.


comments, suggestions, corrections, criticisms

please contact us

your name:
email address:
phone number:
message:

free music player coding example

    Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

    View music player in action: www.musicinpublic.com/.

    Create your own copy from the original source code/ (presented for learning programming).


return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

previous page next page
previous page next page

free book on UNIX/Linux System Administration

Teach Yourself UNIX/Linux System Administration and Shell Programming

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

    If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

more information on donating

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.


Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2012, 2013, 2014 Milo

    Created: January 22, 2012

    Last Updated: June 1, 2014


return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book

previous page next page
previous page next page