Listing directories, 7 practical examples

GNU/Linuxls is a utility that list information for any file type including directories, by default (when not passed any option) ls displays the contents of the current (or specified) DIR no recursively, ordered alphabetically and ignoring the hidden files/directories.

Synopsis

ls [Opciones] [Fichero]

Options:

-a: Show all files including hidden files
-B: Ignore files ending with ~
-d: list only directories
--full-time: displays the date in long format.
-g: omits the owner of each file
-G: omits the Group of each file.
-h: list size for each file in understandable format: K, M, or G
-i: inode number
-l: in addition to the name of the file prints the type of file and permissions
, number of hard links, owner, group, size and
date
-R: List files recursively
-t: list files sorted by date
-Z: Print the SELinux context

Note:
-Do not specify all options.
- Most of the options have their equivalent in the long format (--option)

Examples

To develop the following examples has been taken as a basis the directory structure of a Symfony2 standard distribution

  1. List the contents of the current dir
$ ls
app  bin  deps  deps.lock  LICENSE  README.md  src  web

2. List properties for the web directory

$ls -ld web/
drwx------+ 3 yoander yoander 4096 Jun 23 20:09 web/

3. Do not ignore hidden file

$ ls -a1 web/
.
..
app_dev.php
apple-touch-icon.png
app.php
bundles
config.php
favicon.ico
.htaccess
robots.txt

Note the use of the option -1 to display the result in a single column

4. List of contents of the current dir: long format

$ ls -l
total 36
drwx------+ 6 yoander yoander 4096 Jun 23 20:09 app
drwx------+ 2 yoander yoander 4096 Jun 23 20:09 bin
-rwx------+ 1 yoander yoander 1577 Jun  5 17:10 deps
-rwx------+ 1 yoander yoander  487 Jun  5 17:10 deps.lock
-rwx------+ 1 yoander yoander 1065 Apr 26 13:24 LICENSE
-rwx------+ 1 yoander yoander 6405 Apr 26 13:25 README.md
drwx------+ 3 yoander yoander 4096 Jun 23 20:09 src
drwx------+ 3 yoander yoander 4096 Jun 23 20:09 web

5. Omit owner and group

$ ls -lgG
total 36
drwx------+ 6 4096 Jun 23 20:09 app
drwx------+ 2 4096 Jun 23 20:09 bin
-rwx------+ 1 1577 Jun  5 17:10 deps
-rwx------+ 1  487 Jun  5 17:10 deps.lock
-rwx------+ 1 1065 Apr 26 13:24 LICENSE
-rwx------+ 1 6405 Apr 26 13:25 README.md
drwx------+ 3 4096 Jun 23 20:09 src
drwx------+ 3 4096 Jun 23 20:09 web

6. Show the size of each file in understandable format

$ ls -lgGh
total 36K
drwx------+ 6 4.0K Jun 23 20:09 app
drwx------+ 2 4.0K Jun 23 20:09 bin
-rwx------+ 1 1.6K Jun  5 17:10 deps
-rwx------+ 1  487 Jun  5 17:10 deps.lock
-rwx------+ 1 1.1K Apr 26 13:24 LICENSE
-rwx------+ 1 6.3K Apr 26 13:25 README.md
drwx------+ 3 4.0K Jun 23 20:09 src
drwx------+ 3 4.0K Jun 23 20:09 web

7. Show date in long format
Useful if you want to know the exact moment when a file was modified

$ ls --full-time
total 36
drwx------+ 6 yoander yoander 4096 2012-06-23 20:09:58.525512425 -0400 app
drwx------+ 2 yoander yoander 4096 2012-06-23 20:09:58.525512425 -0400 bin
-rwx------+ 1 yoander yoander 1577 2012-06-05 17:10:00.000000000 -0400 deps
-rwx------+ 1 yoander yoander  487 2012-06-05 17:10:00.000000000 -0400 deps.lock
-rwx------+ 1 yoander yoander 1065 2012-04-26 13:24:24.000000000 -0400 LICENSE
-rwx------+ 1 yoander yoander 6405 2012-04-26 13:25:59.000000000 -0400 README.md
drwx------+ 3 yoander yoander 4096 2012-06-23 20:09:58.529512587 -0400 src
drwx------+ 3 yoander yoander 4096 2012-06-23 20:09:58.605511982 -0400 web

Further reading

- man ls
- info ls

The tutorials here on LibreByte are provided under a free software licence. if you like my work you should consider:
Buy a Hosting/VPS or Dedicated Server at MCKHost

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.