ls - list contents of directory

Hi! If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

ls command is one of the most commonly used tools in Unix. You simply cannot underestimate the importance of being able to confirm exactly what files and directories are available to you, and ls does its job perfectly.

ls - basic syntax

The most basic way to use this command is to simply make it list files and directories in your current directory. You don't need to specify any parameters for it, just type the command itself:

$ ls

ls - most common usage

If you're interested in a particular file or directory, you should specify the filename as a parameter to ls.

This command will simply output the filename of the file you have specified:

$ ls /etc/passwd

/etc/passwd

Similarly, it's possible to confirm that a certain file is not found:

$ ls /etc/passwd5

ls: /etc/passwd5: No such file or directory

You may also specify a directory (the output in this example is abridged) to confirm the contents of it:

base-3.00$ ls /etc

Muttrc

Net

X11

adduser.conf

adjtime

aliases

aliases.db

alternatives

apache2

apm

apt

at.deny

…

And finally, the most common way ls is used: the long version of the output, which is invoked using the -l parameter:

base-3.00$ ls -l /etc

total 1064

-rw-r–r– 1 root root   8063 Mar  8  2007 Muttrc

-rw-r–r– 1 root root    611 Mar  5  2007 Net

drwxr-xr-x 5 root root   4096 Sep  7 04:44 X11

-rw-r–r– 1 root root   2077 Aug  3  2006 adduser.conf

-rw-r–r– 1 root root     44 Aug  3  2006 adjtime

-rw-r–r– 1 root root     51 Mar 25  2007 aliases

-rw-r–r– 1 root root  12288 Sep  7 05:01 aliases.db

drwxr-xr-x 2 root root   4096 Sep  7 05:03 alternatives

drwxr-xr-x 8 root root   4096 Sep 26 03:02 apache2

drwxr-xr-x 6 root root   4096 Aug  3  2006 apm

drwxr-xr-x 4 root root   4096 Sep  7 04:34 apt

-rw-r—– 1 root root    144 Aug  3  2006 at.deny

Such output allows you to confirm the type of each directory entry, the access permissions, the number of links to this file, the ownership (user and unix group which own it), size in bytes, date of the last modification, and, finally, the name of the directory entry.

See also:

- basic Unix commands

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Fark
  • Furl
  • Netscape
  • Reddit
  • Smarking
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

7 comments ↓

#1 find - search for files and directories in Unix | Unix Commands on 02.07.08 at 10:08 am

[...] And if I want to verify that rpm really is the owner of some file from this list, I can use the ls command: [...]

#2 Unix symlink example | UNIX Tutorial: Learn UNIX on 02.14.08 at 11:07 pm

[...] you notice, the /tmp/file.2 has an "l" in the long-format output of the ls command, which confirms it's a symbolic link. You also can see right away where this symlink points [...]

#3 Finding the Owner of a File in Unix | UNIX Tutorial: Learn UNIX on 03.20.08 at 10:22 am

[...] It's very easy to confirm who the owner of a file is, and you can do it using the ls command. [...]

#4 cd - change directory | Unix Commands on 03.25.08 at 9:40 am

[...] you can probably tell from the error message, the problem is directory permissions. If we use the ls command to verify access permissions, we can see that in my example the /tmp/protected directory belongs to [...]

#5 Show What a Symlink Points To | UNIX Tutorial: Learn UNIX on 03.26.08 at 5:01 pm

[...] listing files, the most obvious choice is a Unix ls command, and the way you use it to list symlink is the same way you'd list any other [...]

#6 ln unix command - make links and symlinks between files | Unix Commands on 03.31.08 at 12:33 pm

[...] there is even a special counter for such references. When you're using the long format of an ls command, you can see this [...]

#7 atime, ctime and mtime in Unix filesystems | UNIX Tutorial: Learn UNIX on 04.11.08 at 12:33 pm

[...] The simplest way to confirm the times associated with a file is to use ls command. [...]

Leave a Comment